Second day, second tutorial.
I will continue with the membership script : after I shown you how to create the php register function, I will show today how to create the php login function and also I will show you how to check if a user is logged in or not.
<?php function loginUser($user, $pass) { if(empty($user) || empty($pass)) { exit(); }else{ $user = addentities($user); $pass = addentities($pass); $sql = "select * from users where user = '$user' and pass = '$pass'"; $rs = mysql_query($sql); if($rs) { if(mysql_num_rows($rs) != 0) { $row=@mysql_fetch_object($rs); $_SESSION['logged'] = "yes"; $_SESSION['user'] = $user; refreshPage(0, $_SERVER['PHP_SELF']); }else{ print "Wrong username and password"; } } } } ?>
Now, how simple is to check if a user is logged in?
<?php if(checkLogged() == false) { print "You are unauthorized to see this page, please login!"; }elseif(checkLogged() == true) { print "Welcome $_SESSION[user]"; } ?>
Please note that into head of the files, at the very begining, before outputing anything else you must add session_start(); function! Check this blog for detailed instructions on how to use sessions.
Good luck!