学习目的:学会SESSION的使用 {`+:!X
"P!zu(h4
SESSION的作用很多,最多用的就是站点内页面间变量传递。在页面开始我们要session_start();开启SESSION; ekCt1^5Y
然后就可以使用SESSION变量了,比如说要赋值就是:$_SESSION['item']="item1";要得到值就是$item1=$_SESSION['item'];,很简单吧。这里我们可能会使用到一些函数,比如说判断是不是某SESSION变量为空,可以这么写:empty($_SESSION['inum'])返回true or false。 &\W5|*`x-
YDaGr6y4i
下面综合一下前面所说的我们来看一个登陆程序,判断用户名密码是否正确。 $]~|W3\G
登陆表单是这样:login.php iUq{c+h
<table width="100%" height="100%" border="0" align="center" cellpadding="0" cellspacing="0"> {4B7a6
<tr> ')Qb,#/,%
<form action="checklogin.php" method="post"><td align="center" valign="middle"><table width="400" border="0" cellpadding="5" cellspacing="1" class="tablebg"> 7,3 g{8
<tr class="tdbg"> A",Xn/d
<td colspan="2"><div align="center">Administrators Login</div></td> JpZ3T~Wrf
</tr> 0IxHB|^$
<tr class="tdbg"> l'RuzBQr
<td><div align="center">Username</div></td> g>n1mK|
<td><div align="center"> &GaI
<input name="username" type="text" id="username"> v%)=!T,
</div></td> 2#Y5*r's\
</tr> *n`8 -=
<tr class="tdbg"> CA3`Ee+rD
<td><div align="center">Password</div></td> 6#Bg99c
<td><div align="center"> uiq;{!dop
<input name="password" type="password" id="password"> q)!G5j3
</div></td> q]DE\*@
</tr> *lyRy/POB
<tr class="tdbg"> y<^hM6S?Z
<td colspan="2"><div align="center"> i)[~]D.EH8
<input type="submit" name="Submit" value="Submit"> S~\u]j^%y
<input type="reset" name="Submit2" value="Clear"> QuBaG<
</div></td> zvKypx
</tr> z<u@::
</table></td></form> v;:. k,E0
</tr> tRXR/;3O
</table> 2l}3L
0c]3 ,#
处理文件是这样 $Hal]
<? ;|.IUXEgcF
require_once('conn.php'); V&>mD"~MP
session_start(); , R $ZZ4
$username=$_POST['username']; 7Yly^
$password=$_POST['password']; /S`d?AV
$exec="select * from admin where username='".$username."'"; e[%g'}D:-
if($result=mysql_query($exec)) Ew2ksZ>B]&
{ J72YZrc
if($rs=mysql_fetch_object($result)) aZL
FsSY
{ =aRE
if($rs->password==$password) 4fau
9bW
{ |r/4
({n
$_SESSION['adminname']=$username; \q:PU6q
header("location:index.php"); UP@-@syGw
} gro@+^DmT
else $-lP"m@}
{ /@9-D
4
echo "<script>alert('Password Check Error!');location.href='login.php';</script>"; pd oCV
} sRSy++FRF
} *_tJ ;
else k1_3\JO"6
{ #3((f[
echo "<script>alert('Username Check Error!');location.href='login.php';</script>"; h7[PU^ m
} nX-%qc"
} &+7G|4!y
else Y?V>%eBu
{ yWZ%|K~$
echo "<script>alert('Database Connection Error!');location.href='login.php';</script>"; qb$f ,E[
} j~`rc2n%
=@go;,"
?> ;T?4=15c
I~NQt^sg
conn.php是这样: 3&7$N#v
<? nnBl:p>< k
$conn=mysql_connect ("127.0.0.1", "", ""); 7V KTI:5y
mysql_select_db("shop"); Oz7WtN
?> H8?Kgaj~vf
ccJ!N
由于 $_SESSION['adminname']=$username;我们可以这样写验证是否登陆语句的文件:checkadmin.asp y3pr(w9A
<? .RxAYf|
session_start(); Zn"1qLPF
if($_SESSION['adminname']==') \!,qXfTMB
{ e X q}0-*f
echo "<script>alert('Please Login First');location.href='login.php';</script>"; kV3Zt@+
} /WE1afe_R
?> l} UOg
K;#9:
Z^+
呵呵,今天说到这里,明天说一下怎么弄一个分页。