phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


Session Variable: Simple Login Checking

Tag: session, variable, login   Category: PHP Basic
post: 08 Mar 2008 read: 122


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

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting