Monday, March 22, 2010

PHP login and sessions

Code:
// 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:
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 favorite text editor, edit the connect.php with your mysql settings, it should work on your server if sessions are supported.

inc/auth.php
PHP Code:
// 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.


  
    username: 
  password: 
  
  "
;
  
  exit;
}
?>
inc/connect.php
PHP Code:
// 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
PHP Code:
// 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.




You will now be returned to the login page.

 "
; ?>
inc/nav.php
PHP Code:
// Login & Session example by sde
// nav.php
?>
Home |
link_1 | 
link_2 | 
link_3 |
logout


index.php
PHP Code:
// 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
PHP Code:
// 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
PHP Code:
// 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
PHP Code:
// 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(); ?>

3 comments:

  1. yeh dekho is ne tumhara copy kar lia :p

    http://php.codenewbie.com/articles/php/1482/Login_With_Sessions-Page_1.html

    ReplyDelete
  2. Thanks Faheem and Tariq. I Post here almost stuff for my sec because its easy to search in blog as compare to other channels. In fact its my learning diary. :)

    ReplyDelete

MyUrduStuff

Search This Blog

My Urdu Stuff