Page 3 of 3 FirstFirst 123
Results 21 to 24 of 24

Thread: User login script error

  1. #21
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ok, I changed the code to what you said, and I get the login success message, but now I am not sure how to check to see if the user is logged in on other pages (the protected ones). Also, I don't really understand how the password is being checked, and I would like to seeing how I am doing this as a learning experience . Any help and explanations would be greatly appreciated

  2. #22
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Now that you are getting the login success message, add what you tried to add before (the setcookie items.)

    Quote Originally Posted by Titan85

    Code:
    $expire = time() + (7*86400);
    
    setcookie("username", $user, $expire); //Set Username cookie
    
    setcookie("password", $pass, $expire); //Set password cookie
    
    //Success
    echo 'Success, you have been logged in!<br />';
    echo '<a href="cpanel.php">Continue</a>...';
    The above will go where the login success message is, and whatever you want to happen if they get the login failed message would go there.

    I don't really understand how the password is being checked
    In your first query (after you changed it to check only for rows with the matching username), you get the information from the only row with the username that you entered. After that, the following line kicks in:

    Code:
    $q = mysql_fetch_array($result);
    That gets the table column names and puts them in an array. After that, we check against the db for a match with the entered password after its md5 encryption:

    Code:
    $password = md5($password);
    
    if ($password == $q[password]) {
    
    echo 'Login Success!';
    }
    
    else {
    
    echo 'Login Failed';
    }
    In other words (for the above code), if the entered md5 password hash matches the one in the db, continue with login success. If it does not match, then the login failed.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #23
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by thetestingsite View Post
    Now that you are getting the login success message, add what you tried to add before (the setcookie items.)
    That may not be the best way to do it. I would use something along these lines.
    Log them in
    PHP Code:
    @session_start();//Start the session, Must be called before anything is sent to the browser
    $_SESSION['user'] = $username;//Remember the username
    $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];//Remember the IP.Another line of defense against the session getting hijacked 
    and check the login
    PHP Code:
    @session_start();//Must be called before anything is sent to the browser
    if(!empty($_SESSION['user']) && !empty($_SESSION['ip']) && $_SESSION['ip'] == $_SERVER['REMOTE_ADDR']){
        echo 
    'User is logged in';


  4. #24
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Either way is fine, just a matter of opinon. I simply suggested adding the part (s)he already had in the original script due to the fact (s)he is currently learning, and if it was already in the original, that means it was already learned. Sessions is a good way to go about user authentication, but it is pretty much the same as a cookie (with the exception that it is not stored client side [browser]).
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •