Simple PHP Session Tutorial

Richard DavisSenior Web Developer
CERTIFIED EXPERT
Published:
Updated:
Sessions can be extremely useful when developing content that requires the ability to be able to carry over from page to page without the use of posting form data. An application that commonly makes use of PHP sessions are authentication and registration systems.

Here, in this tutorial, I will attempt to layout the basics of a simple session with examples and descriptions that will support the examples.

Probably the greatest stumbling block that will almost invariably get in the way of every new PHP developer is the issue of creating a new session, but with the error Cannot send session cache limiter - headers already sent

This error is almost always due to one very simple mistake and it's solution is just as simple to resolve. So, let's start by showing the code snippet that will cause this error followed by it's simple solution so that you can get rolling in creating working and useful sessions in PHP.
 

1. Create a test.php file

Using your favorite editor, create a file called test.php and save it to a local development directory (or folder for you windows users)

2. Add PHP code

Now you are going to want to add the following code to this file;

'Add a space here or any character'
                      <?php
                        session_start();
                      ?>

Open in new window


3. Save & Upload your work

Now it's just a matter of saving your file and uploading it to your webserver. I'll assume that you have a good understanding of that side of things.Once you have completed the above mentioned steps, now go ahead and call up test.php from your webserver in your browser and you should see an error report like the one mentioned above.

As you can see from the code snippet. There is a line of non-PHP page content above the opening

Fixing this problem is just as simple as the problem itself. Simply remove the entire line before the opening

And voila! Problem solved!

This was a test article submission and was meant to aid in the beta testing of this new feature.
2
5,707 Views
Richard DavisSenior Web Developer
CERTIFIED EXPERT

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.