Thread: PHP Sessions
View Single Post
  #16 (permalink)  
Old 07-08-09, 03:22 AM
klaniak klaniak is offline
Wannabe Coder
 
Join Date: Apr 2005
Location: Underground
Posts: 119
Thanks: 0
Thanked 0 Times in 0 Posts
Here is an example if you want to use sessions:
My guess is probably you want to use mysql
PHP Code:

//you need to connect to database


session_start(); //we start the session
if(isset($_POST['submit']))
{
 
$user=strip_tags(addslashes(($_POST['username']));
 
$password=strip_tags(addslashes($_POST['password']));
//you select your database
//you grab the user id and name and compare them with $user and $password

$_SESSION['valid_user']="your id number from database";
$_SESSION['name']="the name of the user";

header('location: menu.php'); //the location where you want your users to be redirected
} else {
  
header('location: sorry.php?not_logged=yes'); //the location if they are not logged
 

You can use the code and include it on all your protected pages,
include('./library/login.php');
if(!$_SESSION['valid_user']){header('location: sorry.php');}

You will need also a logout.php to destroy your session, here is an example:

PHP Code:

session_start();

unset(
$_SESSION);
session_destroy();
header('location: index.php'); 
Reply With Quote