Notice: Unknown: Skipping numeric key 1 in Unknown on line 0
The PHP session storage mechanism was originally built around "registering" variables, so the keys in $_SESSION
must be names that could be treated as variables in their own right.
This means that $_SESSION[42]
is invalid, because $42
wouldn't be a valid variable name, and since $foo[42]
and $foo['42']
refer to the same thing, $_SESSION['42']
is invalid as well.
The solution is either to use a prefix on your session variables (e.g. $_SESSION['row_count_' . $x] = $row['Count'];
) or make them into an array, which you can then loop over etc later (e.g. $_SESSION['row_counts'] = array(); ... $_SESSION['row_counts'][$x] = $row['Count'];
)
Note: this limitation is actually part of the "serialization handler" used when writing the session to disk, which is why the errors have no context (they're fired while PHP is shutting down). In current versions of PHP there is a setting of session.serialize_handler which doesn't have this limitation:
php_serialize is available from PHP 5.5.4. php_serialize uses plain serialize/unserialize function internally and does not have limitations that php and php_binary have. Older serialize handlers cannot store numeric index nor string index contains special characters (| and !) in $_SESSION. Use php_serialize to avoid numeric index or special character errors at script shutdown.
Error in php (Skipping numeric key 0 in Unknown on line 0)
The PHP session storage mechanism was originally built around "registering" variables, so the keys in $_SESSION
must be names that could be treated as variables in their own right.
This means that $_SESSION[]
or $_SESSION[22]
is invalid, because it wouldn't be a valid variable name, $_SESSION[]
is invalid as well.
The solution is either to use a prefix on your session variables (e.g. $_SESSION['email'] = $row['email'];)
a stupid 'important' question about php $_SESSION array
Superglobals like $_SESSION
are not normal arrays. You should store an array inside $_SESSION
, like so:
file 1: $_SESSION['foo'][] = 'Hi!';
file 2: $_SESSION['foo'][] = 'there';
Session variables seem not to be saved
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.
http://php.net/manual/en/session.examples.basic.php
can a php $_SESSION variable have numeric id thus : $_SESSION['1234’]
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.
Carrying a variable via session to another file
The only variables that get carried between scripts are $_SESSION['xxx']
. Ordinary variables like $profile_user
don't persist. The assignment
$_SESSION['viewer'] = $profile_user;
doesn't make $profile_user
get copied, it copies its value into $_SESSION
, and you have to pull it out of there in the other script. So script 2 should start with:
session_start();
$profile_user = $_SESSION['viewer'];
Session Variables Disappearing in PHP
The keys of $_SESSION
adhere to the same rules as valid variable names in PHP. They must begin with a letter or an underscore, followed by any number of letters, numbers or underscores. Therefore, the output of time()
can’t serve as a key unless it’s prefixed by at least one letter or underscore.
Add the line error_reporting(E_ALL);
to your code and PHP will throw a notice like the following:
Notice: Unknown: Skipping numeric key 1396747512 in Unknown on line 0
dynamically name session isset how to use?
i found problem we can not use numeric index for $_SESSION
but we can use number in $_SESSION by convert number to roman numerals
first page url
https://example.com/test.php?id=1548393
first page code
<?php
session_start();
$roman_id = romanic_number($_GET['id']);
$_SESSION[$roman_id] = "mysecretstringline";
function romanic_number($integer, $upcase = true)
{
$table = array('M'=>1000, 'CM'=>900, 'D'=>500, 'CD'=>400, 'C'=>100, 'XC'=>90, 'L'=>50, 'XL'=>40, 'X'=>10, 'IX'=>9, 'V'=>5, 'IV'=>4, 'I'=>1);
$return = '';
while($integer > 0)
{
foreach($table as $rom=>$arb)
{
if($integer >= $arb)
{
$integer -= $arb;
$return .= $rom;
break;
}
}
}
return $return;
}
?>
second page url
https://example.com/test2.php?id=1548393
second page code
<?php
session_start();
$roman_id = romanic_number($_GET['id']);
if(isset($_SESSION[$roman_id])){
echo "working";
}else{
echo "not working";
}
function romanic_number($integer, $upcase = true)
{
$table = array('M'=>1000, 'CM'=>900, 'D'=>500, 'CD'=>400, 'C'=>100, 'XC'=>90, 'L'=>50, 'XL'=>40, 'X'=>10, 'IX'=>9, 'V'=>5, 'IV'=>4, 'I'=>1);
$return = '';
while($integer > 0)
{
foreach($table as $rom=>$arb)
{
if($integer >= $arb)
{
$integer -= $arb;
$return .= $rom;
break;
}
}
}
return $return;
}
?>
output
working
thanks @gre_gor and @Katie
Related Topics
Fastest Way to Add Prefix to Array Keys
Symfony2 - Assetic - CSS Font Icons
Custom Order Status Background Button Color in Woocommerce 3.3 Admin Order List
Why Do Changes to Some PHP Files Take So Long to Show on the Live Site
How to Check Which PHP Extensions Have Been Enabled/Disabled in Ubuntu Linux 12.04 Lts
How to Send Email from Localhost Using PHP on Linux
How to Enable MySQLi Support for PHP's Cli
PHP Syntax for Arrays: Different Behaviour Between PHP Versions
Swift Httppost Data Not Inserting to MySQL Database
Mysqli Prepared Statements with in Operator
How to Use PHP Reserved Names for My Functions and Classes
Alter Table in Magento Setup Script Without Using SQL
Calling a Particular PHP Function on Form Submit
Convert Utc Dates to Local Time in PHP
How to Fetch Associative Array Grouped by the Values of a Specified Column with Pdo
String Contains Any Items in an Array (Case Insensitive)
Pdostatement (Mysql): Inserting Value 0 into a Bit(1) Field Results in 1 Written in Table