Jump to content

  •  

Photo
- - - - -

P10: Passing Variables P3 Sessions

PHP4Beginners

  • Please log in to reply
No replies to this topic

#1 DraxxTV

DraxxTV

    Member

  • SuperGeek
  • PipPip
  • 14 posts

Posted 06 July 2014 - 10:46 PM

Session

A PHP Session allows information to be stored on a server for later use. Information saved this way is only temporary and will only be kept until the user leaves the website. They work by creating a Unique ID (UID) for each visitor and storing information for this UID on the server. There are no limit on the number of variables; also, there's no limit on the amount of data you can store in a session. The only limits are the power of your server. If for example you had sessions of 50+MB and a thousand visitors, You may find things been very slow and you may not be able to deal with the load. In this tutorial, to explain the uses of Sessions, I will be creating a session based hit counter. This will run across all of the pages people visit and will count how many pages they have visited. Before we do this though, I will just go over the basics of how you can work with SESSIONS.

 

Starting a Session

The first thing you need to do whenever you plan on using a Session is to start it. The way you do this is by including this line of code.

session_start();

Now that we have started a Session, we can start learning what you can do with them. You only need to start a session once per page.

Setting  a Variable

When you want to set a variable in a session, once you have first started a session, all you need to do is the following. I will show you by setting a variable of Username to fred:

$_SESSION['username']='fred';

You can store the same kind of data you would in other variables so if you wanted to store things like arrays then you could. The way you would go about storing an array is like this:

$_SESSION['array']=array(
"Key 1" => "Value 1",
"Key 2" => "Value 2",
"Key 3" => "Value 3",
"Key 4" => "Value 4");

Reading a Variable

Reading a variable is fairly easy. If you wanted to value of a session, you can do so like this:

echo $_SESSION['username'];

The same principle works for if you wanted to print an array, you would do so like this:

print_r($_SESSION['array']);

If you wanted to define a session variable as a normal variable which is more easily accessible, you can do so like this:

$username=$_SESSION['username'];

This would also be the same for any other kind of data you had inside the session variable.

Changing a Variable

The way you would go about changing the value of a sessions variable would basically be the same principle of just resetting the session variable just like we did above.

Deleting a Variable

The way you would go about deleting a variable in a session is to either declare it like we did above or to just unset it like you would in an array (As it is an array.).

unset($_SESSION['username']);

Destroying a Session

To destroy a session we can do this:

session_destroy();

 Playing with Sessions with a Session Based Hit Counter

The next thing we are going to do is to have a little play with sessions and write a little file which can be included which will count the number of pages that have been loaded and display it to the user. The first thing we are going to do is to have a variable to count the number of pages the user has been on. We shall call this hitCounter. We then need to see if it has been set yet (they have already visited the domain recently so we need to increment the number by 1) or if it hasn't, set it to 1. We can do this with the following:

session_start();
if(isset($_SESSION['hitCounter'])){
    $_SESSION['hitCounter']++;
}else{
    $_SESSION['hitCounter'] = 1;
}

Once you have done that, you are going to want to define that as a local variable for later use.

$hitCounter=$_SESSION['hitCounter'];

We can now call this later down the page a lot easier. The full code for this page is below


<?
session_start();
if(isset($_SESSION['hitCounter'])){
    $_SESSION['hitCounter']++;
}else{
    $_SESSION['hitCounter'] = 1;
}
$hitCounter=$_SESSION['hitCounter'];
?>

<html>
    <head>
        <title>PHP For Beginners P10: Passing Variables P3 Sessions</title>
    </head>
    <body>
        <center>
            <h1>PHP For Beginners P10: Passing Variables P3 Sessions</h1>
            <?
                echo "You have been on ".$hitCounter . " pages.<br>";
            ?>

        </center>
    </body>
</html>

Attached Files







Also tagged with one or more of these keywords: PHP4Beginners