Example 2 (page2.php):
<?php
session_start
();
print "Value of 'my_session_variable': $my_session_variable";
?>
Try this out, you’ll see that the variable kept it’s value across pages!
Using session variables for authentication in conjunction with a database
is just as simple. Create a login-page gives the user a userid and
password form and posts to another PHP page (this example uses mysql):
is just as simple. Create a login-page gives the user a userid and
password form and posts to another PHP page (this example uses mysql):
<?php
session_start
();
if (@
$userid && @$password) {
$res = @mysql_query("SELECT userid FROM users WHERE userid='$userid' AND password='$password'");
if(@mysql_num_rows($res) != 0) {
$verified_user = $userid;
session_register("verified_user");
}
}
Header("Location: your_main_page.php");
?>
Now, on ‘your_main_page.php’, you call
then you can check the
user has been authenticated (and who he is). As simple as that.
session_start()
andthen you can check the
verified_user
variable to see if theuser has been authenticated (and who he is). As simple as that.
There’s alot more uses for session variables though, I use it for letting
the load of my database by caching certain values in the session rather
than reading them from the database on each page access.
the load of my database by caching certain values in the session rather
than reading them from the database on each page access.
Another small addition to my previous article: You can store
objects in session variables as of Beta 3 of PHP4. Several people pointed
this out to me, so I thought I’d tell you all.
objects in session variables as of Beta 3 of PHP4. Several people pointed
this out to me, so I thought I’d tell you all.