How to do authentication with sessions using php

Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to do authentication with sessions using php

Post by Neo » Sun Feb 28, 2010 9:43 pm

There are 8 pages in this example as below.

Code: Select all

// these files should be put in a directory named 'inc'
inc/auth.php      // the meat and potatoes of this subject
inc/connect.php // an excellent connection script
inc/nav.php     // navigation include for site

// these files should be on the web root
index.php  // main page of site
link_1.php // page of site
link_2.php // page of site
link_3.php // page of site
logout.php   // destroy session and re-direct to login
Here is the SQL Schema for the table I Query:

Code: Select all

CREATE TABLE users (
  user_id int(10) unsigned NOT NULL auto_increment,
  username varchar(20) NOT NULL default '',
  password varchar(20) NOT NULL default '',
  PRIMARY KEY  (user_id)
) TYPE=MyISAM;
If you copy and paste these files into your favourite text editor, edit the connect.php with your mysql settings, it should work on your server if sessions are supported.

inc/auth.php

Code: Select all

<?
// Login & Session example by sde
// auth.php

// start session
session_start(); 

// convert username and password from _POST or _SESSION
if($_POST){
  $_SESSION['username']=$_POST["username"];
  $_SESSION['password']=$_POST["password"];  
}

// query for a user/pass match
$result=mysql_query("select * from users 
  where username='" . $_SESSION['username'] . "' and password='" . $_SESSION['password'] . "'");

// retrieve number of rows resulted
$num=mysql_num_rows($result); 

// print login form and exit if failed.
if($num < 1){
  echo "You are not authenticated.  Please login.<br><br>
  
  <form method=POST action=index.php>
  username: <input type=text name=\"username\">
  password: <input type=password name=\"password\">
  <input type=submit>
  </form>";
  
  exit;
}
?>
inc/connect.php

Code: Select all

<?
// Login & Session example by sde
// connect.php

// replace with your db info
$hostname="localhost";
$mysql_login="myusername";
$mysql_password="mypass";
$database="test";

if (!($db = mysql_connect($hostname, $mysql_login , $mysql_password))){
  die("Can't connect to mysql.");    
}else{
  if (!(mysql_select_db("$database",$db)))  {
    die("Can't connect to db.");
  }
}
?>
logout.php

Code: Select all

<?
// Login & Session example by sde
// logout.php

// you must start session before destroying it
session_start();
session_destroy();

echo "You have been successfully logged out.

<br><br>
You will now be returned to the login page.

<META HTTP-EQUIV=\"refresh\" content=\"2; URL=index.php\"> ";
?>
inc/nav.php

Code: Select all

<?
// Login & Session example by sde
// nav.php
?>

<a href=index.php>Home</a> |
<a href=link_1.php>link_1</a> | 
<a href=link_2.php>link_2</a> | 
<a href=link_3.php>link_3</a> |
<a href=logout.php>logout</a>

<br><br>
index.php

Code: Select all

<?
// Login & Session example by sde
// index.php

// connect to database
include("inc/connect.php");

// include auth and nav
include("inc/auth.php");

// begin content
include("inc/nav.php");

echo "This is my home page.";

// close mysql connection
mysql_close();
?>
link_1.php

Code: Select all

<?
// Login & Session example by sde
// link_1.php

// connect to database
include("inc/connect.php");

// include auth and nav
include("inc/auth.php");

// begin content
include("inc/nav.php");

echo "This is my Link 1.";

// close mysql connection
mysql_close();
?>
link_2.php

Code: Select all

<?
// Login & Session example by sde
// link_2.php

// connect to database
include("inc/connect.php");

// include auth and nav
include("inc/auth.php");

// begin content
include("inc/nav.php");

echo "This is my Link 2.";

// close mysql connection
mysql_close();
?>
link_3.php

Code: Select all

<?
// Login & Session example by sde
// link_3.php

// connect to database
include("inc/connect.php");

// include auth and nav
include("inc/auth.php");

// begin content
include("inc/nav.php");

echo "This is my Link 3.";

// close mysql connection
mysql_close();
?>
Post Reply

Return to “PHP & MySQL”