PHP and sessions

php.pngSessions are a way to store data on the server rather than in a cookie on the clients computer.

One problem with sessions, is that they work for one visit only, whereas cookies can track information from one session to another by using the time() paramater. It's up to you, to decide what works best for your website. Just like with cookies you have add this session creation before any other code on your page.

To create a session in php you write the following:

    <?php
        session_start( );
    ?>
    <html>
        <head>
            <title>Page Title</title>
        <head>
        <body>

Once the session has been started, data can be added to the session by using the $_SESSION variable. To create, a username variable with a value of "hockeyFanatic":

Note: $_SESSION is what is considered to be a php superglobal variable and is available at all times.
    <?php
        $_SESSION["username"] = "hockeyFanatic";
    ?>


To read the session variable of username = hockeyFanatic, make a request to the $_SESSION var of username:

    <?php
        echo $_SESSION["username"]; //  This will display "hockeyFanatic to the screen
        $name =
$_SESSION["username"]; // This writes and stores "hockeyFanatic" to the var $name
    ?>


If you care to remove a session variable while a user is still surfing through your site, you can unset() the $_SESSION the variable.

    <?php
        unset($_SESSION["username"]);
    ?>


Now the username variable has been removed from the users session. You could also completely kill off the session and all variable's and behavior, by calling the destroy() method which is similar to the create method but the total opposite as in live() and death():

    <?php
        session_destroy();
    ?>


That's all it take's to work with php sessions. Now what you do with it is all up to you and whether you prefer cookies or sessions.

I prefer using cookie's over sessions most of the time, but I mostly prefer Oreo's with some milk and my son agree's with that as well.

Leave a comment

BMW, Toyota, BMW, Saab, Audi, Nissan Mercedes, BMW, Nissan, Honda,bmw bmw bmw Acura, Honda, Acura Forerunner, SUV, BMW, Mercedes, Toyota  
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan, Honda,Volvo Volvo Volvo Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan, Honda,Volvo Volvo Volvo Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota


Chicago Blackhawks
Chicago Blackhawks
Stanley Cup
Stanley Cup
Pittsburgh Penguins
Pittsburgh Penguins
Boston Bruins
Boston Bruins
Fire & Flames
Fire & Flames








 


Categories

BMW, Toyota, BMW, Saab, Audi, Nissan Mercedes, BMW, Nissan, Honda, Acura, Honda, Acura Forerunner,bmw bmw bmw SUV, BMW, Mercedes, Toyota