Stupid "important" question about php $ _SESSION array

I have 2 files that put something in the $ _SESSION array. file1.php

<?php
session_start();

$_SESSION[] = 'Hi';

echo '<pre>';
print_r($_SESSION);
echo '</pre>';

What prints

Array
(
    [0] => Hi
)

And file2.php, which is similar to file1

<?php
session_start();

$_SESSION[] = 'There!';

echo '<pre>';
print_r($_SESSION);
echo '</pre>';

Suppose you first go to file1, and then go to file2. Printing $ _SESSION in file2 should output

Array
(
    [0] => Hi
    [1] => There!
)

I am wrong?

I should mention that I am getting a notification: Unknown, skipping the numeric key 0 in Unknown on line 0. And register_globals in my php.ini is Off.

As I see in the comments for one of you, file2 prints an array of two elements, and for some (for example, me) the "hello" elements are lost. This happens, but not for Marc B, only if we use the number as an index to the session array, not a string.

Marc B , . php.ini ? , ?

+3
5

$_SESSION . $_SESSION, :

1: $_SESSION['foo'][] = 'Hi!';

2: $_SESSION['foo'][] = 'there';

+5

$_SESSION .

0

, . , 1, "" , "" , "" ...

0

- ,

$_SESSION[]='Hi' instead of $_SESSION["Greet"]='Hi'?

. file1.php, :

Array
(
    [0] => hi
)

: 0 0 file2.php, :

Array
(
    [0] => there!
)

with the same notice. just answer your question, you're wrong :). If you add indexes ("greet" and "meet" respectively) to the session variable, this will be the output on page 1:

Array
(
   [greet]=> hi      
)

and when you go to the2.php file, you would:

Array
(
  [greet] => hi
  [meet] => there!
)
0
source

file1:

<?php
session_start();

$_SESSION['0'] = 'Hi';

echo '<pre>';
print_r($_SESSION);
echo '</pre>';

That prints 
Array
(
    [0] => Hi
)

And file2.php, similar to file1, but a different session index

<?php
session_start();

$_SESSION['1'] = 'There!';

echo '<pre>';
print_r($_SESSION);
echo '</pre>';

Now this prints 
Array
(
    [0] => Hi
    [1] => There!
)
-1
source

Source: https://habr.com/ru/post/1788305/


All Articles