Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    153
    Thanks
    53
    Thanked 0 Times in 0 Posts

    Problem with logging in code

    Hi I'm having problems with a login code. The problem is that the first time I login, then go to a different page, it logs me out. But on the second login, this problem doesn't happen.
    I use a page login.php to process login:
    PHP Code:
    <?php
    mysql connection stuff
    session_start
    ();
    $user=$_POST['user'];
    $pass=$_POST['pass'];
    $loginchek=mysql_query("SELECT * FROM users where user='$user'") or die(mysql_error());
    $loginchek2=mysql_fetch_array($loginchek);
    if(
    $loginchek2['pass']==$pass){

    $_SESSION['user']=$loginchek2['user'];

    $_SESSION['userid']=$loginchek2['id'];
    header('Location: index.php');
    echo
    "You are now logged in!";

    }
    else{
    echo
    "Wrong password";
    }

    ?>
    What could be causing the problem?

    Thanks

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    Quote Originally Posted by wincode View Post
    Hi I'm having problems with a login code. The problem is that the first time I login, then go to a different page, it logs me out. But on the second login, this problem doesn't happen.
    I use a page login.php to process login:
    PHP Code:
    <?php
    mysql connection stuff
    session_start
    ();
    $user=$_POST['user'];
    $pass=$_POST['pass'];
    $loginchek=mysql_query("SELECT * FROM users where user='$user'") or die(mysql_error());
    $loginchek2=mysql_fetch_array($loginchek);
    if(
    $loginchek2['pass']==$pass){

    $_SESSION['user']=$loginchek2['user'];

    $_SESSION['userid']=$loginchek2['id'];
    header('Location: index.php');
    echo
    "You are now logged in!";

    }
    else{
    echo
    "Wrong password";
    }

    ?>
    What could be causing the problem?

    Thanks
    1) encase ur login variables with mysql_escape_string(); function
    2) u need session_start(); function on all pages
    3) if ur going to just use the username as the session why not just use $_SESSION['username'] = $_POST['username'] ?

    also when you check for user credentials you want to do SELECT username, userid FROM users WHERE username='$username' AND password='$password'

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Do you have this at the top of every other page too? (you should have it).

    <?php
    mysql connection stuff
    session_start();


    Also,
    Change this line:
    echo"You are now logged in!";
    To:
    exit;

    You're redirecting, so that line should not be there.


  •  

    Posting Permissions

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