捌玖网络工作室's Archiver

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

十天学会php之第七天

[b][color=#ff6600] [/color][/b]
;m-wi&h&b/w [color=#000000][font=宋体][size=10.5pt]学习目的:学会[/size][/font]SESSION[font=宋体][size=10.5pt]的使用[/size][/font][/color]Vd4L9t0P],k
7h zf8E|7g*HJ q
[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]
~x\7C'FW'I [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]
m7ptm.x*b4L n [ [font=宋体][size=10.5pt][color=#000000]下面综合一下前面所说的我们来看一个登陆程序,判断用户名密码是否正确。[/color][/size][/font]
+|&O*gphzq8o4jo{ [font=宋体][size=10.5pt][color=#000000]登陆表单是这样:[/color][/size][/font][color=#000000]login.php
D1gq\8nvz]H <table width="100%" height="100%" border="0" align="center" cellpadding="0" cellspacing="0"> N5B0b"V PJ`bo
<tr>
)h!q!G!?#l&H9F <form action="checklogin.php" method="post"><td align="center" valign="middle"><table width="400" border="0" cellpadding="5" cellspacing="1" class="tablebg">
bQ!i+Q9\"U\k <tr class="tdbg">%KN Ayp6r5E&o {o
<td colspan="2"><div align="center">Administrators Login</div></td>
:N'TAz:y/@ b </tr>
%sC/Fw wv <tr class="tdbg">
Fu#Z0D*u+k <td><div align="center">Username</div></td> HH^oY
<td><div align="center">C.W#[tk0DW
<input name="username" type="text" id="username">
,wf:}[Y5N </div></td>q#Qb%yRM
</tr>H1H2Mp2_-T0?$K@+jW
<tr class="tdbg">Uv"z B7pG c&VmD ?]
<td><div align="center">Password</div></td>
/fL] fhu4w2x;WVR <td><div align="center">
*fC8q.e0Mt q8HA <input name="password" type="password" id="password">2~|o|im2Fu
</div></td>/Q`R%mK @6D%?!O
</tr>
0K`1b1c4E[M8J-D <tr class="tdbg">
n Uo ]uka!E-}M <td colspan="2"><div align="center">O*o es(] }I3t9M
<input type="submit" name="Submit" value="Submit">#c:fXdM
<input type="reset" name="Submit2" value="Clear">
%xo2G+O,`4r-x </div></td>%Yo fy2w/y`[
</tr>
I0i9ff8P!a)E </table></td></form>
b \"H&LkI2c0A </tr>
CUH7L"KHotz`9}+ce </table>
x7_5Bn E4I8Q { f,\P~8m2k%?/@
[font=宋体][size=10.5pt]处理文件是这样[/size][/font][/color]
A6nbQX^:{ [color=#000000]<?
XH~8^#]U(Mx require_once('conn.php');
|(W d I-v/N1g7z session_start();Y!o6Im|de
$username=$_POST['username'];
%`j(al z9HP9t $password=$_POST['password'];
LKpp'H~ XB2L $exec="select * from admin where username='".$username."'";
H#_ D/^X*A]H"| if($result=mysql_query($exec))
|.o~:P:{1g N {
,t-w.tB @ if($rs=mysql_fetch_object($result))
ij;@,a*d+sZ a!E {
C Mwwb+n}-j9@ if($rs->password==$password)
UffVG {
lc3r g4c6t-AZ $_SESSION['adminname']=$username;
P-q%R?L!G6f1rL header("location:index.php");
`NJ1\Y }
,~9nxs1M#v||^t else
V \:EO-e%F,N {
k.hY8B8p)P `8b-V:P\ echo "<script>alert('Password Check Error!');location.href='login.php';</script>";
mwUfxw.f9L }-}C4pv` Q
}b{7|(O;?x \o
else\/o!p.ee:OQ
{ Kug Ov~.]XJ*G
echo "<script>alert('Username Check Error!');location.href='login.php';</script>"; `Ks"t1R
}|K+[ M9y)m4Fn
},fQ$k2b j$dwO;`)b
else
F~I6ef4ZN {
Gm:J ]7G'wO3E echo "<script>alert('Database Connection Error!');location.href='login.php';</script>";
|B9X9B{X VV }
bI)g"L;C+WQ-q
c4q9DiZf ?>&E"dl+k(o1qt%zK `o
gj z;K}U3h
conn.php[/color][font=宋体][size=10.5pt][color=#000000]是这样:[/color][/size][/font]
"J$O pEs-dc&z [color=#000000]<?
A#I2}3wnOa $conn=mysql_connect ("127.0.0.1", "", "");|)j V9wj
mysql_select_db("shop"); j2\FsK7i!T
?> H/c^}r#_2Q$VD

a/s~ q J#d8gtv [/color][color=#000000][font=宋体][size=10.5pt]由于[/size][/font] $_SESSION['adminname']=$username;[font=宋体][size=10.5pt]我们可以这样写验证是否登陆语句的文件:[/size][/font][/color][color=#000000]checkadmin.asp
j&I SV {j <?:}4b%uVn0_L)m
session_start();&@9rP;@#U*eQ
if($_SESSION['adminname']=='')
!L,mX H-Ri0J {3UnVdlL`c
echo "<script>alert('Please Login First');location.href='login.php';</script>";
3@'aRN$Xa4U N1w }
@/d+zL+BG ?> Z p4T*rbr+?lob
[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.