Session Variable: Simple Login Checking



Session Variable Step By Step Tutorial - Part 3: From the user.form.php program at previous post, the program will check whether user's name and password is correct and registered in user.dat.php file or not. If not, the login will be denied. If it is correct, so the program will register the name and the password as variable session and the order is:
session_start();
session_register("user_session");
session_register("cat_user_session");

The inspection whether user have login or not is by checking whether variable session is already exist or not, you can use session_is_registered() function.

Here is the example if the user who did not login then tries to access page 1 (page1.php program file).

session variable check login

In order to check this variable session, you have to make a program file so that it can be 'include' in every pages:

<?
//variable session check program
//check_first.php

session_start();
if(!session_is_registered(user_session)){
  echo"You are not Login yet. 
  You are not allowed to access this page";
  echo("<br><a href=user.form.php><<< Login >>></a>");
	exit;
}
?>

Every pages of this website has their own user category. For example, page 1 (page1.php) and page 3 (page3.php) can be accessed by all of the user then page 2 (page2.php) can only accessed by super user.

<?
//page 1 is the same with page 3
//page1.php

include"check_first.php";
echo"Welcome in page 1 user: $user_session";
?>

<?
//page 2
//page2.php

include"check_first.php";

//check whether user is a super user
if($cat_user_session!="super user"){
	echo"You are not allowed to access this page";
	exit;
}
echo"Welcome in page 2 user: $user_session";
?>

The result is:

session variable

session variable


Series this article:
Session Variable: Introduction
Session Variable: Session Application
Session Variable: Simple Login Checking


Tag: session, variable, login Category: PHP Basic Post : March 08th 2008 Read: 1,163 Bookmark and Share

blog comments powered by Disqus