session_id();
?>
This results in output similar to the following:
Your session identification number is 967d992a949114ee9832f1c11c
Creating and Deleting Session Variables
Session variables are used to manage the data intended to travel with the user from
one page to the next. These days, however, the preferred method involves simply
setting and deleting these variable just like any other, except that you need to refer to
it in the context of the $_SESSION superglobal. For example, suppose you wanted to set
a session variable named username:
session_start();
$_SESSION['username'] = "jason";
printf("Your username is %s.", $_SESSION['username']);
?>
This returns the following:
Your username is jason.
To delete the variable, you can use the unset() function:
session_start();
$_SESSION['username'] = "jason";
printf("Your username is: %s
", $_SESSION['username']);
unset($_SESSION['username']);
printf("Username now set to: %s", $_SESSION['username']);
?>
456 CHAPTER 18 ?– SESSION HANDLERS
This returns:
Your username is: jason
Username now set to:
?– Caution You might encounter older learning resources and newsgroup discussions referring to the
functions session_register() and session_unregister(), which were once the recommended
way to create and destroy session variables, respectively. However, because these functions rely on a
configuration directive called register_globals, which was disabled by default as of PHP 4.
Pages:
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537