FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Sessions





yule
Ok, here is the low down on sessions.

1stly, what do sessions do?

Well, sessions are a variable you can set that gets passed throughtout the pages that can be used for all sorts of things that need short term data storage. The most common of these is a User System. This is what I will focus on in this tutorial.

Sessions are like cookies, but are usually stored server side and aren't stored in the browser cache on the user's local machine. Also, a session ends when the session_destroy() function is executed or when the user closes hisher browser window.

So how can I start using these funky session things?

Well, whenever you want to start using sessions, there is a golden rule. That golden rule should never be broken, or else your sessions will just not work!

This golden rule is:-

Always, always, always put session_start(); at the top of each page you are going to use sessions, no matter if you will be setting a session, destroying a session or retreiving data from a session. You may also wish to put ob_start(); at the very top of your pages if you get a "Headers already sent" error.

Now you have started your session, you need to set it. This can be done like this:-
Code:

<?php

session_start();
$_SESSION['sessionname'] = "username";

?>



Now, to use that session you would do the following:-

Code:

<?php

session_start();
echo "$_SESSION['sessionname'];

?>



This would echo "username" just as it would if you were entering data into a normal array. Got it so far? The little snippet above is part of the way sites say "Welcome Guest" if you aren't logged in, and "Welcome username" if you are. I will talk more in depth about that later.

Now, you have used your sessions, and your user wants to log out. True, they don't need to bother because the end of the session comes when they close the browser, but sometimes users like to feel secure and safe. Providing a log out button that ends the session does just that.

To simply destroy your session, use the following code:-

Code:

<?php

session_start();
session_destroy();

?>



Simple as that!

Now I know the basics, how can I put them to use?

Well, now I will show you the practical ways of using sessions, in the form of a User System. I will not give you all the code, because you should be able to work it out for yourself.

Is the user logged in?

Code:

<?php

session_start(); /* Starts the session */

if (!IsSet($_SESSION['user']) || ($_SESSION['user'] == "")) /* If the session doesn't exist or is blank do the following.*/
{
   echo "Please go back and log in."; // Says they need to log in.
   die(); /* Stops the script from parsing */
}else{
   echo "Yes, you are logged in!"; /* Says they are logged in. */
}

?>




If the user is not logged in, log them in

Code:

<?php

session_start();

$user = "username" /* In reality you would use $_POST['user']; */
$pass = "pass" */ In reality you would use MD5($_POST['pass']); See MD5 Hashing tutorial */

$_SESSION['user'] = $user;

echo "Welcome " . $_SESSION['user'] . ", enjoy your stay!";

?>



The above code doesn't validate the user's information etc. from a database, but it sets a session and echos the session.

Logging a user out

Code:
<?php

session_start();
session_destroy();

echo "You are logged out";

?>



Finally, limiting pages to users that are only logged in.

Code:

<?php

session_start();

if (!($_SESSION['user']) || ($_SESSION['user'] == "")
{
    die "Please log in to access this content.";
}else{
    EXECUTE PAGE STUFF}

?>



That's the basic way to do it! Oh yeah, nearly forgot, to stop the user's local machine caching the sessions (so they don't lose typed data if they hit back etc.) use the following just under session_start();:-

Code:
Header ("Cache-control: private");
Related topics
php sessions
PHP: Sessions and Cookies
php sessions
is mutiple login in windows XP simultaneously possible?
FireFox and Cookies/Sessions
php sessions for different accounts
Sessions are deleted fast.
php SESSIONS question
destroy\unset sessions?
Need Help : Dreamweaver sessions
Help with Dreamweaver sessions !!!!!!!!
I need some help please!
Sessions
phpBB sessions
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.