I have a session_ID problem that does not seem to be discussed on the Web. The following script will correctly echo "Session ID found" when the following lines are run as part of a page I've called article.php. However, when I run the exact same script on the exact same browser in a different file (something like counthits.php) the script echoes "Session ID not found"! How can this be? It happens both on Firefox and on Safari. Both will recognize my session ID when this script below is used in the article.php file, but won't recognize my session ID when the same script is used as part of my counthits.php file. Many thanks in advance for any clues as to why I'm getting these Twilight Zone results! if ($_SESSION['identifier'] == "Brian") { echo "Session ID found"; } else { echo "Session ID not found"; } PHP:
Do you remember to start your session in the top of counthits.php? session_start(); PHP: If you already does that, you can print the whole session array in counthits.php to see what's really being passed in the session: print_r($_SESSION); PHP: A quick explanation, is that you're maybe checking your session another place in the code, and forgot an =. e.g. if($_SESSION[something] = "something"){ <---- There's only one =, which should be two. When having only one, it puts "something" into $_SESSION[something], and will return true. Actually, when building big scripts and applications, I don't use e.g. if($_SESSION[something] == "something") but using: if("something" == $_SESSION[something]). If you somewhere in your code forgetting an = without notice, it wont change your session. if("something" = $_SESSION[something]) <-- This wont change your session.
Thanks for the quick response. Unfortunately, I do have the double equal sign in both versions of the script and both start with session_start. When I use print_r($_SESSION), it outputs the word "Array"
Okay... So something happens when you're changing the page (from article.php to counthits.php) or somewhere in the article.php file. Try, bottom most in article.php to print_r($_SESSION); and see what it returns.