Php session
Introduce how to use session for checking
login.
Session 1: code (database file, login.php, checklogin.php,
sessionCSS.css, conn.php, index.php)
Session 2: concept thread
Session 3: explanation
Database
-------------------------------------------------------------------------------------------------------
create database `sessionShop`;
CREATE TABLE admin
(
name varchar(64) NOT NULL default '',
password varchar(64) NOT NULL default '',
primary key(name)
);
-------------------------------------------------------------------------------------------------------
login.php
-------------------------------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta
http-equiv="Content-Type" content="text/html;
charset=utf-8" />
<html>
<head>
<title>login</title>
<link
href="sessionCSS.css" type="text/css"
rel="stylesheet"/>
</head>
<body
class="basicbody">
<table
class="tbStyle" cellpadding="0" cellspacing="0"
align="center">
<tr
class="trStyle">
<td>
Administrator
LogIn
</td>
</tr>
<tr
class="trstyleBotm">
<td>
<form
action="checklogin.php" method="post">
<div
class="ContainerDiv">
<div
class="Div90">
<div
class="wordDiv100">
UserName:
</div>
<div
class="inputDiv500">
<input
type="text" maxlength="200" name="username"
id="username" placeholder="name" autofocus/>
</div>
</div>
<div
class="Div90">
<div
class="wordDiv100">
Password:
</div>
<div
class="inputDiv500">
<input
type="password" maxlength="200" name="psw"
id="password" placeholder="password"/>
</div>
</div>
<div
class="Div90">
<button
type="submit" name="Submit" value="submitIt"
class="btnStyle">
Submit
</button>
<button
type="reset" name="Reset" value="ResetIt"
class="btnStyle">
Reset
</button>
</div>
</div>
</form>
</td>
</tr>
</table>
</body>
</html>
-------------------------------------------------------------------------------------------------------
checklogin.php
-------------------------------------------------------------------------------------------------------
<?php
require_once
'conn.php';
session_start();
$sessionTBname
= 'admin';
$sUserName
= $_POST['username'];
$sPassword
= $_POST['psw'];
$_SESSION['adminName']
= $sUserName;
$_SESSION['adminPsw']
= $sPassword;
$execSQL
= 'select * from '.$sessionTBname." where name =
'".$sUserName."'";
if
($result = mysql_query($execSQL))
{
echo
"connect to Table.";
if
($rs = mysql_fetch_object($result))
{
echo
"Record is right.";
if
($rs->name == $sUserName && $rs->password == $sPassword
&& $_SESSION['adminName'] != '' && $_SESSION['adminPsw'] != '')
{
header("location:index.php");
}
else
{
echo
"<script>alert('Username, Password check error.');location:href='login.php'</script>";
}
}
else
{
echo
"<script>alert('Record check
error.');location:href='login.php'</script>";
}
}
else
{
echo
"can't connect to Table".mysql_error();
echo
"<script>alert('Table connection error.');location:href='login.php';</script>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta
http-equiv="Content-Type" content="text/html;
charset=utf-8" />
<html>
<head>
<title>checklogin</title>
<link
href="sessionCSS.css" type="text/css"
rel="stylesheet"/>
</head>
<body
class="basicbody">
</body>
<?php
session_unset();
unset($_SESSION['adminName']);
unset($_SESSION['adminPsw']);
mysql_close();
?>
</html>
sessionCSS.css
-------------------------------------------------------------------------------------------------------
table.tbStyle
{
width:
600px;
height:
400px;
border:
0px;
text-align:
center;
}
tr.trStyle
{
width:
600px;
height:
130px;
font-family:"Times
New Roman", Times, serif;
font-size:50px;
font-weight:
bolder;
text-align:center;
text-shadow:
grey 2px 2px;
}
tr.trstyleBotm
{
width:
600px; height: 270px;
}
div.ContainerDiv
{
width:
600px; height: 270px;
margin:
0 auto;
padding:
0px;
}
div.Div90
{
width:
600px; height: 90px;
margin:
0 auto;
padding:
0px;
}
div.wordDiv100
{
width:
120px; height: 66px;
margin:
10px auto;
padding:
0px;
float:
left;
text-align:
center;
font-family:"Palatino
Linotype", "Book Antiqua", Palatino, serif;
font-size:16px;
font-weight:
bolder;
line-height:
60px;
}
div.inputDiv500
{
width:
476px; height: 66px;
margin:
10px auto;
padding:
0px;
float:
left;
}
input#username
{
width:
320px; height: 30px;
margin:
12px auto;
text-align:
left;
border-radius:
6px;
-moz-border-radius:
6px;
-webkit-border-radius:
6px;
box-shadow:
2px 2px 5px #666;
-moz-box-shadow:
2px 2px 5px #666;
-webkit-box-shadow:
2px 2px 5px #666;
outline:
0px;
}
input#password
{
width:
320px; height: 30px;
margin:
12px auto;
text-align:
left;
border-radius:
6px;
-moz-border-radius:
6px;
-webkit-border-radius:
6px;
box-shadow:
2px 2px 5px #666;
-moz-box-shadow:
2px 2px 5px #666;
-webkit-box-shadow:
2px 2px 5px #666;
outline:
0px;
}
button.btnStyle
{
width:
60px; height: 30px;
border-radius:
5px;
border:
1px #cfdcec solid;
overflow:
hidden;
box-shadow:
1px 1px 3px gray;
background:
#e2eaf3;
}
button.btnStyle:hover
{
background:
#30588e;
}
conn.php
-------------------------------------------------------------------------------------------------------
<?php
$sessionUserName
= 'root';
$sessionPassword
= '654321';
$sessionDBname
= 'sessionShop';
$ConnSession
= mysql_connect("localhost", $sessionUserName, $sessionPassword) or
die("connect wrong --- ".mysql_error());
mysql_select_db($sessionDBname);
?>
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta
http-equiv="Content-Type" content="text/html;
charset=utf-8" />
<html>
<head>
<title>index</title>
<link
href="sessionCSS.css" type="text/css"
rel="stylesheet"/>
</head>
<body
class="basicbody">
<div
class="wordDiv100">Hello World.</div>
</body>
</html>
-------------------------------------------------------------------------------------------------------
the thread about this sample is do login
page first, then input username and password. Send username and password to checklogin.php.
If the username and password is fit, then jump to index.php.
EXPLANATION
conn.php
-------------------------------------------------------------------------------------------------------
<?php
$sessionUserName
= 'root';
$sessionPassword
= '654321';
$sessionDBname
= 'sessionShop';
$ConnSession
= mysql_connect("localhost", $sessionUserName, $sessionPassword) or
die("connect wrong --- ".mysql_error());
mysql_select_db($sessionDBname);
?>
-------------------------------------------------------------------------------------------------------
This file is about connecting to database.
login.php
-------------------------------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8" />
<html>
<head>
<title>login</title>
<link
href="sessionCSS.css" type="text/css"
rel="stylesheet"/>
</head>
<body
class="basicbody">
<table
class="tbStyle" cellpadding="0" cellspacing="0"
align="center">
<tr
class="trStyle">
<td>
Administrator
LogIn
</td>
</tr>
<tr
class="trstyleBotm">
<td>
//post form here
<form
action="checklogin.php" method="post">
<div
class="ContainerDiv">
<div
class="Div90">
<div
class="wordDiv100">
UserName:
</div>
//input username
<div
class="inputDiv500">
<input
type="text" maxlength="200" name="username"
id="username" placeholder="name" autofocus/>
</div>
</div>
<div
class="Div90">
<div
class="wordDiv100">
Password:
</div>
//input password
<div
class="inputDiv500">
<input
type="password" maxlength="200" name="psw"
id="password" placeholder="password"/>
</div>
</div>
<div
class="Div90">
//submit button and reset button
<button
type="submit" name="Submit" value="submitIt"
class="btnStyle">
Submit
</button>
<button
type="reset" name="Reset" value="ResetIt"
class="btnStyle">
Reset
</button>
</div>
</div>
</form>
</td>
</tr>
</table>
</body>
</html>
-------------------------------------------------------------------------------------------------------
checklogin.php
-------------------------------------------------------------------------------------------------------
<?php
//connect to database
require_once
'conn.php';
//start session
session_start();
$sessionTBname
= 'admin';
//get username from the FORM in login.php
$sUserName
= $_POST['username'];
//get password from the FORM in login.php
$sPassword
= $_POST['psw'];
//assign username and password to current
page session
$_SESSION['adminName']
= $sUserName;
$_SESSION['adminPsw']
= $sPassword;
//sql select the record according to the
username posted.
$execSQL
= 'select * from '.$sessionTBname." where name =
'".$sUserName."'";
//get the record
if
($result = mysql_query($execSQL))
{
echo
"connect to Table.";
//get the object of the record.
if
($rs = mysql_fetch_object($result))
{
echo
"Record is right.";
//check username and password are correct
and session is not null.
if
($rs->name == $sUserName && $rs->password == $sPassword
&& $_SESSION['adminName'] != '' && $_SESSION['adminPsw'] != '')
{
header("location:index.php");
}
else
{
echo
"<script>alert('Username, Password check error.');location:href='login.php'</script>";
}
}
else
{
echo
"<script>alert('Record check
error.');location:href='login.php'</script>";
}
}
else
{
echo
"can't connect to Table".mysql_error();
echo
"<script>alert('Table connection error.');location:href='login.php';</script>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta
http-equiv="Content-Type" content="text/html;
charset=utf-8" />
<html>
<head>
<title>checklogin</title>
<link
href="sessionCSS.css" type="text/css"
rel="stylesheet"/>
</head>
<body
class="basicbody">
</body>
<?php
session_unset();
unset($_SESSION['adminName']);
unset($_SESSION['adminPsw']);
mysql_close();
?>
</html>
That is all.