1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: php $_SESSION and function header()

Discussion in 'Web Design & Development' started by andynic, Feb 28, 2015.

Thread Status:
Not open for further replies.
Advertisement
  1. andynic

    andynic Thread Starter

    Joined:
    May 25, 2007
    Messages:
    386
    Hi,

    I'm almost certain that the problem is that I do not have a good understanding of the concept of "session" in php.

    I am under the, very likely mistaken, impression that if I have a series of scripts beginning with index.php, where index.php calls a.php and a.php calls b.php, and so forth, that as long as I have session_start() at the beginning of index.php, then anything that has been put into $_SESSION by index.php, or a.php, it will also be there when b.php is executing.

    I made the following two test scripts:
    The calling script (x1.php)
    Code:
    <?php
    session_start(); 
    $_SESSION['categorie'] = "ALL";
     header("Location: http://192.168.178.22/~andynic/andyTest/public_html/x2.php");
    exit();
    ?>
    
    The called script (x2.php)
    Code:
    <?php
    echo __FILE__ . " SESSION['categorie'] = " . $_SESSION['categorie'] . "<br />";
    ?>
    
    x2.php displays:
    /Users/andynic/Sites/andyTest/public_html/x2.php SESSION['categorie'] =

    The following appears in the Apache error log:
    [28-Feb-2015 18:36:24 Europe/Amsterdam] PHP Notice: Undefined variable: _SESSION in /Users/andynic/Sites/andyTest/public_html/x2.php on line 2

    Can someone please explain where I am going wrong in my thinking and also tell me how I can achieve what I want to do, that is, have x2.php display:
    /Users/andynic/Sites/andyTest/public_html/x2.php SESSION['categorie'] = ALL.

    Thanks for your help.
    Andynic
     
  2. JiminSA

    JiminSA

    Joined:
    Dec 15, 2011
    Messages:
    3,388
    First Name:
    Jim
    Hi andynic:)
    In order for subsequent php 'pages' to 'join' the session and share the variables after a header redirect, they must also contain the session_start(); instruction. Re-executing it, does not, as one would have thought, start a new session, but rather enables session 'continuation'.
     
  3. Ent

    Ent Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,467
    First Name:
    Josiah
    The issue is that you're not continuing one script. In your header line, you're redirecting them to another script which they have to start over with.
    If you were instead using to use require or require_once to pull the instructions in x2 and run them, they would then be run as part of your calling script.

    Imagine by analogy a call centre,
    In the model that you're using the first guy, Andrew, to pick up says "I can't help you, let me transfer you to Bill", and after a bit of hold music Bill is on the other end of the phone and asking your name (again) and opening up your file on his computer system.
    The alternative, which would work as you imagine, is where the Andrew hollars accross the room saying "How do I fix the widgit again?", he gets his answer from Bill, and then you continue talking to Andrew (who already knows your name and has your file open.)
     
  4. andynic

    andynic Thread Starter

    Joined:
    May 25, 2007
    Messages:
    386
    Hi Jim and Ent,

    Thanks for the replies.

    Adding session_start() to x2.php solved the problem.

    I do agree, Jim, it is not "intuitively obvious". My thinking was just as you suggest when you say "one would have thought....".

    Andynic
     
  5. JiminSA

    JiminSA

    Joined:
    Dec 15, 2011
    Messages:
    3,388
    First Name:
    Jim
    Cool Andy - glad you've got a handle on it!
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1143952

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice