Remember Username & Password

Discussion in 'PHP' started by webgenius_coder, Mar 15, 2008.

  1. #1
    At present I'm using $_SESSION['UserName'] to create and store the login details. But I'm not able to access this session variable. Is there any way for the website to remember these details?

    $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 "LoginOK.php"
    	session_start();
    	session_register("myusername");
    	session_register("mypassword");
    	$_SESSION['loginname'] = $myusername;
    	//echo $_session['loginname'];
    	header("location:index.php");
    }
    PHP:
    I'm using <?php echo $_session['loginname']; in index.php. Is there anything wrong with the code?
     
    webgenius_coder, Mar 15, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Did you place session_start() on all pages where the session should run?

    And don't use session_register(), it's deprecated. Do it just like you do below in your code:
    
    $_SESSION['loginname'] = $myusername;
    
    PHP:
    Where does $myusername come from? Maybe the variable just doesn't contain what you expect, and therefore it's not working...
     
    nico_swd, Mar 15, 2008 IP
  3. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #3
    yes he must be forgetting session_start()

    Regards

    Alex
     
    kmap, Mar 16, 2008 IP
  4. The Critic

    The Critic Peon

    Messages:
    392
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The Critic, Mar 16, 2008 IP
  5. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You also need to declare session_start() as the very first line of code on every page. It needs to be called before any headers are sent, otherwise it will fail.
     
    Christian Little, Mar 16, 2008 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    There are no problems with you code, everything seems fine. However, I would do it using database (more professional).

    Give the user a unique session ID which is stored in the database and use it to retrieve the user info on each page load.

    Peace,
     
    Barti1987, Mar 16, 2008 IP