+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Login system problems

  1. #1
    taigah50 is offline x10Hosting Member taigah50 is an unknown quantity at this point
    Join Date
    Nov 2010
    Posts
    11

    Login system problems

    Below is my login script.

    PHP Code:
    <?php    
        $username 
    $_POST['username'];
        
    $password $_POST['password'];
        
        
    //connect to the database here
        
        
    if($_GET['action'] == "login")
    {
        
    $dbhost 'localhost';
        
    $dbname 'taigah_members';
        
    $dbuser 'taigah_th100';
        
    $dbpass 'taigaisb';

        
    $conn mysql_connect($dbhost$dbuser$dbpass);
        
    mysql_select_db($dbname$conn);
    }

        
    //sanitize username
        
    $username mysql_real_escape_string($username);
        
        
    $query "INSERT INTO users ( username, password, salt )
                VALUES ( '
    $username' , '$hash' , '$salt' );";
        
    mysql_query($query);
            
        
    $username mysql_real_escape_string($username);
        
            
    $query "SELECT password, salt
            FROM users
            WHERE username = '
    $username';";
        
    $result mysql_query($query);
        
        if(
    mysql_num_rows($result) < 1//no such user exists
    {
            
    header('Location: login_form.php?login=failed&cause='.urlencode('Invalid User/Password'));
            die();
    }
        
        
    $userData mysql_fetch_array($resultMYSQL_ASSOC);
        
    $hash sha1$userData['salt'] . sha1($password) );
        
        if(
    $hash != $userData['password']) //incorrect password
    {
            
    header('Location: login_form.php?login=failed&cause='.urlencode('Invalid User/Password'));
            die();
    }

        
    //Login successful; redirect to another page or display "login success" message
        
    session_register("$username");
        
    session_register("$password");
        
    header('Location: index.php');
        
        
    mysql_close();
        
    ?>
    When it tries to redirect to index.php, THIS happens to it.

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'taigah'@'10.33.248.75' (using password: NO) in /home/taigah/public_html/members/login.php on line 28

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/taigah/public_html/members/login.php on line 28

    Warning: mysql_query() [function.mysql-query]: Access denied for user 'taigah'@'10.33.248.75' (using password: NO) in /home/taigah/public_html/members/login.php on line 32

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/taigah/public_html/members/login.php on line 32

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'taigah'@'10.33.248.75' (using password: NO) in /home/taigah/public_html/members/login.php on line 34

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/taigah/public_html/members/login.php on line 34

    Warning: mysql_query() [function.mysql-query]: Access denied for user 'taigah'@'10.33.248.75' (using password: NO) in /home/taigah/public_html/members/login.php on line 39

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/taigah/public_html/members/login.php on line 39

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/taigah/public_html/members/login.php on line 41

    Warning: Cannot modify header information - headers already sent by (output started at /home/taigah/public_html/members/login.php:2 in /home/taigah/public_html/members/login.php on line 43
    Any solutions?

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Login system problems

    Well, it's telling you pretty clearly there that there is something wrong with your SQL connection. Looking at the code I see that you have wrapped your connect code in an if statement, so if you haven't set the GET data then no connection will be made. I recommend you look into transitioning over to using PDO for your database connections, it is both more secure and has more advanced query features.

    Additionally, at no point in your code have you created the $hash and $salt variables for your password and also I don't see why you are doing an insert of that data in the login command. When creating the hash, ensure you use one of the more recent hash algorithms (SHA256 etc) with a different salt for every user to prevent dictionary attacks.

  3. #3
    bdistler's Avatar
    bdistler is offline x10 Lieutenant bdistler is an unknown quantity at this point
    Join Date
    May 2010
    Location
    Catalina AZ USA
    Posts
    349

    Re: Login system problems

    Some things

    PHP Code:
    $username $_POST['username'];
    $password $_POST['password']; 
    You need to test both of these for what they are or are not

    PHP Code:
    if($_GET['action'] == "login"
    what to do if $_GET['action'] != "login" also use "===" for this test

    PHP Code:
    $dbhost 'localhost';
    $dbname 'taigah_members';
    $dbuser 'taigah_th100';
    $dbpass 'taigaisb'
    This stuff should be in a 'include' file above you root folder

    PHP Code:
    $conn mysql_connect($dbhost$dbuser$dbpass);
    mysql_select_db($dbname$conn); 
    This is where most of your errors start - you should test

    if (!$conn)
    {
    do something about the error
    }

    if (!mysql_select_db($dbname, $conn))
    {
    do something about the error
    }

    PHP Code:
    //sanitize username
    $username mysql_real_escape_string($username); 
    before you use "mysql_real_escape_string"
    test if "Magic Quotes" is set on
    (a server at x10Hosting had it set on)

    if (get_magic_quotes_gpc())
    {
    undo the effects of "Magic Quotes" on $_POST[$username]
    before mysql_real_escape_string($username)
    }

    you do not need it after "mysql_query($query);"

    PHP Code:
    $query "INSERT INTO users ( username, password, salt )
            VALUES ( '
    $username' , '$hash' , '$salt' );";
    mysql_query($query); 
    if you do this now then your test "if(mysql_num_rows($result) < 1) //no such user exists"
    will all-ways be true
    as lemon-tree said you never set $hash or '$salt

    PHP Code:
    $query "SELECT password, salt FROM users WHERE username = '$username';"
    The end of this line ==>';";<== S/B ==>'";<==

    after the query test the result

    if (!$result)
    {
    do something about the error
    }

    PHP Code:
    session_register("$username"); 
    use "Sessions"

    NEVER store users name and password

    take the user's name and password add in some salt pull a hash on the mix
    save the hash in the data base
    then in this script (at login) take name and password mix in the salt pull a hash
    look for the hash in the database

    be sure to use a strong password on your data base and for the FTP to your site

    I agree with lemon-tree take a look at using PDO for your database connections
    but what you have started will work
    Last edited by bdistler; 11-14-2010 at 08:22 PM. Reason: remove caps from Lemon-Tree

  4. #4
    taigah50 is offline x10Hosting Member taigah50 is an unknown quantity at this point
    Join Date
    Nov 2010
    Posts
    11

    Re: Login system problems

    Right, thanks. I've started a PDO version of my login.

  5. #5
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Login system problems

    Right, thanks. I've started a PDO version of my login.
    Good to hear. Six months down the road you'll be thanking yourself for taking the time now to do this, when your code is more advanced and caters for a larger site.

  6. #6
    taigah50 is offline x10Hosting Member taigah50 is an unknown quantity at this point
    Join Date
    Nov 2010
    Posts
    11

    Re: Login system problems

    Until my PDO code is complete, I've decided to use MySQL.

    Anyway, here's an excerpt from my revised code:

    PHP Code:
    $username $_POST['username'];
    $dbhost 'localhost';
    $dbname 'taigah_members';
    $dbuser 'taigah_th100';
    $dbpass 'taigaisb';
    $conn mysql_connect($dbhost$dbuser$dbpass);
    mysql_select_db($dbname$conn);
    $username mysql_real_escape_string($username);
    $query "SELECT password, salt FROM users WHERE username = '$username'";
    $result mysql_query($query);
    $userData mysql_fetch_array($result);
    $hash sha1$userData['salt'] . sha1($password) );
    if(
    $hash != $userData['password']) //incorrect password
    {
    header('Location: login_form.php?login=failed&cause='.urlencode('Invalid User/Password'));
    die();
    }
    mysql_close(); 
    This gives the following parse error:
    Parse error: syntax error, unexpected T_IF in /home/taigah/public_html/members/login.php on line 54
    Well... since the above code is just an excerpt, the error is at "if($hash != $userData['password']) //incorrect password". Did I miss a mistake?

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Login system problems

    The fragment you posted has no syntax error. Remember, sample code should be representative of the actual code (it should also be complete, which the sample is not). Code (both sample and production) should also be indented consistently according to some style, which will make some mistakes more obvious.

    As for the error, one possibility is that the line before the "if" is missing a terminating semicolon.
    Last edited by misson; 11-15-2010 at 10:11 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  8. #8
    taigah50 is offline x10Hosting Member taigah50 is an unknown quantity at this point
    Join Date
    Nov 2010
    Posts
    11

    Re: Login system problems

    Quote Originally Posted by misson View Post
    The fragment you posted has no syntax error. Remember, sample code should be representative of the actual code (it should also be complete, which the sample is not). Code (both sample and production) should also be indented consistently according to some style, which will make some mistakes more obvious.

    As for the error, one possibility is that the line before the "if" is missing a terminating semicolon.
    PHP Code:
    <?php
        session_start
    ();
        
        
    $username $_POST['username'];
        
    $password $_POST['password'];
        
        
    //connect to the database here
        
        
    $dbhost 'localhost';
        
    $dbname 'taigah_members';
        
    $dbuser 'taigah_th100';
        
    $dbpass '*******';

        
    $conn mysql_connect($dbhost$dbuser$dbpass);
        
    mysql_select_db($dbname$conn);
        
        if (!
    $conn)
    {
        
    mysql_close();
        
    header('Location: login_form.php?login=failed&cause='.urlencode('Database error.'));
        die();
    }

        if (!
    mysql_select_db($dbname$conn))
    {
        
    mysql_close();
        
    header('Location: login_form.php?login=failed&cause='.urlencode('Cannot connect to database.'));
        die();
    }
        
    //sanitize username
            
        
    $username mysql_real_escape_string($username);
        
            
    $query "SELECT password, salt
            FROM users
            WHERE username = '
    $username'";
        
    $result mysql_query($query);
        
        if(!
    $result)
    {
        
    header('Location: login_form.php?login=failed&cause='.urlencode('Database is unavailable.'));
        die();
    }
        
        if(
    mysql_num_rows($result) < 1//no such user exists
    {
            
    header('Location: login_form.php?login=failed&cause='.urlencode('Invalid User/Password'));
            die();
    }
        
        
    $userData mysql_fetch_array($result);
        
    $hash sha1$userData['salt'] . sha1($password) );
     
        if(
    $hash != $userData['password']) //incorrect password
    {
        
    header('Location: login_form.php?login=failed&cause='.urlencode('Invalid User/Password'));
        die();
    }

        
    //Login successful; redirect to another page or display "login success" message
        
    header('Location: index.php');
        
        
    mysql_close();
        
    ?>
    Can't see any syntax errors, either. And the semicolons are in their rightful places.
    Last edited by taigah50; 11-16-2010 at 09:13 AM.

  9. #9
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Login system problems

    Are you sure that code generates the syntax error you mentioned?

    As a link in my previous post explains, example code should also be concise, which benefits you just as much as us, for various reasons. It's especially helpful when it comes to syntax errors. It's still not self-contained and, if it doesn't generate the error, it's not representative.

    That better not be your real DB password I see.

    As lemon-tree points out, SHA1 is considered broken by security experts. You can call other hash functions with hash. If there are any real users in your database, you can process them by adding a new column to indicate which users have been updated (or haven't been updated, or which hash was used to hash the password). When a user successfully logs in that hasn't been processed, re-hash their password and update the DB.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  10. #10
    taigah50 is offline x10Hosting Member taigah50 is an unknown quantity at this point
    Join Date
    Nov 2010
    Posts
    11

    Post Re: Login system problems

    Below's my latest revision, and it still gives me the same parse error, but on a different line.

    PHP Code:
    <?php
        $username 
    $_POST['username'];
        
    $password $_POST['password'];
            
        function 
    validateUser()
    {
        
    session_regenerate_id (); //this is a security measure.
        
    $_SESSION['valid'] = 1;
        
    $_SESSION['userid'] = $userid;
    }
        
    //connect to the database here
        
        
    $dbhost 'localhost';
        
    $dbname 'taigah_members';
        
    $dbuser 'taigah_th100';
        
    $dbpass '********';

        
    $conn mysql_connect($dbhost$dbuser$dbpass);
        
    mysql_select_db($dbname$conn);
        
        if (!
    $conn)
    {
        
    mysql_close();
        
    header('Location: login_form.php?login=failed&cause='.urlencode('Database error.'));
        die();
    }

        if (!
    mysql_select_db($dbname$conn))
    {
        
    mysql_close();
        
    header('Location: login_form.php?login=failed&cause='.urlencode('Cannot connect to database.'));
        die();
    }
        
    //sanitize username
            
        
    $username mysql_real_escape_string($username);
        
            
    $query "SELECT username, password, salt
            FROM users
            WHERE username = '
    $username';";
            
        
    $result mysql_query($query);
        
    $userData mysql_fetch_array($resultMYSQL_ASSOC);
        
        if(
    $username != $userData['username']) //no such user exists
    {
            
    header('Location: login_form.php?login=failed&cause='.urlencode('Invalid User/Password'));
            die();
    }

        
    $hash md5$userData['salt'] . md5($password) );
    *
        if(
    $hash != $userData['password']) //incorrect password
    {
        
    header('Location: login_form.php?login=failed&cause='.urlencode('Invalid User/Password'));
        die();
    }

        
    validateUser();
        
        
    //Login successful; redirect to another page or display "login success" message
        
        
    mysql_close();
        
        
    header('Location: index.php');
    ?>
    I am clearly sure that the code above has correct syntax. I also converted from sha1 to md5 - this might be the problem.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 07-26-2010, 09:05 AM
  2. Login System
    By nitin94 in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 10-14-2009, 05:02 PM
  3. problems with system()
    By roelmb in forum Programming Help
    Replies: 3
    Last Post: 10-01-2009, 08:05 AM
  4. Problems with Registration system [PHP Code included]
    By Jarryd in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 11-24-2007, 05:38 AM
  5. login system help
    By brentcatoe in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 10-04-2005, 04:31 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