Simple PHP Session Tutorial

AID: 208
  • Status: Published

1890 points

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();
?>
                                    
1:
2:
3:
4:

Select allOpen 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 <?php code body. Anything put before the opening <?php line that can be printed to a webpage, including a simple space, will cause this error.

Fixing this problem is just as simple as the problem itself. Simply remove the entire line before the opening <?php block and resave your file. Upload the newly saved file and pull it up in your browser again. (NOTE: Sometimes a hard refresh may be needed...this is achieved by holding down your shift key while clicking the refresh button on your browser.)

And voila! Problem solved!

This was a test article submission and was meant to aid in the beta testing of this new feature.
Asked On
2008-11-20 at 19:59:52ID208
Tags

PHP

,

Session

Topic

PHP Scripting Language

Views
1236

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top PHP Experts

  1. Ray_Paseur

    1,354,218

    Genius

    3,734 points yesterday

    Profile
    Rank: Savant
  2. DaveBaldwin

    284,951

    Guru

    4,300 points yesterday

    Profile
    Rank: Genius
  3. StingRaY

    144,754

    Master

    4,000 points yesterday

    Profile
    Rank: Wizard
  4. jason1178

    143,172

    Master

    1,000 points yesterday

    Profile
    Rank: Genius
  5. bportlock

    123,643

    Master

    1,600 points yesterday

    Profile
    Rank: Genius
  6. ChrisStanyon

    111,336

    Master

    0 points yesterday

    Profile
    Rank: Sage
  7. Roads_Roads

    106,350

    Master

    0 points yesterday

    Profile
    Rank: Genius
  8. maeltar

    95,247

    Master

    0 points yesterday

    Profile
    Rank: Guru
  9. gr8gonzo

    95,168

    Master

    0 points yesterday

    Profile
    Rank: Sage
  10. smadeira

    82,088

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  11. Slick812

    77,062

    Master

    0 points yesterday

    Profile
    Rank: Sage
  12. johanntagle

    74,700

    Master

    2,000 points yesterday

    Profile
    Rank: Sage
  13. logudotcom

    67,088

    Master

    0 points yesterday

    Profile
    Rank: Genius
  14. COBOLdinosaur

    65,841

    Master

    0 points yesterday

    Profile
    Rank: Genius
  15. leakim971

    63,819

    Master

    0 points yesterday

    Profile
    Rank: Genius
  16. un1x86

    56,406

    Master

    0 points yesterday

    Profile
    Rank: Master
  17. Derokorian

    46,763

    10 points yesterday

    Profile
    Rank: Guru
  18. marqusG

    44,475

    10 points yesterday

    Profile
    Rank: Sage
  19. DrDamnit

    42,892

    0 points yesterday

    Profile
    Rank: Genius
  20. ahoffmann

    40,068

    0 points yesterday

    Profile
    Rank: Genius
  21. designatedinitializer

    37,800

    0 points yesterday

    Profile
    Rank: Master
  22. maestropsm

    37,678

    0 points yesterday

    Profile
    Rank: Guru
  23. TerryAtOpus

    34,800

    0 points yesterday

    Profile
    Rank: Genius
  24. xterm

    32,850

    0 points yesterday

    Profile
    Rank: Sage
  25. kaufmed

    31,808

    0 points yesterday

    Profile
    Rank: Genius

Hall Of Fame