Not all session variables stored?

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

Locked
bmarklan
Forum Newbie
Posts: 4
Joined: Wed Jul 20, 2005 2:43 pm

Not all session variables stored?

Post by bmarklan »

This is really strange... The session doesn't seem to be storing data like it should.

On the first page I have this code:

Code: Select all

print session_name()."<br>\n";
print session_id()."<br>\n<pre>";
print "SESSION\n";
var_dump(array_keys($_SESSION));
print "\nSESSION[0]\n";
var_dump(array_keys($_SESSION[0]));
print "</pre>\n";
which results in:

Code: Select all

PEWeb
1a5da917d0b08c4293f35e58f789ce09

SESSION
array(7) {
  &#1111;0]=&gt;
  string(2) &quote;Qs&quote;
  &#1111;1]=&gt;
  int(0)
  &#1111;2]=&gt;
  string(11) &quote;ArrProdVals&quote;
  &#1111;3]=&gt;
  string(13) &quote;MouseOverText&quote;
  &#1111;4]=&gt;
  string(8) &quote;GraphMin&quote;
  &#1111;5]=&gt;
  string(8) &quote;GraphMax&quote;
  &#1111;6]=&gt;
  string(5) &quote;dist0&quote;
}

SESSION&#1111;0]
array(7) {
  &#1111;0]=&gt;
  string(7) &quote;HashVar&quote;
  &#1111;1]=&gt;
  string(7) &quote;CSVdata&quote;
  &#1111;2]=&gt;
  string(8) &quote;FileName&quote;
  &#1111;3]=&gt;
  string(5) &quote;Image&quote;
  &#1111;4]=&gt;
  string(5) &quote;count&quote;
  &#1111;5]=&gt;
  string(2) &quote;DF&quote;
  &#1111;6]=&gt;
  string(2) &quote;DT&quote;
}
Yet, when I open a new window with the code

Code: Select all

session_name('PEWeb');
session_start();
plus the above code, I get:

Code: Select all

PEWeb
1a5da917d0b08c4293f35e58f789ce09

SESSION
array(1) {
  &#1111;0]=&gt;
  string(2) &quote;Qs&quote;
}

SESSION&#1111;0]
NULL
As you can see the session name and ID are the same in both, yet only the first session variable is being passed. Any ideas on why this might happen?

Thanks,
BAM
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

try this.. what the output..

Code: Select all

session_start();
$_SESSION = array("1"=>'one',
						  "2"=>'two',
						  "3"=>'three',
						  "4"=>'four');

print_r($_SESSION);
bmarklan
Forum Newbie
Posts: 4
Joined: Wed Jul 20, 2005 2:43 pm

Tried that... output still strange

Post by bmarklan »

Now the output is:

Code: Select all

PEWeb
ecefd02ec784d244041ffb465d200e9e

SESSION
array(4) {
  &#1111;0]=>
  int(1)
  &#1111;1]=>
  int(2)
  &#1111;2]=>
  int(3)
  &#1111;3]=>
  int(4)
}

SESSION&#1111;0]

Notice:  Undefined offset:  0 in /var/www/htdocs/byoung/cgi-bin/ac_Intermediate.php on line 751

Warning:  array_keys(): The first argument should be an array in /var/www/htdocs/byoung/cgi-bin/ac_Intermediate.php on line 751
NULL
and

Code: Select all

PEWeb
ecefd02ec784d244041ffb465d200e9e

SESSION
array(0) {
}

SESSION&#1111;0]
NULL
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

by a new window do you mean a popup window "JavaScript "?

I have had some troubles with them myself. When using a popup JavaScript window i prefer to use parameters

Code: Select all

<script language=&quote;javascript&quote;>
function popup_window(id,id2,id3){
#do whatever
}
</script>
<a href=&quote;#&quote; onclick=&quote;popup_window('1',2,'3);&quote;>Click</a>
If not u sure your not registering/clearing your Sessions again somewhere.
bmarklan
Forum Newbie
Posts: 4
Joined: Wed Jul 20, 2005 2:43 pm

Post by bmarklan »

Yes, the new window is a javascript window.

Code: Select all

<body onload=&quote;def();&quote;>
<script language=&quote;javascript&quote;>
function def() {
  window.open('look.php','look','scrollbars=yes');
}
bmarklan
Forum Newbie
Posts: 4
Joined: Wed Jul 20, 2005 2:43 pm

I got it!

Post by bmarklan »

I figured out the problem... $_SESSION doesn't like it when the key is just a number, so where I had $_SESSION[0], it just stopped writing to it. Make sure your keys start with a letter or an underscore. I changed it to $_SESSION["query0"] and it worked fine.

Thanks for the help,

BAM
mfindlay
Forum Newbie
Posts: 2
Joined: Fri Feb 10, 2006 2:50 pm

Also don't use numbers in session_name()

Post by mfindlay »

Just another comment related to the warning not to use just numbers in a session key (i.e. don't use $_SESSION[0]), also don't use just numbers when using session_name()

For example, don't use session_name("000") as you will not be able to read the values once written (at least not on PHP for windows, on Win2K).

When I prefxed the string with a letter (session_name("A000")), it worked fine.

Hope this helps anyone having similar problems.

feyd | resurrecting a thread = no-no
Locked