学习目的:学会SESSION的使用 4KkjBPV
.|pyloL.
SESSION的作用很多,最多用的就是站点内页面间变量传递。在页面开始我们要session_start();开启SESSION; S-8wL%r
然后就可以使用SESSION变量了,比如说要赋值就是:$_SESSION['item']="item1";要得到值就是$item1=$_SESSION['item'];,很简单吧。这里我们可能会使用到一些函数,比如说判断是不是某SESSION变量为空,可以这么写:empty($_SESSION['inum'])返回true or false。 I,:R~^qJ8v
@DYxDap{
下面综合一下前面所说的我们来看一个登陆程序,判断用户名密码是否正确。 EPZ^I)
登陆表单是这样:login.php FccT@,.F
<table width="100%" height="100%" border="0" align="center" cellpadding="0" cellspacing="0"> .Kn)sD1
<tr> D]s8w
<form action="checklogin.php" method="post"><td align="center" valign="middle"><table width="400" border="0" cellpadding="5" cellspacing="1" class="tablebg"> x'.OLXx>
<tr class="tdbg"> z`^DQ8+\j
<td colspan="2"><div align="center">Administrators Login</div></td> z DP
</tr> .)zX<~,
<tr class="tdbg"> Wx i|(}
<td><div align="center">Username</div></td> )tRqt9Th*
<td><div align="center"> sU/R$Nbr
<input name="username" type="text" id="username"> 7HpfHqJ7
</div></td> H</Mh*Fl2G
</tr> 99\;jz7
<tr class="tdbg"> ^zMME*G
<td><div align="center">Password</div></td> A@W/
<td><div align="center"> /ox9m7Fz7
<input name="password" type="password" id="password"> QB Nnvg4v
</div></td> b~1]}9TJ
</tr> g@va@*|~d
<tr class="tdbg"> 0! :1o61
<td colspan="2"><div align="center"> &7{/ x~S{
<input type="submit" name="Submit" value="Submit"> JMUk=p<\
<input type="reset" name="Submit2" value="Clear"> B4<W%lm
</div></td> '>}dqp{Wr
</tr> $8{|25
*E
</table></td></form> QEavbh^S
</tr> FuiEy=+
</table> Qe&K
scffWqEo
处理文件是这样 !F|mCEU
<? (&w'"-`
require_once('conn.php'); lR^OS*v
session_start(); rT2gX^Mj&
$username=$_POST['username']; vSt7&ec
$password=$_POST['password']; }|k_sx:
$exec="select * from admin where username='".$username."'"; +0,{gDd+
if($result=mysql_query($exec)) u]B15mT?
{ Tk^J#};N
if($rs=mysql_fetch_object($result)) y}fF<qih'>
{ yN0!uzdW*
if($rs->password==$password) ,<^7~d{{3m
{ UogkQ& B
$_SESSION['adminname']=$username; c\n&Z'vK
header("location:index.php"); ",b3C.
} \8~P3M":c
else jAa{;p"jU
{ q*Hf%I"
echo "<script>alert('Password Check Error!');location.href='login.php';</script>"; \,w*K'B_Y
} U%Kv}s/(F{
} 5kK:1hH7
else gbf-3KSp^
{ Myl!tXawe8
echo "<script>alert('Username Check Error!');location.href='login.php';</script>"; ]kN<N0;\d
} ?y] q\>
} 62R94
else {M7`z,,[
{ M*r/TT
echo "<script>alert('Database Connection Error!');location.href='login.php';</script>"; m#D+Yh/y{n
} t3#My2 =
\k#|[d5W
?> woH B![Q,
,_JhvPWR,)
conn.php是这样: V-y"@0%1
<? },"T,t#
$conn=mysql_connect ("127.0.0.1", "", ""); .%.kEJh`
mysql_select_db("shop"); JJ50(h)U
?> $a.!X8sHB.
GwOn&EpY!
由于 $_SESSION['adminname']=$username;我们可以这样写验证是否登陆语句的文件:checkadmin.asp +>em
!~3
<? _^)Wrf+
session_start(); <K~> :4c
if($_SESSION['adminname']==') .`iG}j)\
{ ElAho3W
echo "<script>alert('Please Login First');location.href='login.php';</script>"; I^M%+\
} q(i^sE[y
?> SWAggW)
73-*|@6
呵呵,今天说到这里,明天说一下怎么弄一个分页。