How to carry data from one page to another using sessions

How do you carry data from one page to another using sessions?

You can use standard PHP features to access and use session data. In your Drupal Web site, you can add code direct to a Web page or write an add on module to process data on any page. The same code works in any page in every Web site except a few that do not have PHP switched on by default.

Yes, there are Web sites that do not have PHP installed or have PHP switched off by default, just as there are people who have electricity but not a dishwasher.

This page shows the code to save data in a session, display the saved data, and a form to test the code. You can use this code in a Drupal page or in a Web page. You can apply the code in Drupal add on modules and scripts behind Web pages.

Drupal Input format

Create a test page in Drupal or a regular HTML page. Make sure the page works. Set the Drupal page Input format to PHP. The default is Filtered HTML and that does not let you enter forms or code. Full HTML lets you add any HTML including forms but not PHP.

By default the Drupal input format of PHP does not automatically create paragraph breaks. Add the paragraph HTML elements around your text in the test page. Add lots of descriptive documentation to your test page so that you can come back to the page any time in the future to test variations of your code.

For all other Web pages, make sure they accept PHP. The easiest thing is to tell your Web server to pass all HTML pages through PHP.

The form

Start by adding the following form to your test Web page.

<form action="" method="post">
<input name="example" type="text" /><br />
<input name="submit" type="submit" value="Save the text" />
</form>

The form is used later on this page with some PHP code added to process the example text. Note that the form element does not have an id or a name because we do not reference the form, only the text field. The action is empty because it defaults to returning to the same page.

The code for the form

Add PHP code to process the form.

Before the form, add the following PHP code to create an error message if you forget to enter example text.

<?php
$text = '';
$error = '';
if(isset($_POST['example']))
   {
   if(empty($_POST['example']))
      {
      $error = '<strong style="color: red;">' . t('You did not enter text!') . '</strong>';
      }
   else
      {
      $text = htmlentities($_POST['example']);
      }
   }
?>

In the form, change one line to display the error message.

<input name="example" type="text" value="<?php print $text; ?>" /> <?php print $error; ?><br />

After the form, add the following PHP code to display the example test from the form.

<?php
if(!empty($text))
  {
  ?><p>You entered: <span style="color: yellow;"><?php print $text; ?></span></p>
<?php
  }
?>

Test the form

Select Save the text to test the form and code with no text.

Type in some text then select Save the text to test the form and code with text. There is a htmlentities() around the text to stop you entering malicious HTML.


What is in the session?

The session contains a set of variables placed in array $_SESSION by PHP. We will look for variable $_SESSION['how_to_carry_data_from_one_page_to_another_using_sessions']. Note the long name in an attempt to make it unique so that it does not crash into another session variable.

Drupal 6 saves arrays in the session and we might want to save something other than $text so we will save $text as an array within $_SESSION['how_to_carry_data_from_one_page_to_another_using_sessions'].

The session variable is not yet set.

How did we display the data from the previous session? Insert the following code to display the existing content of our session variable.

<?php
if(!isset($_SESSION['how_to_carry_data_from_one_page_to_another_using_sessions']))
  {
  ?><p style="color: red;">The session variable is not yet set.</p>
<?php
  }
elseif(empty($_SESSION['how_to_carry_data_from_one_page_to_another_using_sessions']))
  {
  ?><p style="color: red;">You saved nothing. Nada. Zero!</p>
<?php
  }
elseif(empty($_SESSION['how_to_carry_data_from_one_page_to_another_using_sessions']['text']))
  {
  ?><p style="color: red;">Session text is empty. Learn to type!</p>
<?php
  }
else
  {
  ?><p>You previously saved example text: <span style="color: yellow;"><?php print $_SESSION['how_to_carry_data_from_one_page_to_another_using_sessions']['text']; ?></span></p>
<?php
  }
?>

Save the data to the session

How did we save the data to the session? Insert the following code to save $text to the session variable. The code removes the session variable on first entry to ensure we do not have unused variables filling up the session.

<?php
if(!isset($_POST['example']))
   {
   if(isset($_SESSION['how_to_carry_data_from_one_page_to_another_using_sessions']))
      {
      ?><p style="color: red;">Removing unused session variable.</p>
<?php
      unset($_SESSION['how_to_carry_data_from_one_page_to_another_using_sessions']);
      }
   }
elseif(empty($text))
  {
  ?><p>Saving <span style="color: red;">nothing!</span></p>
<?php
  }
else
  {
  $_SESSION['how_to_carry_data_from_one_page_to_another_using_sessions']['text'] = $text;
  ?><p>Saving example text: <span style="color: yellow;"><?php print $_SESSION['how_to_carry_data_from_one_page_to_another_using_sessions']['text']; ?></span></p>
<?php
  }
?>

Additional notes

$_SESSION is always $_SESSION, not $_session or $SESSION or $_Session. PHP is case sensitive in some places and case insensitive in others, the same as a URL and a lot of other parts of the Internet. The _ after the The $ indicates a name reserved by PHP. You can use the underscore anywhere except straight after the dollar.

Use the first name, in this case ['how_to_carry_data_from_one_page_to_another_using_sessions'], to distinguish all your variables within a project or application or add on module. If your add on module is named example, use $_SESSION['example'] as the top level variable.

If the user has cookies turned off, PHP will attempt to use an alternative to cookies. When cookies and the alternative fail, you will not have sessions. You might want to consider an alternative to sessions.

Sessions expire. If you want your data to last longer than a session, put the data in your database.

Sessions are good for storing small amounts of data. When a session is used to store huge amounts of data, things will slow down and may break depending on your session configuration. Session data is usually stored in a database and the database may have limitations or settings restricting the amount of data that is efficient and practical.

Drupal creates sessions very early in the Drupal bootstrap process. You can save almost anything anytime. The session start has to occur after the HTTP headers are created and HTTP headers are used for redirecting to another page. If you want to save something then redirect, you cannot save in the session because that will stop the redirect.

Conclusion

Sessions are a handy way to store temporary data from page to page. Use a unique top level name. Clean up your session variables after use. Do not store your multi gigabyte videos in the session.