Notice: Unknown: Skipping Numeric Key 1 in Unknown on Line 0

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



Leave a reply



Submit