1

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?

Jongware
  • 22,200
  • 8
  • 54
  • 100
John Black
  • 305
  • 2
  • 8
  • 19
  • 1
    Give us more to go on. It can be anything from a configuration problem to some (stupid) mistake on your part. Either way, we need more information. – Sverri M. Olsen Sep 07 '13 at 02:02
  • Adding "php-internals" tag because this question likely relates to `$_SESSION` internal index and serialization/deserialization handling, and it could be better answered there. – bob-the-destroyer Sep 07 '13 at 03:43
  • Regarding the question marked as duplicate: I can't see the relation between this question and [this](http://stackoverflow.com/questions/155920/php-session-data-not-being-saved) since this one is about a session variable that's set as a single number and no relation to `$_SESSION['views'] = $_SESSION['views']+ 1;` which increments a number. – Funk Forty Niner Sep 07 '13 at 14:33

3 Answers3

3

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Yeah, I discovered that before you but whatever, here you have. – John Black Sep 07 '13 at 02:06
  • 1
    @JohnBlack If you feel that this answered your question, accept it as being correct, otherwise it will remain in the "unanswered" category. – Funk Forty Niner Sep 07 '13 at 02:27
  • 1
    Yeah, give a winner an accept (if answer is true), we are browsing this question(s) for nothing this way....! – Xfile Sep 07 '13 at 02:42
  • @Xfile The OP has the choice to accept it or not. However I did fully test my answer and found reference to it on the PHP.net website. Cheers – Funk Forty Niner Sep 07 '13 at 02:44
  • 1
    To add regarding array keys, from http://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. Please update the PHP manual page itself to include this note if you can. – bob-the-destroyer Sep 07 '13 at 02:44
  • @bob-the-destroyer Good point, I will do that Bob, thanks for the added info. I will put your name as credit. – Funk Forty Niner Sep 07 '13 at 02:45
  • @Fred -ii-: Sure if you want though no credit is due to me. "Steve Clay" on the PHP manual comments and you yourself already pointed this out. I'm just saying someone should put that in the manual rather than just leaving that fact in the comments. – bob-the-destroyer Sep 07 '13 at 02:50
  • @bob-the-destroyer Those important pieces of information are "often buried" as you pointed out. I often have to spend hours trying to find pertinent information on many different technical aspects. I'm glad you made that comment, it's indeed a valuable piece of information. – Funk Forty Niner Sep 07 '13 at 02:54
  • @Fred -ii-: sorry, I meant updating the [PHP manual page](http://php.net/manual/en/reserved.variables.session.php) itself, not here on SE. Anyone can edit PHP manual pages. Click the "edit" link on any PHP manual page. – bob-the-destroyer Sep 07 '13 at 02:54
  • @bob-the-destroyer Oh, I never did an edit on PHP.net, I guess there's a first time for everything lol – Funk Forty Niner Sep 07 '13 at 02:56
  • @bob-the-destroyer Well it's done. Now to sit & wait to find out if it will be accepted. – Funk Forty Niner Sep 07 '13 at 03:12
  • @JohnBlack Not a problem. Am glad that your question was resolved. SO is about finding solutions, cheers :) – Funk Forty Niner Sep 07 '13 at 15:34
0

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

blakroku
  • 531
  • 5
  • 12
0

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

Vineet1982
  • 7,730
  • 4
  • 32
  • 67