PHP 8.4.0 RC2 available for testing

$_SESSION

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

$_SESSIONセッション変数

説明

現在のスクリプトで使用できるセッション変数を含む連想配列です。 セッション変数の使用法についての詳細は、 セッション関数 のドキュメントを参照ください。

注意

注意:

これは 'スーパーグローバル' あるいは自動グローバル変数と呼ばれるものです。 スクリプト全体を通してすべてのスコープで使用することができます。 関数やメソッドの内部で使用する場合にも global $variable; とする必要はありません。

参考

  • session_start() - 新しいセッションを開始、あるいは既存のセッションを再開する

add a note

User Contributed Notes 3 notes

up
104
Tugrul
9 years ago
Creating New Session
==========================
<?php
session_start
();
/*session is started if you don't write this line can't use $_Session global variable*/
$_SESSION["newsession"]=$value;
?>
Getting Session
==========================
<?php
session_start
();
/*session is started if you don't write this line can't use $_Session global variable*/
$_SESSION["newsession"]=$value;
/*session created*/
echo $_SESSION["newsession"];
/*session was getting*/
?>
Updating Session
==========================
<?php
session_start
();
/*session is started if you don't write this line can't use $_Session global variable*/
$_SESSION["newsession"]=$value;
/*it is my new session*/
$_SESSION["newsession"]=$updatedvalue;
/*session updated*/
?>
Deleting Session
==========================
<?php
session_start
();
/*session is started if you don't write this line can't use $_Session global variable*/
$_SESSION["newsession"]=$value;
unset(
$_SESSION["newsession"]);
/*session deleted. if you try using this you've got an error*/
?>
up
11
bohwaz
16 years ago
Please note that if you have register_globals to On, global variables associated to $_SESSION variables are references, so this may lead to some weird situations.

<?php

session_start
();

$_SESSION['test'] = 42;
$test = 43;
echo
$_SESSION['test'];

?>

Load the page, OK it displays 42, reload the page... it displays 43.

The solution is to do this after each time you do a session_start() :

<?php

if (ini_get('register_globals'))
{
foreach (
$_SESSION as $key=>$value)
{
if (isset(
$GLOBALS[$key]))
unset(
$GLOBALS[$key]);
}
}

?>
up
-1
opajaap at opajaap dot nl
11 years ago
Be carefull with $_SESSION array elements when you have the same name as a normal global.

The following example leads to unpredictable behaviour of the $wppa array elements, some are updated by normal code, some not, it is totally unpredictable what happens.

<?php
global $wppa;
$wppa = array( 'elm1' => 'value1', 'elm2' => 'value2', ....etc...);

if ( !
session_id() ) @ session_start();
if ( ! isset(
$_SESSION['wppa']) $_SESSION['wppa'] = array();

if ( ! isset(
$_SESSION['wppa']['album']) ) $_SESSION['wppa']['album'] = array();
$_SESSION['wppa']['album'][1234] = 1;

$wppa['elm1'] = 'newvalue1';

print_r($_SESSION);
?>
This will most likely display Array ( [wppa] => Array ( [album] => Array ( [1234] => 1 ) [elm1] => 'newvalue1' [elm2] => 'value2' ... etc ...
But setting $wppa['elm1'] to another value or referring to it gives unpredictable results, maybe 'value1', or 'newvalue1'.

The most strange behaviour is that not all elements of $wppa[xx] show up as $_SESSION['wppa'][xx].
To Top