Reading session data

Once you have put your data safely away, it becomes immediately available in the $_SESSION superglobal array with the key of the variable name you gave it. Here is an example of setting data and reading it back out again:

<?php
    $_SESSION['foo'] = 'bar';
    print $_SESSION['foo'];
?>

Unlike cookies, session data is available as soon as it is set, which can be quite an advantage.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Removing session data >>

Previous chapter: Adding session data

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.