I've been driving myself nuts with this problem.
I'm creating a session id dynamically in order to retain the page state on refresh.
If a page element is clicked, I take the id of the element and pass it to my server side script which creates the session variable:
$_SESSION[$id] = $id;
Bizarrely, this was working only some of the time, I narrowed it down to the fact that some elements have a purely numeric id and others do not:
if (is_numeric($id))
{
$_SESSION[$id] = $id;
$_SESSION['test'] = $id;
}else{
$_SESSION[$id] = $id;
};
In the example above only non-numeric session IDs were visible. For example I could echo $_SESSION['test'];
with no issue at all.
Any ideas?
Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser. So; Session variables hold information about one single user, and are available to all pages in one application.
As @Thariama said, there's no limit on the number of variables; also, there's no limit on the amount of data you can store in a session (I've seen sessions tens of MB in size).
The session id is a random value generated when a session is started. The session id is stored as a cookie in the browser such that on subsequent visits the data stored in the session can be loaded and reused. This issue is about the session id (cookie value) and not about the session name (cookie name).
Developers should consider a session ID length of 32 characters or more. At least 26 characters are required when session.
From the manual:
The keys in the $_SESSION associative array are subject to the same limitations as regular variable names in PHP, i.e. they cannot start with a number and must start with a letter or underscore. For more details see the section on variables in this manual.
Using purely numeric keys in a session will not work. If it is numeric you can try preceding it with an underscore.
EDIT: As of PHP 5.5.9 in October 2015, this appears to still be true despite the manual reference no longer appearing.
Test code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
$_SESSION['a123'] = 'a123';
$_SESSION['123'] = '123str';
$_SESSION[455] = '455int';
$_SESSION['_123'] = '_123';
Yields:
Notice: Unknown: Skipping numeric key 123 in Unknown on line 0
Notice: Unknown: Skipping numeric key 455 in Unknown on line 0
Then a var_dump($_SESSION);
shows only:
array(2) {
["a123"]=>
string(4) "a123"
["_123"]=>
string(4) "_123"
}
This actually appears to happen when the session data gets serialized at the end of the request here. Apparently the session engine itself prevents the numeric session keys from being saved to the session.
Top level keys in the $_SESSION
can't be numeric, but keys on the deeper level can.
Eg.
$_SESSION['ids'][13] = $foo;
$_SESSION['ids'][666] = $bar;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With