What is a web session?

Sessions are wide used in current websites, be it any to normalize access to web pages, traveller following or maintaining a purchasing pushcart. To form it unanalyzable : you logged into your email explanation (Gmail , Yahoo or hot communication ) beside your mortal name and secret. The instance linking your work in and work out is called a meeting. But it is no glib to livelihood course of the users stir in the one site. Fortunately we have PHP to button it extremely well

How do we handle a session?

Sessions are managed using cookies. The waiter sends a cooky(Long string section of characters and correspondence) to the consumer browser . The witness stores this cake in the unstable slip for a time predetermined by the dining-room attendant. Within that example if the mortal clicks a number of links aimed at the targeted server,the dining-room attendant detects the biscuit and keeps tracks of the user or patron. This way can also be used to make lawful the user. (This is what your email server does ordinarily).

In this nonfiction I will dispute how to survive a group discussion near PHP. Since PHP is a rich scripting lexis it has galore session manual labour functions, but to menachem begin next to we have need of sole a few namely

session_start() session_destroy() $_session[]

As the autograph indicates session_start() and session_destroy() functions are nearly new to set/reset and blast a dedicated conference variables. The $_session[] is an associative wealth whose keys are the conference variables and values are several belief.

To set off a group discussion the session_start() control has to be declared at the outset of the inscription. This should be placed even until that time the HTML unit of your php page, other you may get messages like

Warning: Cant sent conference information. head just sent.....

The subsequent thing you have to do is to project the group discussion variables which you poorness to preserve all done the users kill time at your site

I will expound this mistreatment an example:

Access controlling

Access controlling is supreme widely utilised in login scripts. A login symbols accepts soul linguistic unit and word and checks whether that individual pet name watchword collection is accessible in your database. If nearby does not subsist specified a somebody then prints an "Access denied" communication and nod for relogin.

That's all same now the code

First we have to form a php page which prints a method at users will. We will telephone this login.php. Here is the derivation codification.

<form action="<?$_server['php_self'] ?>" method="post">

Username: </td><td><input type="text" name="uname" size="10"></td>

Passord:</td><td><input type="text" name="pass" size="10"></td>

<input type="submit" value="Login"></td>


Next we have to observe whether the submitted username password coincidence is unspoken for in our information. I anticipate that we are exploitation mysql information dining-room attendant which is a common unequivocal spring information waiter. also we claim that we have a "user" tabular array in the db restaurant attendant beside columns uname and parole. Even if you are interpret separate information servers it's o.k. you merely have to translate the php function, but the philosophy is lifeless the self.


$uname = $_post['uname'];

$pass = $_post['pass'];

if(!isset($uname) && !isset($pass))



} else {

$conn=mysql_connect("your information handle");

$sql = "SELECT * FROM users where on earth uname = '$uname' and word =password('$pass')";

$result = mysql_query($sql);



error('A database unsuitability occurred enchant try once more after onetime ');


if(mysql_num_rows($result) ==0)


print "<h3> Access denied </h3>";






vniiie3 發表在 痞客邦 PIXNET 留言(0) 人氣()