2

I'm having trouble with PHP session variables: I try setting the variables, and they can't be read even in the same 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.

3 Answers 3

6

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 :

Sign up to request clarification or add additional context in comments.

4 Comments

Right, of course. I've been coding in PHP for about a year, and this is the first times I've screwed that up... Must have been pretty tired when I wrote that code.
Huhu ^^ sometimes, having another person look at your code helps :-) (It's the kind of mistake we all do... and never see it when it's in our own code ^^ )
Yup. Thanks for the answer. I have another question, if you might be able to answer it (would SO rather I create another question?): Is there an easy way to check what variables are set, through the browser, or am I better off trying to print off most of the variables?
If the question is a new one (i.e. something not exactly related to this one), you'd better create a new question, I suppose : it would be pushed at the top of the questions list, it would allow other people to answer, ...
2

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.