开发者

PHP session variables not being set

I'm having trouble with PHP session variables: I try setting the variables, and they can't be read even in the same开发者_如何学Python file, much less another file.

Sorry if the question has been asked before, but I can't find anything that doesn't say "make sure you have session_start() at the beginning of the file."

Lines 1, 2, & 3 of file:

<?php
    ini_set('session.save_path','/home/[username]/session_data'); // Just in case, points to a correct directory
    session_start();

Later, after username validation, and login, the variables are set, and code tries to echo them out:

$_SESSION['login'] == "true";
$_SESSION['username'] == $username;
$_SESSION['password'] == sha1($password);
//header("Location: [admin page hidden]"); // Code would normally redirect to admin page (commented out for debugging), and exit
//exit;
echo "Username: ".$username."<br />"; // The following 4 lines try to print out the data
echo "Password: ".$password."<br />";
echo "Secure Password: ".sha1($password)."<br />";
echo "Session Username: ".$_SESSION['username']."<br />";

The output is:

Username: root
Password: [correct password]
Secure Password: [correct sha1 version of password]
Session Username: 

The secure password (sha1, what is checked) matches what is in the database file, I checked. Nothing comes out for the session variables, even though it was created 7 lines previously. The directory that I pointed the save path to just has a blank file for the session.

Does anyone have any clue why this doesn't work, or advice on how to proceed? Any helpful response is appreciated.


The following lines are the cause of the problem :

$_SESSION['login'] == "true";
$_SESSION['username'] == $username;
$_SESSION['password'] == sha1($password);

Those lines do pretty much nothing : they compare what's before the == with what's after it ; and do nothing with the result of that comparison.


To assign values to variables, you should use ONE =, and not two :

$_SESSION['login'] = "true";
$_SESSION['username'] = $username;
$_SESSION['password'] = sha1($password);

Those three lines assign what's on the right of the = to what's on its left.


In PHP :

  • == is a comparison operator
  • while = is an assignment operator.


You are using ==, which is an equality test. Instead use =.


What does the webserver's log says? It must be some rights issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜