Login With Sessions
There has been many inquiries/posts regarding login and sessions, I decided to create and test some scripts to share.
I've made a mock-up site consisting of 8 pages:
// 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:
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
<? // 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
<? // 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
<? // 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
<? // 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
<? // 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
<? // 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
<? // 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
<? // 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(); ?>
confused but looks good
confused but looks good
I am still not understand the
I am still not understand the programming language.
if you could help me.
Please create a thread in the
Please create a thread in the appropriate forum section and any issue can be discussed there.
excellent example.
excellent example.
it works a lot thanks for
it works a lot
thanks for this tutorial
nice
nice
Since you didn't mention any
Since you didn't mention any error codes, I'm going to ask, did you come across something similar to this thread ?
didt work
didt work
Post new comment