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.
session_register
(PHP 4, PHP 5)
session_register — Enregistre une variable globale dans une session
Description
session_register() enregistre toutes les variables de nom name dans la session courante. Le nombre de variables enregistrées est libre. Les noms peuvent être passés comme des chaînes, ou comme des tableaux contenant des chaînes ou des tableaux. Pour chaque nom, session_register() place la variable dans la session courante, pour la sauvegarde de fin de script.
Vous pouvez aussi créer une variable de session, simplement en ajoutant l'index approprié dans la variable $_SESSION ou $HTTP_SESSION_VARS (PHP < 4.1.0).
<?php
// L'utilisation de session_register() est déconseillée
$barney = "Un gros dinosaure violet.";
session_register("barney");
// L'utilisation de $_SESSION est encouragée depuis PHP 4.1.0
$_SESSION["zim"] = "Un envahisseur d'une autre planète.";
// L'ancienne méthode avec $HTTP_SESSION_VARS
$HTTP_SESSION_VARS["spongebob"] = "Il a un caleçon carré !";
?>
Si session_start() n'a pas été appelé avant cette fonction, un appel implicite à session_start() sans aucun paramètre sera fait. $_SESSION ne reproduit pas ce comportement et nécessite d'abord un appel à la fonction session_start().
Cette fonction est OBSOLETE depuis PHP 5.3.0 et a été SUPPRIMEE depuis PHP 6.0.0. Nous vous encourageons vivement à ne plus l'utiliser.
Liste de paramètres
- name
-
Le nom de la variable ou du tableau contenant les noms des variables ou d'autres tableaux.
- ...
-
Valeurs de retour
Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec.
Notes
Si vous voulez que votre script fonctionne indépendamment de la configuration de la directive register_globals, vous devez utiliser la variable $_SESSION. Tous les éléments de $_SESSION sont automatiquement enregistrés. Si votre script utilise session_register(), il ne fonctionnera pas dans les environnements où register_globals est désactivée.
Note: register_globals : note importante
Depuis PHP 4.2.0, la valeur par défaut de la directive de configuration PHP register_globals vaut off et est complètement retirée depuis PHP 6.0.0. La communauté PHP vous recommande de ne pas dépendre de cette directive, mais de trouver d'autres moyens pour accéder aux données, tels que les superglobals.
Cette fonction enregistre une variable globale. Si vous enregistrez une variable globale dans une fonction, vous devez vous assurer de la rendre globale, avec le mot clé global ou le tableau $GLOBALS[], ou bien utiliser un des tableaux de session ci-dessous.
Si vous utilisez $_SESSION (ou $HTTP_SESSION_VARS), n'utilisez pas session_register(), session_is_registered() et session_unregister().
Note: Il n'est actuellement pas possible d'enregistrer des ressources dans les sessions. Par exemple, vous ne pouvez pas créer de connexion à une base de données, et stocker la connexion dans une variable de session. Elle ne sera pas valide lors de la prochaine page. Les fonctions PHP qui retournent des ressources sont identifiées avec le type resource dans leurs définitions. Une liste de fonctions qui retournent des ressources sont disponible dans l'annexe types de ressources.
Si $_SESSION (ou $HTTP_SESSION_VARS pour les versions antérieures à PHP 4.0.6) est utilisé, assignez les variables à $_SESSION : i.e. $_SESSION['var'] = 'ABC';
Voir aussi
- session_is_registered() - Vérifie si une variable est enregistrée dans la session
- session_unregister() - Supprime une variable de la session
- $_SESSION
session_register
13-Nov-2009 09:14
14-Aug-2009 09: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.
26-May-2009 02: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];
}
}
?>
19-May-2009 11: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.
01-Jun-2006 08: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.
11-Apr-2006 09: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
21-Nov-2004 09: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.
12-Nov-2004 07: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.
