PHP Doku:: Registriert eine oder mehrere globale Variablen in der aktuellen Session - function.session-register.html

Verlauf / Chronik / History: (8) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSession-ErweiterungenSessionbehandlungSession-Funktionensession_register

Ein Service von Reinhard Neidl - Webprogrammierung.

Session-Funktionen

<<session_regenerate_id

session_save_path>>

session_register

(PHP 4, PHP 5)

session_registerRegistriert eine oder mehrere globale Variablen in der aktuellen Session

Beschreibung

bool session_register ( mixed $name [, mixed $... ] )

session_register() akzeptiert eine variable Anzahl von Argumenten, die jeweils entweder eine Zeichenkette sein können, die den Namen einer Variablen trägt, oder ein Array, das aus solchen Variablennamen oder anderen Arrays besteht. Für jeden Namen registriert session_register() die globale Variable mit diesem Namen in der aktuellen Session.

Sie können eine Session-Variable auch erzeugen, indem Sie das entsprechende Element des $_SESSION- oder (PHP <= 4.1.0) $HTTP_SESSION_VARS-Arrays setzen.

<?php
// Sie sollten session_register() nicht verwenden
$barney "A big purple dinosaur.";
session_register("barney");

// Ab PHP 4.1.0 ist die Verwendung von $_SESSION vorzuziehen
$_SESSION["zim"] = "An invader from another planet.";

// Die alte Methode war, $HTTP_SESSION_VARS zu verwenden
$HTTP_SESSION_VARS["spongebob"] = "He's got square pants.";
?>

Wenn session_start() nicht vor dieser Funktion aufgerufen wurde, erfolgt ein impliziter Aufruf von session_start() ohne Parameter. $_SESSION ahmt dieses Verhalten nicht nach und benötigt den Aufruf von session_start() bevor es verwendet wird.

Warnung

Diese Funktion ist seit PHP 5.3.0 DEPRECATED (veraltet). Sich auf diese Funktion zu verlassen ist in keiner Weise empfehlenswert.

Parameter-Liste

name

Eine Zeichenkette, die den Namen einer Variablen trägt, oder ein Array, das aus solchen Variablennamen oder anderen Arrays besteht.

...

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.

Anmerkungen

Achtung

Wenn Sie wollen, dass ihr Script unabhängig von der Einstellung von register_globals funktioniert, müssen Sie stattdessen das Array $_SESSION verwenden, weil $_SESSION-Einträge automatisch registriert werden. Wenn Sie in Ihrem Script session_register() verwenden, funktioniert es nicht in Umgebungen, in denen die PHP-Anweisung register_globals deaktiviert ist.

Hinweis: register_globals: Wichtiger Hinweis

Seit PHP 4.2.0 ist die Standardeinstellung für die Konfigurationsoption register_globals off. Die PHP-Community ermutigt alle, sich nicht auf diese Option zu verlassen und Alternativen wie superglobals zu verwenden.

Achtung

Diese Funktion registriert eine globale Variable. Wenn Sie eine Session-Variable innerhalb einer Funktion registrieren wollen, müssen Sie sicherstellen, dass Sie sie unter Verwendung des global-Schlüsselworts oder des $GLOBALS[]-Arrays global machen oder die nachstehend vermerkten speziellen Session-Arrays verwenden.

Achtung

Wenn Sie $_SESSION (oder $HTTP_SESSION_VARS) verwenden, sollten Sie nicht session_register(), session_is_registered() und session_unregister() verwenden.

Hinweis:

Gegenwärtig ist es nicht möglich, Ressourcen-Variablen in einer Session zu registrieren. Zum Beispiel können Sie nicht erwarten, dass die als Session-Variable gespeicherte Verbindungs-Kennung der zu einer Datenbank aufgebauten Verbindung bei der nächsten Wiederherstellung der Session noch gültig ist. PHP-Funktionen, die eine Ressource zurückgeben, können daran erkannt werden, dass sie einen Rückgabewert resource in ihrer Funktionsdefinition haben. Eine Liste der Funktionen, die Ressourcen zurückgeben befindet sich im Anhang Liste von PHP Ressourcen.

Bei Verwendung von $_SESSION (oder $HTTP_SESSION_VARS bei PHP 4.0.6 oder niedriger) weisen Sie $_SESSION einen Wert zu, also $_SESSION['var'] = 'ABC';

Siehe auch


10 BenutzerBeiträge:
- Beiträge aktualisieren...
yarco dot w at gmail dot com
1.10.2010 5:17
You *MUST* notice that

session_register($var)

*IS NOT*

$_SESSION[$var] = $$var;

it is

if (!isset($_SESSION[$var]))
  $_SESSION[$var] = $$var;

when migrating from old style code.
Ezion oudpapierdoos at gmail dot com
17.02.2010 3:38
Below is a fix that may be included in older code to make it work with PHP6.
When needed it recreates the functions
- session_register()
- session_is_registered()
- session_unregister()

The functions inside the function fix_session_register() are only available  after fix_session_register() has run.
Therefore in PHP<6 where there already is a session_register() nothing happens.

<?php
// Fix for removed Session functions
function fix_session_register(){
    function
session_register(){
       
$args = func_get_args();
        foreach (
$args as $key){
           
$_SESSION[$key]=$GLOBALS[$key];
        }
    }
    function
session_is_registered($key){
        return isset(
$_SESSION[$key]);
    }
    function
session_unregister($key){
        unset(
$_SESSION[$key]);
    }
}
if (!
function_exists('session_register')) fix_session_register();
?>


[EDIT BY danbrown AT php DOT net: Bugfix provided by "dr3w" on 02-APR-2010: "its [sic] function_exists with an S at the end".]
Scarab
13.11.2009 10:14
If you want to store an  object into the session, you have to check up that object can be serialized at all.
For example, if your object contains aggregated PDO object (which can't be serialized), you will get an error and no data would be stored.
rob at akrabat dot com
14.08.2009 11:18
if you remove session_register() calls and replace with $_SESSION assignments, make sure that it wasn't being used in place of session_start(). If it was, you'll need to add a call to session_start() too, before you assign to $_SESSION.
david dot demri at gmail dot com
26.05.2009 16:15
If you have an old code with a lot of call to the function session_register(), and you would like to make it compatible with PHP5 or PHP6, instead of rewriting all your code by replacing this function by $_SESSION['var']="val"; you could use the function set_session_vars(), that is used exactly the same way than session_register() (but there is no implicit call to session_start() ).

<?php
function set_session_vars() {
   
$nb_args=func_num_args();
   
$arg_list=func_get_args();
    for(
$i=0;$i<$nb_args;$i++) {
        global $
$arg_list[$i];
       
$_SESSION[$arg_list[$i]] = $$arg_list[$i];
    }
}
?>
kavih7 at yahoo dot com
20.05.2009 1:15
For those of you who use this function (session_register that is), even though the manual does specify that this function implicitly calls session_start(), I just wanted to reiterate that fact. It is also important to know that if you ever switch from session_register to using $_SESSION, make sure to call session_start before adding items to the $_SESSION variable, because unlike session_register, no implicit call to session_start is done.

Another reason I explain this is because I ran into a problem in which you can add items to the $_SESSION variable all you want, but if session_start is not called before adding them, they will not actually be saved to the session. Using the same code, though, and replacing the $_SESSION assignments with session_register without calling session_start WILL save that info to the session.

It would be nice to have PHP check for writes to the $_SESSION variable and complain with a warning if session_start hasn't been called.
guideng at unlv dot nevada dot edu
1.06.2006 22:10
Make sure you put session_start() at the beggining of your script.

My sessions kept unsetting and I finally figured out why.

On my script, session_start() has to be said and uses cookies to set the session.

But I was outputting html prior to calling session_start(), which prevented it from succeeding becouse it uses the header function to place the cookies.

Once html has been outputed, session_start() can't use the header function to set cookies, hence session_start() fails and no session can be started.
martijn at brothersinart dot net
11.04.2006 23:04
Please note that if you use a "|" sign in a variable name your entire session will be cleared, so the example below will clear out all the contents of your session.

<?php
session_start
();
$_SESSION["foo|bar"] = "foo";
?>

It took me quite some time finding out why my session data kept disappearing. According to this bugreport this behaviour is intended.
http://bugs.php.net/bug.php?id=33786
mikej
21.11.2004 10:40
I've noticed that if you try to assign a value to a session variable with a numeric name, the variable will not exist in future sessions.
For example, if you do something like:
session_start();
$_SESSION['14'] = "blah";
print_r($_SESSION);

It'll display:
Array ( [14] => "blah" )

But if on another page (with same session) you try
session_start();
print_r($_SESSION);

$_SESSION[14] will no longer exist.

Maybe everyone else already knows this, but I didn't realize it until messing around with a broken script for quite a while.
baldanders
12.11.2004 20:05
If you are using sessions and use session_register()  to register objects, these objects are serialized automatically at the end of each PHP page, and are unserialized automatically on each of the following pages. This basically means that these objects can show up on any of your pages once they become part of your session.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)