Solved: php $_SESSION and function header()

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

andynic

Thread Starter
Joined
May 25, 2007
Messages
425
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
 

JiminSA

Jim
Joined
Dec 15, 2011
Messages
3,407
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'.
 

Ent

Josiah
Retired Trusted Advisor
Joined
Apr 11, 2009
Messages
5,467
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.)
 

andynic

Thread Starter
Joined
May 25, 2007
Messages
425
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
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Members online

Top