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:
Code:
auth.php // the meat and potatoes of this subject
connect.php // an excellent connection script
logout.php // destroy session and re-direct to login
nav.php // navigation include for site
index.php // main page of site
link_1.php // page of site
link_2.php // page of site
link_3.php // page of site
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.
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["username"])
{
$username=$_POST["username"];
$password=$_POST["password"];
}
elseif($_SESSION["username"])
{
$username=$_SESSION["username"];
$password=$_SESSION["password"];
}
// start and register session variables
session_register("username");
session_register("password");
// connect to database
include("connect.php");
// query for a user/pass match
$result=mysql_query("select * from users
where username='" . $username . "' and password='" . $password . "'");
// retrieve number of rows resulted
$num=mysql_num_rows($result);
// print login form and exit if failed.
if($num < 1){
session_destroy();
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;
}
?>
connect.php
PHP Code:
<?
// Login & Session example by sde
// connect.php
// replace with your db info
$hostname="localhost";
$mysql_login="root";
$mysql_password="";
$database="test";
if (!($db = mysql_connect("$hostname", "$mysql_login" , "$mysql_password")))
{
print("Can't connect to mysql.");
}
else
{
if (!(mysql_select_db("$database",$db)))
{
print("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.
<br><br>
You will now be returned to the login page.
<META HTTP-EQUIV=\"refresh\" content=\"2; URL=index.php\"> ";
?>
nav.php
PHP Code:
<?
// 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
PHP Code:
<?
// Login & Session example by sde
// index.php
// include auth and nav
include("auth.php");
// begin content
include("nav.php");
echo "This is my home page.";
?>
link_1.php
PHP Code:
<?
// Login & Session example by sde
// link_1.php
// include auth and nav
include("auth.php");
// begin content
include("nav.php");
echo "This is my Link 1.";
?>
link_2.php
PHP Code:
<?
// Login & Session example by sde
// link_2.php
// include auth and nav
include("auth.php");
// begin content
include("nav.php");
echo "This is my Link 2.";
?>
link_3.php
PHP Code:
<?
// Login & Session example by sde
// link_3.php
// include auth and nav
include("auth.php");
// begin content
include("nav.php");
echo "This is my Link 3.";
?>