I am writing an application that makes use of sessions to store the fact that visitors have logged in. I do not want to store any session IDs in a database but merely want to make use of the fact that a session variable has been set to denote the fact that a login attempt was successful. When I do this ordinarily I have no problems but when I do this from inside a function and seek to read the functions from inside another function, problems arise. First iterating through the $_SESSION superglobal variable shows me that though the sessions are registered, they are empty of value. Please can someone tell me how to register a session from inside a function, and still have that session available outside the function.
I am running the Application on php 5.0, windows XP pro sp2 and I have register_globals directive disabled.
Below is the code I am working with:
Listing 1: sess.php
<?
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?
function sess()
{
session_register('email');
session_register('product');
$email = "tori-wowo";
$product = "rice and beans";
print session_encode()."<br>";
}
sess();
?>
<a href='sess2.php'>go to</a>
</body>
</html>
Listing 2: sess2.php
<?
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?
function sess2($email, $product)
{
print $email."<br>";
print $product;
}
//sess2($email, $product);
print $email."<br>";
print $product;
?>
</body>
</html>