Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session Variables

Status
Not open for further replies.

rvr1

Programmer
Feb 6, 2004
16
NL
Hi,

I am having problems with session variables. Initially I set the variables using:

session_register('unittitle','unitcode','unitcodepk');

These variables can then be used in different pages by refering to them as $unittitle etc...

Once the user has entered all info, I save the values of these variables to a database. However, when I come to unset these variables, and then move to another page, the variables still hold a value.

I unset the variables using:

unset($unittitle,$unitcode,$unitcodepk);

to move between pages, I use the code:

header("Location: lecturer_page.php");
exit();

Can anyone tell me how I can unset the values completely please?
 
If you're done with a session variable session_unregister() unregisters variables from the session.

In general, though, I recommend that you not use session_register() or session_unregister(). Instead I recommend directly referencing the $_SESSION (or with PHP versions older than 4.1.0, $HTTP_SESSION_VARS):
Code:
<?php
session_start();

//setting a session variable "foo"
$_SESSION['foo'] = 3;


//unsetting the session variable "foo"
unset ($_SESSION['foo']);
?>

Use of session_register() then expecting the session variables to be available in code requires that the PHP runtime configuration directive "register_globals" be set to "on". The PHP online manual discusses security problems with having "register_globals" set to "on".

The security problem aside, writing your code as if "register_globals" were "off" makes your code more portable. Write your code expecting it to be "on" and move to an installation where it's "off", and your code will barf. Write your code expecting "register_globals" to be "off", and it doesn't matter what the setting is if you move your code to another machine.

Also, with a complex script you may in six months forget how the variable "foo" got its value. But a reference to $_SESSION['foo'] is pretty obvious.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top