Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

A Simple Sessions Tutorial
Jun 27, 2006 @ 14:43:37

Developers just starting out with PHP have a pretty easy time getting the basics down, but there are a few things that can take a little time to get ones head around. I just looking around the PHP community, I've noticed a bit of a barrier when it comes to getting aquainted with sessions. So, to help overcome this bump in the road, here's a brief introduction to sessions - what they are and how they can help you.

Click here for the full article!

What are sessions?
Would like the simple answer or the long answer first? That's what I thought, short answer first. Basically, sessions are a storage method - there's no real mystery about them other than the special way that PHP handles them. You can read and write to them just like you can any other PHP variable, with one very handy difference. Sessions are special - they get to zip around from page to page, following the user.

Confused? I hope not, it's a pretty simple thing to get. Sessions are something built into PHP to allow data to be shared between pages - like login information or the path a user's taking through the site. By default, when a session is started up, a file is written to the temporary directory on the web server to contain the information for you. It's all automatic - you don't have to mess with any of the file functionality in PHP to work with it.

PHP tracks which file it's using for which user based on the session ID. This is a string of letters and numbers that are (ideally) unique to the visitor, making it easy for PHP to match the viewer and the file.

Why do I want to use them?
So, now you know what they are, but do you see the advantage to them? Why can't I just pass the data around on the URL string? Well, doing something like that is a choice (and is often combined with sessions), but gets to be a little bit of a problem when working with larger values. Sessions are also a bit more secure than just passing things around in a GET request (on the URL line). You wouldn't want to pass user information all around your site on the end of a URL, would you? Not only is that a security issue waiting to happen, but then it's also saved in the person's browser history. Bad news all around.

How do I use them?
And now we get to the big question (and hopefully some code, eh?) - how to use these uber-handy, simple, slick little bits of functionality PHP has to offer. Well, thankfully, the answer's pretty simple - and it all starts with one function:

[php] [/php]

Pretty simple, right? There has to be more to it, right? Wrong. Really, that's all it takes to start using sessions in your code. Keep in mind, though, that to be able to use your session on other pages, a call to session_start must be the first thing on every page. So, you have your session started - what are you going to do with it? Well, as I mentioned before, sessions are really just a handy place to store things. For example, if I started up my session and wanted to put a value of "enygma" into the "username" place, I'd do something like:

[php] [/php]

You'll notice two new things here - first is the format: $_SESSION[]. $_SESSION is one of the special variables that PHP handles internally, known as the superglobals. These variables (such as $_SESSION, $_GET, and $_POST) all have their places in PHP's functionality. They're also all arrays to make storing lots of data in them (or pulling from them) a much simpler process. So, using a simplified syntax to enter the value into the array, we push the value "enygma" into the $_SESSION array under the key of 'username'.

Get it? Got it? Good! On with the show!

Okay, so can you give me an example?
Now we get down to the fun part, applying this knowledge to a simple, small example - just passing data from one page to another. There's two files we'll create: file1.php and file2.php. File1.php will be the one setting the data and file2.php will just display what we set before. Now here's some code...

[php] go to file2'; ?> [/php]
[php] [/php]

It's a very simple example, but it gets the main idea across - bridging the gap between pages with the help of sessions. There's lots more to learn about sessions and some other handy ways to use them, but that'll have to be for another time. For now, play with the examples provided here, and enjoy the new-found freedom of being able to connect pages together without all of that messy URL line handling.

tagged: phpdev tutorial simple session introduction beginner phpdev tutorial simple session introduction beginner