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
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;
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.
;
exit;
} ?>
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.");
}
} ?>
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.
"; ?>
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(); ?>
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(); ?>
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(); ?>
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(); ?>
not a good example
ReplyDeleteyeh dekho is ne tumhara copy kar lia :p
ReplyDeletehttp://php.codenewbie.com/articles/php/1482/Login_With_Sessions-Page_1.html
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