homeHome contactContact portfolioPortfolio musicMusic videoVideo historyHistory calendarCalendar rssRSS
karlpox | with GOD all things are possible

Using Sessions

How to use sessions

What is session anyways? According to php.net session is a “way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.” As for me session is like a super global array which can be passed to different pages using the session_start() function. Sessions are very useful especially an on page that requires users to be logged. Because sessions can track weather a user is still logged or not. So let’s get started using sessions in log in cases.

First of all you have to insert the session_start() function at the most top of your coded. So it must be found on line 0 of your codes. For example.

<?php session_start(); ?>

But some servers like mine displays error even if the session_start() is already at the most top of your code so you add this to your code to solve this problem.

<?php
session_cache_limiter(`nocache`);
session_start();
?>

And you need to do a session_start() on every page if you want the user to be logged in order to access the page. So now how do we determine if the session of the user is still active or not? So after you check if the username & password of the user is correct you create a session for the user to know if the user is still active or not. So after checking if the user is valid you do this.

<?php
$_SESSION[`active`] = true;
?>

The word `active` is not constant, you can change it to anything you want. You can even change it to something like $_SESSION[`weeee`] = active; it really doesn’t matter as long as you remember it. So to check if the user is still active, on every page that requires a user to be logged at the most top you do this.

<?php
session_start();
if ( $_SESSION[`active`] != true )
{
//post a redirect here for example.
echo `<meta http-equiv=”refresh” content=”0;url=login.php”/>`;
};
?>

The code above means on the second line we indicated the session_start() so the session from the previous page will be pass to this page. And the next line we check if the session of the user is still active. If its not active the we used the meta function to redirect the user to the login page which is the login.php and if the user is still active the user can access this page without problems. Easy huh? ^_^

There are 9 Comments to this article

AJAX learner says:
03/22/2008

Nice, I’m looking forward for Ajax too. Nice one poxXx. I’m learning pa. Nice thing that you implemented it! Xiao!

Ronald says:
03/22/2008

lol… kinsa na si wakeke?? harhar

Ronald says:
03/22/2008

try daw karl himo ug chatbox… :D:D paspas gud xa.. hehehe.. online chatrum bah… :D

karLpOx says:
03/22/2008

^_^ ahahahah ^_^

karLpOx says:
03/22/2008

awww. wala ko kblo knsa na. nka smart bro mana so dynamic ilang ip. lols. try nako bhat ana pero sunod na dghan pako unahon. lol

karLpOx says:
03/23/2008

spam test…

joseph says:
03/23/2008

karl. static ang IP sa SMBRO.. ehhee nice work!

karLpOx says:
03/23/2008

awww. naga base lang ko sa comments ni abz. since smartbro users man siya ang naga change iyang ip sa uban comments niya.

karLpOx says:
03/23/2008

AJAX bug testing…

Write a Comment