Domanda

I have both pages, but the $_SESSION["1"] is not received in the 2nd one.

UPDATE: Turns out, that if I type a number in the session, it won't work. I typed a normal string and worked.. I never saw this happening

1st

<?php
session_start();
$_SESSION["1"] = "LOGGED";
?>

2nd

<?php
session_start();

echo $_SESSION["1"];
?>

Why isn't this working?

È stato utile?

Soluzione

Session variables with a single number will not work, however 1a will work, as will a1 and even a just single "letter" a will also work.

1st

<?php
session_start();
$_SESSION["a1"] = "LOGGED";
?>

2nd

<?php
session_start();
echo $_SESSION["a1"];
?>

Example from PHP.net manual on Session variables

<?php
$_SESSION[1][1] = 'cake'; // fails

$_SESSION['v1'][2] = 'cake'; // works
?>

Source: http://php.net/manual/en/language.types.array.php


EDIT:

  • As per bob-the-destroyer's comment:

To add regarding array keys, from php.net/manual/en/language.types.array.php, "Strings containing valid integers will be cast to the integer type". The manual on $_SESSION says "An associative array". So an associative array is expected literally...? It does no one any good if this bit of important info about accessing and storing session data remains buried in manual comments.

Altri suggerimenti

You can assign your $_SESSION's to names that define the $_SESSION's purpose eg. $_SESSION['logged'] = logged; Don't forget to start your $_SESSIONS's with letters and make them more descriptive. This makes you code more readable and easy to understand

The reason of the behavior is C variable declarations. As variables names can't start with numerical digit.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top