This Season
 

How to Set a Session Variable in PHP

How to Set a Session Variable in PHPthumbnail
PHP session variables are used in e-commerce sites.

PHP is a Web programming language used in many sites to run database-driven sites and e-commerce sites. One of the many attributes of PHP is the capability to hold information you can pass on from one Web page to another. To do this in PHP, you need to set a session variable. Session variables serve as temporary storage for certain information such as usernames and passwords or shopping cart items. Session variables only last during the time a visitor is on your site and the information is automatically deleted once he leaves the site.

Related Searches:
    Difficulty:
    Moderately Challenging

    Instructions

      • 1

        Open a new file in Notepad.

      • 2

        Type in the code to start your PHP session:

        <?php

        session_start();

        ?>

      • 3

        Add the code to set your session variable:

        <?php

        session_start();

        $_SESSION["color"] = "red";

        ?>

        In the above example, the session variable is set to "red."

      • 4

        Add the following code to test if the session variable is set :

        <?php

        echo $_SESSION["color"];

        ?>

      • 5

        Save it as a PHP file.

      • 6

        Upload the file to your Web host and open it on your browser. You should be able to see a page displaying the text "red."

    Related Searches

    References

    • Photo Credit Comstock Images/Comstock/Getty Images

    Read Next:

    Comments

    You May Also Like

    Follow eHow

    Related Ads