PHP Exercise 6

Sessions

 

  1. Sessions store information that are unique to a users visit. Every time a person visits a website and the php function session_start() is called, a unique number is assigned to them.
  2. To see this unique number try the following. I called my file session_id.php.

 

<?php

session_start();

echo "You session ID #: ";

// The session_id() function prints your session ID to the

// screen. The print() function is similar to the echo()

// function

print(session_id());

?>

 

  1. Next we will need to understand how to pass variables using sessions. This is useful when keep track of items in a shopping cart, user information, or any other data that you want destroyed when a user closes their browser or logs out.
  2. To pass variables between pages, we will use the function session_register() to name our session variables. Note that in order to keep the variable and session alive, any page that a user will browse to on your site MUST contain session_start() and register all of your session variables.
  3. Here we will have a session variable named users_name.  Name this file session_variable_1.php. Examine the following code:

 

<?php

// start the session

session_start();

session_register(users_name);

 

// The trick we use to prevent us from making two

// files (a .html and .php file)

if (isset($_GET[name]))

{

$_SESSION[users_name] = $_GET[name];

echo "Hello $_SESSION[users_name]<br>";

}

else // Print out the input form

{

echo "<form action=\"$_SERVER[PHP_SELF]\" method=\"GET\">

<input type=\"text\" name=\"name\">

<br>

<input type=\"submit\" value=\"Submit\">

</form>";

}

// Print some information about the variable

// $_SESSION

echo "- - - - - - - - - - - - - - <br>";

echo "Sessions: <pre>";

print_r($_SESSION);

echo "</pre>";

echo "Your session ID: ";

print_r(session_id());

?>

 

  1. Now if we browse to another page, let's say session_variable_2.php, we can view the session variable users_name by using the following code. Notice that nothing changed here except Hello Again in the echo statement, and I did away with the IF,ELSE clause.

 

<?php

// Start the session and register the variables

session_start();

session_register(users_name);

echo "Hello, Again $_SESSION[users_name]";

echo "<br>";

// Print some information about the variable

// $_SESSION

echo "- - - - - - - - - - - - - - <br>";

echo "Sessions: <pre>";

print_r($_SESSION);

echo "</pre>";

echo "Your session ID: ";

print_r(session_id());

?>

 

  1. You can use sessions and session variables to pass pretty much any information. Session variables can also be arrays. The array is accessed by $_SESSION[variable_name][index], where index is an integer beginning with 0.
  2. Now if you want to destroy a session, that is, if you want to get rid of all session variables in one easy step, there is a special function in php called session_destroy(). Note that you have to call session_start() before you call session_destroy() in order to tell the browser which session to destroy. Make a file called session_destroy.php and insert the following:

 

<?php

session_start();

session_destroy();

echo "Your session has been destoryed<br>";

echo "Return to <a href=\"session_variable_2.php\">session_variable_2.php</a>";

?>

 

 

 

 

On Your Own Exercise 6

 

  1. Create a session variable that is an array containing 4 (indexed from 0 to 3) names which are input from a user in 4 text boxes. Display them on the screen after the user has submitted the input.
  2. Then create another .php file that reads the 4 names from the session variable and prints them out to a screen in a different format. A sample screen is shown below.
  3. You MUST comment your code to receive full credit!
  4. Also, copy the following code and put it at the very end of your .php file:

 

// Print some information about the variable

// $_SESSION

echo "<br>- - - - - - - - - - - - - - <br>";

echo "Sessions: <pre>";

print_r($_SESSION);

echo "</pre>";

echo "Your session ID: ";

print_r(session_id());

 

 

On Your Own Exercise 6 – Sample Answer