十天学会php之第七天
[b][color=#ff6600] [/color][/b]+w\&Xjp;l[color=#000000][font=宋体][size=10.5pt]学习目的:学会[/size][/font]SESSION[font=宋体][size=10.5pt]的使用[/size][/font][/color]YMhtM&g!_
[color=#000000]SESSION[/color][color=#000000][font=宋体][size=10.5pt]的作用很多,最多用的就是站点内页面间变量传递。在页面开始我们要[/size][/font]session_start();[font=宋体][size=10.5pt]开启[/size][/font]SESSION[font=宋体][size=10.5pt];[/size][/font][/color]
[color=#000000][font=宋体][size=10.5pt]然后就可以使用[/size][/font]SESSION[font=宋体][size=10.5pt]变量了,比如说要赋值就是:[/size][/font]$_SESSION['item']="item1";[font=宋体][size=10.5pt]要得到值就是[/size][/font]$item1=$_SESSION['item'];[font=宋体][size=10.5pt],很简单吧。这里我们可能会使用到一些函数,比如说判断是不是某[/size][/font]SESSION[font=宋体][size=10.5pt]变量为空,可以这么写:[/size][/font]empty($_SESSION['inum'])[font=宋体][size=10.5pt]返回[/size][/font]true or false[font=宋体][size=10.5pt]。[/size][/font][/color]kD9Itt+]]y
[font=宋体][size=10.5pt][color=#000000]下面综合一下前面所说的我们来看一个登陆程序,判断用户名密码是否正确。[/color][/size][/font]
[font=宋体][size=10.5pt][color=#000000]登陆表单是这样:[/color][/size][/font][color=#000000]login.php nxOW@T*l
<table width="100%" height="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<form action="checklogin.php" method="post"><td align="center" valign="middle"><table width="400" border="0" cellpadding="5" cellspacing="1" class="tablebg">
<tr class="tdbg">
<td colspan="2"><div align="center">Administrators Login</div></td>h0kY7W0^VGe eLQ
</tr>C{T*b6A6X:E
<tr class="tdbg">
<td><div align="center">Username</div></td>5D{,Z%GG[*g V8J
<td><div align="center">Ws)^hhlI
<input name="username" type="text" id="username">K2]d-e&{
</div></td>
</tr>
<tr class="tdbg">
<td><div align="center">Password</div></td>!{_;[$R"[c
<td><div align="center">
<input name="password" type="password" id="password">
</div></td>
</tr>:X^ g q%LA7]
<tr class="tdbg"> ~'m Utw&{6j1[
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="Submit">[OC!?:c }`-n _
<input type="reset" name="Submit2" value="Clear">:E} p5YB9k)J7ee O|
</div></td>M1Fbb}7J$}yp
</tr>G8y]*xn` U4Vg
</table></td></form>Yjo {8lI Bzc|
</tr>N~t*[y
</table>
[font=宋体][size=10.5pt]处理文件是这样[/size][/font][/color]
[color=#000000]<?]8U+]C"u:i[L&P
require_once('conn.php');B9q+be;ou2n${
session_start();
$username=$_POST['username'];'{b#W1y;Ln5V[0B
$password=$_POST['password'];
$exec="select * from admin where username='".$username."'";
if($result=mysql_query($exec))8su7_"T+?{Ya M
{
if($rs=mysql_fetch_object($result))"r6xyD e
{ a2vN d1mR;i3^c
if($rs->password==$password)
{;A}TD&nLv
$_SESSION['adminname']=$username;nx5Z3\"e.nI9U
header("location:index.php");
}Dj1Q#R(Bt$Q4~2xPM
else|6?.o9hC!E%o
{O5XKWNO@
echo "<script>alert('Password Check Error!');location.href='login.php';</script>";
}
}ku8@;A`&k
else!Osi%^1}kHe/[+h
{ U_4xrtkNH!l
echo "<script>alert('Username Check Error!');location.href='login.php';</script>";
}EJmz_7|-[E2E6b
}
else
{
echo "<script>alert('Database Connection Error!');location.href='login.php';</script>";mx`:U1O:R
}
!jbN3M~ j5c(`
?>'XJ3k(z9S p)A$M.g
*~k-QFQ:?CJ
conn.php[/color][font=宋体][size=10.5pt][color=#000000]是这样:[/color][/size][/font]5@\ I,C8wZ+es?;py
[color=#000000]<?
$conn=mysql_connect ("127.0.0.1", "", "");
mysql_select_db("shop"); K]-L;v;?BWE
?> YHmw2X}7G
[/color][color=#000000][font=宋体][size=10.5pt]由于[/size][/font] $_SESSION['adminname']=$username;[font=宋体][size=10.5pt]我们可以这样写验证是否登陆语句的文件:[/size][/font][/color][color=#000000]checkadmin.asp
<?
session_start();
if($_SESSION['adminname']=='')
{4|ML[s!f-S!v I O
echo "<script>alert('Please Login First');location.href='login.php';</script>";9c%Ua,BkpYO2B
}}x.~-xPb-_
?> __@^2RS-H
[font=宋体][size=10.5pt]十天学会[/size][/font]php[font=宋体][size=10.5pt]之第七天就说到这里,第八天说一下怎么弄一个分页。[/size][/font][/color]
页:
[1]