捌玖网络工作室's Archiver

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

十天学会php之第七天

[b][color=#ff6600] [/color][/b]
] kQhJ8ar m#f [color=#000000][font=宋体][size=10.5pt]学习目的:学会[/size][/font]SESSION[font=宋体][size=10.5pt]的使用[/size][/font][/color](SmE%b Ou;d

F'hG5l+]f)Ep [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]
3|8J,q(kB [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]J%M l,K(v W%qm
[font=宋体][size=10.5pt][color=#000000]下面综合一下前面所说的我们来看一个登陆程序,判断用户名密码是否正确。[/color][/size][/font]m@)I2blbe
[font=宋体][size=10.5pt][color=#000000]登陆表单是这样:[/color][/size][/font][color=#000000]login.php5Y5eU5F\ Z
<table width="100%" height="100%" border="0" align="center" cellpadding="0" cellspacing="0">
iQ2@8y&W <tr>
M/o-M['YX0YC <form action="checklogin.php" method="post"><td align="center" valign="middle"><table width="400" border="0" cellpadding="5" cellspacing="1" class="tablebg">
8Q.L/axa)kPb\+Z <tr class="tdbg">8J5Y ~&e,@f'A
<td colspan="2"><div align="center">Administrators Login</div></td>
O*y)P:t"A'e </tr>]9?&|7L"~.Hvwi
<tr class="tdbg">&MG4Y`7f/N
<td><div align="center">Username</div></td>
6tt$Wp5YQ,Nk,H7Z <td><div align="center">!_1Q:fAHI W
<input name="username" type="text" id="username">
Y,{m)bHx3t </div></td>Z1Y S#G'n*R ].m
</tr>
q-@.N%b\-wO <tr class="tdbg">
6gr5Q1O"YRX7_E9mK <td><div align="center">Password</div></td>P7|T2G2EV
<td><div align="center">$RN"d `y|#_e2WP)^
<input name="password" type="password" id="password">4| [[T-BPk
</div></td>
r%@D\ lD </tr>8Q*wM |0j8_
<tr class="tdbg">O3_f-vye$p ?;TV
<td colspan="2"><div align="center">I L7m?2YH
<input type="submit" name="Submit" value="Submit">St{Ef1TD&d
<input type="reset" name="Submit2" value="Clear">
A"iglp-Mq </div></td>M'J `8Ob$|
</tr>gSA*v3["n9gQ
</table></td></form>
YUZ h${-QJ.n|:a </tr> |c2wN-fr `
</table> p7uAXm_*O
+E3O6GI{K8t$R
[font=宋体][size=10.5pt]处理文件是这样[/size][/font][/color]e ap3Q4I.`&_
[color=#000000]<?
^ @ j A&h:p:C require_once('conn.php');d%Ft(X.zc)r4`
session_start();*etYnFbk ]8y
$username=$_POST['username'];0Y5od O*JR4?1N K
$password=$_POST['password'];
~C7Q3c%a4j/Vb $exec="select * from admin where username='".$username."'";S@'D|0y/ElLwl
if($result=mysql_query($exec))^ q(O!rw{ |N
{
QR8uem_ if($rs=mysql_fetch_object($result))/ebi,yc%z
{C&] evB,ER
if($rs->password==$password)mr-CGa7tC}c
{
"E-o;a~G/]]Z!l $_SESSION['adminname']=$username;gFSa8_?
header("location:index.php");9` c6f'z4[;J RG L
}9a I9x5g-E"Koe
else
5Z3@ q*P!H,iV8d {
DrF8TT-gc echo "<script>alert('Password Check Error!');location.href='login.php';</script>";
1b l ciq'A0G]^8W }
Ob?G5Y M4U:c }
M0dEG*Q[(ab else
`m ofCT b} N {1M iV/v0X
echo "<script>alert('Username Check Error!');location.href='login.php';</script>"; EH1kntS5y/s%qXd
}
Z!M"hp4Y8?U R }
Q{dg2n{S else
G.wT.n)}#sM {
.{"Wn-L$Fjc S echo "<script>alert('Database Connection Error!');location.href='login.php';</script>";
.g:[x;hI-|I }
l I iN^i {[|Da je!f'W*I'n&u9pH@
?>I*c_4M^!@9YM

T(gs!i \KF+^L conn.php[/color][font=宋体][size=10.5pt][color=#000000]是这样:[/color][/size][/font]
2@Yf(P9`#tvO [color=#000000]<?6v%{^WQ0x
$conn=mysql_connect ("127.0.0.1", "", "");KB8A1~6@7Ic T
mysql_select_db("shop"); /C"e7W}7h
?> 1? x,J%}/C%h3T*wT"p

%]6~,Y1m.WBW0Z.~ [/color][color=#000000][font=宋体][size=10.5pt]由于[/size][/font] $_SESSION['adminname']=$username;[font=宋体][size=10.5pt]我们可以这样写验证是否登陆语句的文件:[/size][/font][/color][color=#000000]checkadmin.asp
/~*a9iE0[8pki} <?A&| {A]c g
session_start();
7lu-_A7F,X&w if($_SESSION['adminname']=='')'^*w;nd5X6V
{
]F y]i,PJ Q echo "<script>alert('Please Login First');location.href='login.php';</script>";
w] y(B8gE\1[| Md }Rl-rh.cS't
?>
;R!w"LJ-l cCd [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.