Session variable not passing between pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Session variable not passing between pages

Post by pthomas »

I'm lost on this one. I went throught the session tutorial on http://www.phpfreaks.com/tutorials/41/0.php and that was pretty easy.

In my page I'm working on I put all the session stuff inside an include file. For every php page that I have, they all have

Code: Select all

require_once "includes/insult.inc";
at the very top before anything else. This include file contains the needed

Code: Select all

session_start();
header("Cached-control: private");
on the very first two lines.

Now from how I understand the use of the $SESSION array, it works like a global and you can access it anywhere. I've got two variables that are set inside the insult.inc file as $_SESSION types $_SESSION['AI'] and $_SESSION['sound'], those two pass to any page just like they should. the variables $_SESSION['insult'] and $_SESSION['insult_URL]' get set inside a function in the same insult.inc file. Trouble is, the last two do not keep their contents between pages. I've tried taking them out of the functions, and moving them in various places, double checked spelling 8 times now by copy/pasting to make sure of spelling errors, but once I switch to another page, those last two become uindefined. I can echo their values out with

Code: Select all

echo $_SESSIONї'insult'];
but when I go to any other page, those two dissappear.

Any ideas? Are there any restrictions on $_SESSION variables for setting? I thought you could set them anywhere in your code once and then if you go to another page (provided the session_start is called on the next page as well) that they would still be defined. I do a

Code: Select all

print_r($_SESSION)
and it shows AI and sound correctly holding their values on the next page, but "insult" and "insult_URL" dissappear.

help.
Paul
Last edited by pthomas on Fri Mar 04, 2005 10:48 am, edited 2 times in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

How are you passing the variables using sessions? Do you put SID in your links?

The other two are in the include at the top of every page so they will always be in the SESSION array on all pages as you say that file is included on all pages.

Either:

1. Your session_start(); is being blocked by something
2. You you are not passing the session id between the pages

Can we see some more code?
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post by pthomas »

The include file that is included at the top of each file contains:

Code: Select all

<?PHP //filename: insult.inc

#########################
#       Includes
#########################
require_once "mysqlconnect.inc";
require_once "master.inc";

#########################
#       First Run
#########################
session_start();                                                //Enable sessions
header("Cached-control: private");                              //IE6 fix for sessions

  if ( (! isset($_GET&#1111;'AI'])) && (! isset($_POST&#1111;'AI'])))       //Neither one is set
  &#123;
        if (isset($_SESSION&#1111;'AI']))
                $AI = $_SESSION&#1111;'AI'];                  //Set AI from session
        else
                $AI = $_SESSION&#1111;'AI'] = "OFF";          //Nothing is set. Set session and AI to OFF
  &#125;
  else if (isset($_GET&#1111;'AI']))                          //If GET is the one set
  &#123;
        if ($_GET&#1111;'AI'] == "ON")                        //Get specified -> ON
                $AI = $_SESSION&#1111;'AI'] = "ON";
        else                                            //Something other than ON -> set to OFF
                $AI = $_SESSION&#1111;'AI'] = "OFF";
  &#125;
  else                                                  //POST must be set
        $AI = $_SESSION&#1111;'AI'] = $_POST&#1111;'AI'];

if ( (!isset($_POST&#1111;'sound'])) && (!isset($_GET&#1111;'sound'])) )
  &#123;
        if (isset($_SESSION&#1111;'sound']))
                $sound = $_SESSION&#1111;'sound'];
        else
                $sound = $_SESSION&#1111;'sound'] = "ON";             //Set sound, session to default
  &#125;
  else if (isset($_POST&#1111;'sound']))
        $sound = $_SESSION&#1111;'sound'] = $_POST&#1111;'sound'];

  else  //$_GET is set
  &#123;
        if ($_GET&#1111;'sound'] == "ON")
                $sound = $_SESSION&#1111;'sound'] = "ON";
        else                                            //Something other than ON -> set to OFF
                $sound = $_SESSION&#1111;'sound'] = "OFF";
  &#125;


skipping a few hundred lines......
?>
Now there is a function that is inside this file that when its called, should set the two session variables:

Code: Select all

function sp_or_gen(& $_list, $AI)
&#123;
   LEFT out the guts

        $_SESSION&#1111;'insult'] = ($name." ".$Name_calling." ".$So." ".$Misc);
        $_SESSION&#1111;'insult_URL'] = $_SERVER&#1111;'PHP_SELF']."?".create_get_codes($_list);

        return ($name." ".$Name_calling." ".$So." ".$Misc);
&#125;
I can echo those two $_SESSION variables and they are getting correctly set. For instance, after calling my sp_or_gen() function, I can print out $_SESSION['insult just fine along with $_SESSION['AI']. I click on a link to another web page on my site and then immediatley try and echo the $_SESSION variables and only "AI" and "sound" stay defined in this page. No matter which page I visit on my site (they all have that insult.inc file included before I do anything else to make sure that start_session() is called) AI and sound keep there values and the insult and insult_URL never do. This function is the only place that they are being set.

From the tutorial that I read over, it didn;t mention anything about needing to pass SID to another page, it just said to make sure and have start_session() on everypage that you want to be able to change/use session variables.

I've looked over and over this code for about 10 hours now and can;t find anything that would change only two session vars. It does not appear that passing the SID is causing the problem. If that were the case then I'd either get all or none. I had taken the session_start() out of the include file and then manually placed it in every php file on the top line and still had the same effect.

keep the ideas coming,
Paul
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post by pthomas »

Helpful hunting...

I retract what I said about SID not being a problem. I started printing out the sessionID:

Code: Select all

session_start();                                                //Enable sessions
header("Cached-control: private");                              //IE6 fix for sessions

echo "<BR>Session ID:".session_id();
and now when I'm on my insult.php page it has say session#1
when i go to either add_insult.html (has an i-frame with add_insult.php) or send_insult.php they both show that they are the same session, say session#2. So the session is changing somehow. I don;t understarnd why, each of the .php files has

Code: Select all

require_once "insult.inc"; [code/] on the first line.

Paul
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

do you switch directories or protocols or something between the URLs? Basically, what's the differences in the URLs used?
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post by pthomas »

Everything is in the same directory. The web pages are called via the POST method. on the insult.php page, it reloads itself a few times. The button HTML for it reloading (keeps session#1)
is

Code: Select all

<FORM ACTION=<?PHP echo $_SERVER&#1111;'PHP_SELF']; ?> METHOD=POST>
        <INPUT NAME="insult-again" type="hidden" align="right">
<SCRIPT TYPE="text/javascript">
<!--
var sr = new submitroll("images/again.gif","images/over/again_over.gif","mysubmit");
sr.write();
//-->
</SCRIPT>
<NOSCRIPT>
<INPUT type="image" src="images/again.gif">
</NOSCRIPT></FORM>
The button that takes it to another page is:

Code: Select all

<FORM ACTION="http://<our server>/send_insult.php" METHOD=POST>
        <SCRIPT TYPE="text/javascript">
<!--
var sr = new submitroll("images/send_this.gif","images/over/send_this_over.gif","send_this");
sr.write();
//-->
</SCRIPT>
<NOSCRIPT>
<INPUT type="image" src="images/send_this.gif">
</NOSCRIPT>
        </FORM>
They both use the post method...all my buttons use post come to think of it. The top one is the one that keeps the session correctly. I think I'll try putting in a bogus <INPUT TYPE=HIDDEN.....>

Paul
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post by pthomas »

No luck with adding in a bogus input. *sigh*

Paul
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post by pthomas »

*bangs head on desk repeatedly*
Guess what simple thing I forgot to do.
*bangs head on desk some more*

I'm using firefox (on my linux box) to test out the sessions and such. Decided to KVM to my winXP machine, fired up IE, everything worked fine, 100% correct like it should. Went back to my linux box wondering what the difference was. Closed Firefox completely and restarted it. Everything works fine.

*bangs head on desk yet again*

Note to self: closing a tab in FF is NOT the same in anyway as completely exiting the browser...sessions are quite dependant on this.
...was oging to post that, but seems that it randomly drops the session or something. I give up.
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post by pthomas »

Looked back on this with a fresh set of eyes after a relaxing weekend. Turned out it was exactly this:
do you switch directories or protocols or something between the URLs?
I was varying between using http://www.website.co.com and website.co.com

Thanks,
Paul
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that can cause the problem. You need to set the domain of the cookie to ".website.co.com" notice the leading dot.
Post Reply