How To

How to Set a Session Variable in PHP

Member
By Mike
User-Submitted Article
(1 Ratings)

PHP is a very popular web based CGI programming language that is used to make websites more interactive. A PHP session is where the website developer can store things locally on the web visitors machine while they are surfing around. Of course the visitor has to have cookies enabled on their browser in order for this to work, and if they do, you, as a developer, can set up things like eCommerce and shopping carts without having to hit the database everytime they add something to the cart or they want to check the contents. Here is how you set up a PHP file to use session variables.

Difficulty: Easy
Instructions

Things You'll Need:

  • A PHP editor...notepad will do
  1. Step 1

    This tutorially assumes you know a little about PHP. Open your editor. I use notepad to create my PHP files.

  2. Step 2

    Create a PHP file that looks like the following:

    session_start()
    $_SESSION['session_name'] = {some value}


    Line 1 starts the session. It will automatically create a sessio id for you. If you ever need the value of the session id you can simply assign it to a variable as follows:

    $sessionid=session_id()

    The second line of code creates a session variable named "session_name" and assigns it the value of whatever comes after the equal sign. When you want to retrieve that value, you can write the code as follows:

    $myvalue=$_SESSION['session_name']

  3. Step 3

    When you want to erase all of your session variables and their values use the line of code:
    session_destroy()

    This will not, however, allow you to create a news session id the next time the visitor hits a PHP page with the session_start() code. [I learned that the hard way].

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2010 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US † requires javascript

eHow Computers
eHow_eHow Technology and Electronics