note 39575 added to ref.session

From: Date: Mon, 02 Feb 2004 23:59:51 +0000
Subject: note 39575 added to ref.session
Groups: php.notes 
Request: Send a blank email to php-notes+get-64572@lists.php.net to get a copy of this message
After browser output, nothing you assign to $_SESSION will be saved into $_SESSION array. So be careful!! This wont work <? session_start(); echo "Hi"; $_SESSION[save_me] = "Hola a todos"; ?> After loading this page $_SESSION[save_me] will remain empty. This will work <? session_start(); $_SESSION[save_me] = "Hola a todos"; echo "Hi"; ?> Now $_SESSION[save_me] contains "Hola a todos" There are some cases where is necessary to save values into current session after server output. In this case you can use following function: <? function session_save(){ global $_SESSION; $current_session = $_SESSION; session_destroy(); session_start(); $_SESSION = $current_session; } session_start(); echo "Hi"; $_SESSION[save_me] = "Hola a todos"; //So now is possible to access $_SESSION[save_me] after server output if we run session save session_save(); ?> This time $_SESSION[save_me] will also contain "Hola a todos" I have tested this under Linux and PHP 4.3.4 ---- Manual Page -- http://www.php.net/manual/en/ref.session.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39575 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39575&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39575&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#64572) next »