+ Reply to Thread
Results 1 to 8 of 8

Thread: problems with Login page (PHP, mySQL}

  1. #1
    digitalimages is offline x10Hosting Member digitalimages is an unknown quantity at this point
    Join Date
    Feb 2009
    Posts
    47

    Question problems with Login page (PHP, mySQL}

    I have created a log in page and I have created a data base called "tiwebs_members" with a table called "members". I have added a test username and password to the table.
    The login form code is:

    Code:
    <form name='login' method='POST' action='checklogin.php' accept-charset='UTF-8'>
                <input type='hidden' name='sfm_form_submitted' value='yes'>
                
                <table cellspacing='0' cellpadding='5' border='0' bgcolor='#c1d0d7'>
                   <tr>
                      <td>
                         <table cellspacing='2' cellpadding='2' border='0'>
                            <tr>
                               <td colspan='2' align='center' class='form_heading'>
    Client Login
                               </td>
                            </tr>
                            <tr>
                               <td align='right' class='normal_field'>
    Username:
                               </td>
                               <td class='element_label'>
                                  <input type='text' name='Name' size='30'>
                               </td>
                            </tr>
                            <tr>
                               <td align='right' class='normal_field'>
    Password:
                               </td>
                               <td class='element_label'>
                                  <input type='password' name='Password' size='30'>
                               </td>
                            </tr>
                            <tr>
                               <td colspan='2' align='center'>
                                  <div id='login_Submit_errorloc' class='error_strings'>
                                  </div>
                                  <input type='submit' name='Submit' value='Login'>
                               </td>
                            </tr>
                         </table>
                      </td>
                   </tr>
                </table>
             </form>


    The checklogin. php is below.
    Code:
    <?php
    ob_start();
     $host="localhost"; // Host name
    $username="removed_for _security"; // Mysql username 
    $password="removed_for _security"; // Mysql password 
    $db_name="tiwebs_members"; // Database name
    $tbl_name="members"; // Table name
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    // Define $myusername and $mypassword
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];
    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_register("myusername");
    session_register("mypassword");
    header("location:login_success.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ob_end_flush();
    ?>
    I have remove the actual password and username for myphpadmin for security.
    When I try to login with the test user name and password I get an error message saying: "Wrong Username or Password " I assume that the code is working correctly but somehow not finding the data base. Using PHP 5.2
    Any obvious problems? Suggestions? :dunno:
    Thanks for any help.
    Last edited by digitalimages; 05-29-2009 at 06:48 PM.

  2. #2
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: problems with Login page (PHP, mySQL}

    If the credentials you used to connect were your cPanel username/password, then it will not work.

    If this is the case, go to CPanel > MySQL Databases and use the “Add New User” give the proper permissions and then do the “Add User To Database” step and use these new username and password to connect.

    -- Edit --
    I also noticed that your html does not post 'myusername' but ‘Name’.

    Code:
    (html)
    <input type='text' name='Name' size='30'>
    
    (php)
    $myusername=$_POST['myusername'];
    Give them the same name. Same thing for ‘password’.
    Last edited by gomarc; 05-29-2009 at 09:06 PM.

  3. #3
    digitalimages is offline x10Hosting Member digitalimages is an unknown quantity at this point
    Join Date
    Feb 2009
    Posts
    47

    Re: problems with Login page (PHP, mySQL}

    Thanks gomark.....
    the username and password were created in myPHPadmin and no way resemble my cpanel log in.
    I changed the form from "name" to "myusername" and that didn't make any difference.
    Thanks anyway....

    I noticed that I posted this in the wrong section...should have been in "
    Programming Help "
    Last edited by digitalimages; 05-30-2009 at 12:11 AM.

  4. #4
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: problems with Login page (PHP, mySQL}

    Code:
    (php)
    $sql="SELECT * FROM '$tbl_name' WHERE ...
    Edit:
    Please note:

    Use of session_register() is deprecated
    $barney = "A big purple dinosaur.";
    session_register("barney");

    // Use of $_SESSION is preferred, as of PHP 4.1.0
    $_SESSION["zim"] = "An invader from another planet.";
    Source: http://us2.php.net/manual/en/functio...n-register.php
    Last edited by gomarc; 05-30-2009 at 12:25 AM. Reason: Automerged Doublepost

  5. #5
    digitalimages is offline x10Hosting Member digitalimages is an unknown quantity at this point
    Join Date
    Feb 2009
    Posts
    47

    Re: problems with Login page (PHP, mySQL}

    thanks again gomarc ...(sorry for wrong spelling above)
    I did change both calls in the login form.
    I have changed a few things but am still getting erros around the "session" functions.
    I have changed the code to the following but still think that there is a syntax error in the "SESSION" area.

    Code:
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // Define $myusername and $mypassword
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];
    
    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    
    if($count==1){
    // Register $myusername, $mypassword and redirect to file "private.php"
    $_SESSION["myusername"];
    $_SESSION["mypassword"];
    header("private.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    
    ob_end_flush();
    ?>
    I guess that things have changed from earlier php to 5
    thanks again

  6. #6
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: problems with Login page (PHP, mySQL}

    You are still missing the apostrophes surrounding $tbl_name:

    Quote Originally Posted by gomarc View Post
    Code:
    (php)
    $sql="SELECT * FROM '$tbl_name' WHERE ...

  7. #7
    digitalimages is offline x10Hosting Member digitalimages is an unknown quantity at this point
    Join Date
    Feb 2009
    Posts
    47

    Re: problems with Login page (PHP, mySQL}

    sorry changed that too and still getting error mesages

  8. #8
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: problems with Login page (PHP, mySQL}

    Code:
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    This query seems to be correct. No single quotes wrapping $tbl_name, and singles quotes wrapping '$myusername' and '$mypassword'

    However, please check the spelling of your field names username and password in your table ‘members’. They must be exactly username and password (remember that php is case sensitive)

    Then, add ‘location: ’ to your header:
    Code:
    header("location: private.php");

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. Process PHP from a MySQL array
    By tnl2k7 in forum Programming Help
    Replies: 12
    Last Post: 05-05-2008, 10:02 AM
  3. Replies: 5
    Last Post: 04-06-2008, 12:47 PM
  4. Unstand PHP?
    By o0slowpaul0o in forum Tutorials
    Replies: 8
    Last Post: 01-07-2008, 09:16 PM
  5. MySQL Problems - Refreshing the cPanel Page
    By nyllo in forum Feedback and Suggestions
    Replies: 1
    Last Post: 12-24-2007, 01:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers