1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Session_ID not being consistently retrieved

Discussion in 'PHP' started by Darden12, Nov 24, 2008.

  1. #1
    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!
    SEMrush
    if ($_SESSION['identifier'] == "Brian") {
     
    
    echo "Session ID found";
    
    } else {
    
    echo "Session ID not found";
    
    }
    PHP:
     
    Darden12, Nov 24, 2008 IP
    SEMrush
  2. elias_sorensen

    elias_sorensen Active Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    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.
     
    elias_sorensen, Nov 24, 2008 IP
  3. Darden12

    Darden12 Well-Known Member

    Messages:
    107
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    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"
     
    Darden12, Nov 25, 2008 IP
  4. elias_sorensen

    elias_sorensen Active Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #4
    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.
     
    elias_sorensen, Nov 25, 2008 IP