捌玖网络工作室's Archiver

admin 发表于 2008-3-26 16:40

十天学会php之第七天

[b][color=#ff6600] [/color][/b]
jt8c9s)^0b6_ OD-R [color=#000000][font=宋体][size=10.5pt]学习目的:学会[/size][/font]SESSION[font=宋体][size=10.5pt]的使用[/size][/font][/color]
S2Z._%JN /i$G'O3D/L\6~-` w&w"w
[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]
.k/r1S9l]"[qJ J [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]
Sh!WE+NI [font=宋体][size=10.5pt][color=#000000]下面综合一下前面所说的我们来看一个登陆程序,判断用户名密码是否正确。[/color][/size][/font]
UVE9Esnb [font=宋体][size=10.5pt][color=#000000]登陆表单是这样:[/color][/size][/font][color=#000000]login.php2}g;TB$_lc
<table width="100%" height="100%" border="0" align="center" cellpadding="0" cellspacing="0">
8N6jox+T_YOt <tr>7D7R |MZ [8t!\;e
<form action="checklogin.php" method="post"><td align="center" valign="middle"><table width="400" border="0" cellpadding="5" cellspacing="1" class="tablebg">
,\'l6|Cb6I2W <tr class="tdbg">
c A^PHy0h"n8| <td colspan="2"><div align="center">Administrators Login</div></td>
6D tU*s0G,Q1nZq8{)] </tr>
RDa-vymh D.pG7_X <tr class="tdbg">^)g'a*M*tuyY)A"h
<td><div align="center">Username</div></td>
c(J ^"lL ogm(K <td><div align="center">
? O8io7JN:\5I.n <input name="username" type="text" id="username">
lH+aC*C)Z2K </div></td>9W/^4_L5az/|6H
</tr>3B#\7\lO)i}C t@
<tr class="tdbg">
k}jxj$P <td><div align="center">Password</div></td>
#^tg(IaG <td><div align="center">P_:sbi4d
<input name="password" type="password" id="password">
@,q/r4Oo+_E/~ </div></td>w&Jh;x Ak
</tr>
[0t:]`+O|x <tr class="tdbg">&z!gO?U Kn
<td colspan="2"><div align="center">1K j4[b,[ q^/]
<input type="submit" name="Submit" value="Submit">
#r c_Z Z [&QH[,`ZF <input type="reset" name="Submit2" value="Clear"> A[,]Z:wR] Z1U
</div></td>
gI+abK%i$X </tr>:wmb JvdE1Q$TK]
</table></td></form>
%Z%XtwN*n </tr>r L'xD.|s#QCk$xl gE
</table>
T/e7jrgj;W t @og7p7A
[font=宋体][size=10.5pt]处理文件是这样[/size][/font][/color]-bt'N%p;M,n9b/I"{j
[color=#000000]<? nwB"ij t m Dv
require_once('conn.php');TcTUk"TI7ui,X
session_start();%D_"x.h!g0IoYS(H$A
$username=$_POST['username'];
Dj4xM7M6Ym\ $password=$_POST['password'];
j0H*W I sCS5G7_+| $exec="select * from admin where username='".$username."'";1A E9d/a:\2mu.R
if($result=mysql_query($exec))
:qIZ)qE-~ {
*w!Ly7uMJ z if($rs=mysql_fetch_object($result))'o#uHWd$@W4N
{Y)vj9as'{'Y F
if($rs->password==$password) C\b{7r"d
{LXJBKmF p
$_SESSION['adminname']=$username;
!Hmc2}+v\ c-R header("location:index.php");
&g#\'T-m5p"mK{ }
2M"R F|oH0u else y9dNnEUv-G
{
kZ s[N A8e echo "<script>alert('Password Check Error!');location.href='login.php';</script>";
$S.G E;@`;e.J x }
i~/O:i;T`mG[ }
,f/a3mt7e i else
(B'T^#N8H {
;I'J(DO+A echo "<script>alert('Username Check Error!');location.href='login.php';</script>";
&}8^b8gt{5h}2A*P }3GTV3U3?5H|"|.i \ U F6_
}
;}3e2r3ODR0s else
y9e5uN.v;L%L FHw {
_RJ4qr3|W,O echo "<script>alert('Database Connection Error!');location.href='login.php';</script>";
/G.e4v@W6_7o!b;h }
s {qE#wC4c-hq$H
1d"|t NNB!gH(j ?>
#D(YY3G*yb+M v#N_ b/@C!pd
conn.php[/color][font=宋体][size=10.5pt][color=#000000]是这样:[/color][/size][/font]
#i V)S0c1Hv [color=#000000]<?2\-s9N&D ^(e7D
$conn=mysql_connect ("127.0.0.1", "", "");
IeugHJ2b mysql_select_db("shop"); )y?s6?&u6`
?>
0I&huXO9[6B/r2|#H
7SCj2@5n9^ |6Z&N [/color][color=#000000][font=宋体][size=10.5pt]由于[/size][/font] $_SESSION['adminname']=$username;[font=宋体][size=10.5pt]我们可以这样写验证是否登陆语句的文件:[/size][/font][/color][color=#000000]checkadmin.aspo&I~ ]&X Y3mS
<?
;y+j2r8S;hr lH.A%n session_start();
p a8Yj#?h if($_SESSION['adminname']=='')
G%VE6t%v {}CN+NF,A
echo "<script>alert('Please Login First');location.href='login.php';</script>";%vkNPC~zT_r
}
3v ZM3hXm|v qh} ?> 'V0|1I&L+BGq
[font=宋体][size=10.5pt]十天学会[/size][/font]php[font=宋体][size=10.5pt]之第七天就说到这里,第八天说一下怎么弄一个分页。[/size][/font][/color]

页: [1]
【捌玖网络】已经运行:


Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.