PHP Tutorial : Membership script – Users Login

Welcome back to the simple php membership script tutorial.

Last time we saw how to create the php user registration function. We will continue today with the php login function and will also see 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 it to check if a user is logged in?

<?php

if(checkLogged() == false) {

print "You are not authorized to see this page, please login first!";
}elseif(checkLogged() == true) {

print "Welcome $_SESSION[user]";
}
?>

Please note that you must add the session_start(); function at the very begining of the script, before outputing anything else! Check this blog for detailed instructions on how to use sessions.

See you next time and happy coding!

2 Responses to “PHP Tutorial : Membership script – Users Login”

  1. XkiD | 13 Jun PHP Tutorial : Membership script - Users Login Posted by: admin  /  Category: Complete Scripts | blog.xkid.ro Says:

    [...] here to read the rest: 13 Jun PHP Tutorial : Membership script – Users Login Posted by: admin  /  Category: Complete Scri… Posted in PHP | Tags: coding, membership, membership-script, PHP, php-register, shown-you, [...]


  2. AdsBidWorld » PHP Tutorials - Crivion's Blog » Blog Archive » PHP Tutorial … Says:

    [...] function and also I will show you how to check if a user is logged in or not. Original post: PHP Tutorials – Crivion's Blog » Blog Archive » PHP Tutorial … This entry is filed under Membership. You can follow any responses to this entry through the RSS [...]


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>