|
|
 |
session_register (PHP 4, PHP 5) session_register --
Register one or more global variables with the current session
Descriptionbool session_register ( mixed name [, mixed ...] )
session_register() accepts a variable number of
arguments, any of which can be either a string holding the name of a
variable or an array consisting of variable names or other arrays. For
each name, session_register() registers the global
variable with that name in the current session.
| Caution |
If you want your script to work regardless of register_globals,
you need to instead use the
$_SESSION array
as $_SESSION entries are automatically
registered. If your script uses
session_register(), it will not work in
environments where the PHP directive
register_globals
is disabled.
|
register_globals: important
note: Since PHP 4.2.0, the default value for the PHP directive
register_globals is
off. The PHP community encourages all to not rely on this
directive but instead use other means, such as the superglobals.
| Caution |
This registers a global variable. If you
want to register a session variable from within a function, you
need to make sure to make it global using the global
keyword or the $GLOBALS[] array, or use the
special session arrays as noted below.
|
This function returns TRUE when all of the variables are successfully
registered with the session.
If session_start() was not called before this function
is called, an implicit call to session_start() with no
parameters will be made. $_SESSION does not mimic
this behavior and requires session_start() before use.
You can also create a session variable by simply setting the
appropriate member of the $_SESSION
or $HTTP_SESSION_VARS (PHP < 4.1.0) array.
Note:
It is currently impossible to register resource variables in a
session. For example, you cannot create a connection to a
database and store the connection id as a session variable and
expect the connection to still be valid the next time the
session is restored. PHP functions that return a resource are
identified by having a return type of
resource in their function definition. A
list of functions that return resources are available in the
resource types appendix.
If $_SESSION (or
$HTTP_SESSION_VARS for PHP 4.0.6 or less) is
used, assign values to
$_SESSION. For example: $_SESSION['var'] = 'ABC';
See also session_is_registered(),
session_unregister(), and
$_SESSION.
User Contributed Notes
session_register
T
12-Apr-2005 08:24
make sure you include files as "filename.php" and not "http://www.yourdomain.com/filename.php" or sessions won't work - spent a day trying to figure out this problem.
Also make sure that $_SESSION['$name'] does not any $name variables that you send with POST OF GET - spent another day until i noticed this could be the cause of my problems.
mantas at systemnetwork dot net
19-Mar-2005 05:40
I have 3 files:
- The first is the form that contains various fields.
- Second is the php script that the form submits data to. This file then stores acquired values to the session.
- Finally the third file is the php script that accesses data from the session
So the first script:
<?
session_start();
$title = $_POST["title"];
$fname = $_POST["fname"];
$_SESSION["first"] = $fname;
?>
... All we do here is set values from HTTP POST request.
Now the second script:
<?
session_start();
echo $_SESSION["first"];
?>
... Here we try to print out the session variable that was set in the previous script.
This demonstrates the use of $_SESSION. There is no need to declare $_SESSION to be global since it is already global, thus can be accessed from anywhere.
Tested on PHP 4.
erik«at»phpcastle«dot»com
22-Feb-2005 01:38
Heya....
A way to check out the visitor has cookies enabled or a firewall or virusscanner blocks them...
[file = cookie_check.php]
<?php
session_start ();
if (isset ($_GET['cookie_check'])){
if (isset ($_SESSION['session_set'])){
echo "working...";
exit;
} else {
echo "failure...";
exit;
}
}
session_register ("session_set");
$_SESSION['session_set'] = true;
header ("Location: cookie_check.php?cookie_check=1");
?>
Hope this is usefull for someone :)
matt at atomboost dot com
01-Feb-2005 02:52
Put this in a functions file to check if a user is logged in simply by typing "session_check();"
function session_check() {
if(!session_is_registered('username')) {
// redirect to login page
exit();
}
}
Hayley Watson
25-Dec-2004 05:12
The posts of Tümmel and Foliot both make the same mistake about storing arrays in sessions when they use "$_SESSION['titel[$i]']". If the intention is to store an array in $_SESSION (or any other array for that matter), the appropriate syntax is $_SESSION['titel'][$i].
Foliot Gerald
07-Dec-2004 09:59
Note on "Tümmel atop at bluewin dot ch"
>However, when I call the $_SESSION-variable, it is necessary to >use the same counter variable name.
> This will not work:
> $p=3;
> echo '<input type =text value ='.$_SESSION['titel[$p]'].'>';
> That one is ok:
> $i=3;
> echo '<input type =text value ='.$_SESSION['titel[$i]'].'>';
The reason is that PHP register variable in the form
titel[$i]' in the session file
and not in the forme titel[1] in the session file
mikej
21-Nov-2004 03: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-Nov-2004 01: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.
Andrew Hoyt
26-Oct-2004 09:20
Yeah, I had an old PHP book that did things the old way, that was a headache. I found out that if one took the session variable:
session_start();
//do some type of login
login($username,$password);
$_SESSION["username"] = 1;
and made sure it was set equal to some value, you can check it on your pages with:
if($_SESSION["username"] != 1) {
print "you need to login";
//maybe some kind of site redirect here
exit;
}
//Page content goes here
Hoops, my mistake Tümmel atop at bluewin dot ch
24-Aug-2004 03:24
so sorry, it worked only one time and I still don't know why.(rotlf*):
<? ... while($f=mysql_fetch_row($result))
{$i++;
$_SESSION['foo1['.$i.']'] =$f[0];
$_SESSION['foo2('.$i.')']= $f[1];} ...?>
<? $s=2;
echo '<input type =text value ='.$_SESSION['foo1('.$s.')']>';
?>
Array as usual. Hope it will help.
Tümmel atop at bluewin dot ch
24-Aug-2004 02:41
If you use a lot of $_SESSION variables, you can put them into an array. For example like that:
while($f=mysql_fetch_row($result))
{$i++;
$_SESSION['foo1[$i]'] =$f[0];
$_SESSION['foo2[$i]']= $f[1];
}
However, when I call the $_SESSION-variable, it is necessary to use the same counter variable name.
This will not work:
$p=3;
echo '<input type =text value ='.$_SESSION['titel[$p]'].'>';
That one is ok:
$i=3;
echo '<input type =text value ='.$_SESSION['titel[$i]'].'>';
Hope it will help.
05-Feb-2004 12:19
Here is a dummy example of user logging using only session_register and session_is_registered functions
<************ login_page.php **********>
//put everything you need here to get user information
//html form etc..
<?php
session_start();
$user_is_logged = 1;
if(session_register('user_is_logged') == FALSE) {
die("cannot register variable !");
}
?>
<A href="i_am_in.php?<? echo SID?>">Press me</A>
<************************************>
<************i_am_in.php**********>
<?php
session_start();
if(session_is_registered('user_is_logged') == FALSE) {
echo "you are not registered !<br>";
} else {
if ($user_is_logged == 0) {
echo "you are registered but not valid<br>";
} else {
echo "you are registered and valid<br>";
}
}
session_unset();
session_destroy();
?>
<************************************>
dominik-ruess at gmx dot de
13-Dec-2003 04:58
Do not even think of using "global" together with "$_SESSION":
<?php
function test()
{
global $_SESSION;
$_SESSION["test"]=1;
}
function test()
{
$_SESSION["test"]=1;
}
?>
took me hours to recognize :/
gianni_t76 at yahoo dot it
05-Aug-2003 02:42
You don't need to serialize an object before adding it to a session var
example:
include("class.exmaple.php");
$obj = new exampel ();
$_SESSION['obj']=$obj;
and to resume the object:
include("class.example.php");
$obj = $_SESSION['obj']);
ice at thesurf dot no-ip dot com
02-Apr-2003 06:09
If you use objects it is alsways a good idear to make a var or array that hold your object-handler
exampel:
include("class.exmaple.php");
$obj = new exampel ();
$se_obj = serialize($obj);
$_SESSION['se_obj']=$se_obj;
when you then got on the next page and so a start_session()
and you don't habe includet the classdescription, your object will still not break.
Just do a
include("class.example.php");
$obj = unserialze($_SESSION['se_obj']);
and all is fine.
This is a preaty good trick if you do some includings in the next page an use the object in a file you include :)
| |