十天学会php之第七天
[b][color=#ff6600] [/color][/b]4BgyNm9s[color=#000000][font=宋体][size=10.5pt]学习目的:学会[/size][/font]SESSION[font=宋体][size=10.5pt]的使用[/size][/font][/color]
[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]Tb2s8_ nPG^Zh
[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]5q#r|%\q
[font=宋体][size=10.5pt][color=#000000]下面综合一下前面所说的我们来看一个登陆程序,判断用户名密码是否正确。[/color][/size][/font]!y4fP?Yeo-I
[font=宋体][size=10.5pt][color=#000000]登陆表单是这样:[/color][/size][/font][color=#000000]login.php
<table width="100%" height="100%" border="0" align="center" cellpadding="0" cellspacing="0">^%g3wGk A2U!x6]`+}4Q
<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>!Os ycwvl2P+RE
</tr>[`L s,x\;u k
<tr class="tdbg">
<td><div align="center">Username</div></td>
<td><div align="center">
<input name="username" type="text" id="username">j c$h#hCW
</div></td>
</tr>
<tr class="tdbg">
<td><div align="center">Password</div></td>(g t%V_G q`q
<td><div align="center">
<input name="password" type="password" id="password">
</div></td>
</tr>9h2yB"G5{yZ%x#Mp?a
<tr class="tdbg"> c*C)E F^4y;S
<td colspan="2"><div align="center">2Osf~ }
<input type="submit" name="Submit" value="Submit">OA n%rLnI
<input type="reset" name="Submit2" value="Clear">
</div></td>9yM2]'D0BVZ
</tr>
</table></td></form>(y9he l%E(hzV Q
</tr>CJ+G[h
</table>
[font=宋体][size=10.5pt]处理文件是这样[/size][/font][/color]
[color=#000000]<?
require_once('conn.php');
session_start();H-B4uvQ9d@re#]
$username=$_POST['username'];
$password=$_POST['password'];
$exec="select * from admin where username='".$username."'";
if($result=mysql_query($exec))6ZIYZX
{
if($rs=mysql_fetch_object($result))
{
if($rs->password==$password)
{L!j1aa3GsPT9q.`
$_SESSION['adminname']=$username;(^4_TS'`ZJ h
header("location:index.php");
}
else
{