  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14249
- 金币
- 2415
- 威望
- 1647
- 贡献
- 1363
|
' r# B. K5 l0 ]9 g
学习目的:学会SESSION的使用
- I6 x2 i5 }9 A% D8 |5 y. }. f) j* b' S2 O6 U# f2 U
SESSION的作用很多,最多用的就是站点内页面间变量传递。在页面开始我们要session_start();开启SESSION;
- K9 o' a$ L+ z0 S4 f6 k0 h' W然后就可以使用SESSION变量了,比如说要赋值就是:$_SESSION['item']="item1";要得到值就是$item1=$_SESSION['item'];,很简单吧。这里我们可能会使用到一些函数,比如说判断是不是某SESSION变量为空,可以这么写:empty($_SESSION['inum'])返回true or false。0 S1 u. _! r% ]+ w; [
下面综合一下前面所说的我们来看一个登陆程序,判断用户名密码是否正确。
0 k* g1 T& Z! ^7 o' U2 V登陆表单是这样:login.php
* A7 s" F U% e4 v* g# ~2 p<table width="100%" height="100%" border="0" align="center" cellpadding="0" cellspacing="0">- I4 }' {; D3 w
<tr>
# |3 b W! y& \4 W" q4 Q! `/ q<form action="checklogin.php" method="post"><td align="center" valign="middle"><table width="400" border="0" cellpadding="5" cellspacing="1" class="tablebg">& V0 R' z9 Q- V; v$ M5 V
<tr class="tdbg">0 c% D9 A0 c+ n# p
<td colspan="2"><div align="center">Administrators Login</div></td>
" B1 G+ D( m$ S8 W0 ~</tr>
$ {* _: H& D# y- M6 Q+ e+ O$ Y- ]<tr class="tdbg">
. Y. Y7 I# Y1 r# P9 Q ^5 W' S<td><div align="center">Username</div></td>% \: v9 g) V' w" A8 B: `
<td><div align="center">3 h4 F( w/ ]" U' w
<input name="username" type="text" id="username">7 o3 M# h7 g; o. @ U
</div></td>
l( B2 ^) f' w' Z H</tr>
& g% M) a- A* ?7 [% n B+ V9 J. `. F6 n<tr class="tdbg">
8 p: l. g0 f0 |& U, E3 x<td><div align="center">Password</div></td>
6 P4 Z5 E% a5 p* N, v* S) k<td><div align="center">: d" O& A+ q- L, f, E1 s) B W* H
<input name="password" type="password" id="password">
% x) A- u4 l( z) i O. q: w# q7 P$ W</div></td>* N7 K: S2 p0 k( }/ B8 c
</tr>. Z4 z, |; X9 a4 ]. k: [ |
<tr class="tdbg">
# a& B' E, [* I+ V I+ Q$ M. L<td colspan="2"><div align="center">: M) C7 r5 z# i5 h+ h
<input type="submit" name="Submit" value="Submit">
; y7 M7 |% V+ U<input type="reset" name="Submit2" value="Clear">; }# ^6 ?0 c3 H( |2 g+ l
</div></td>
7 w4 N/ t4 E7 B</tr>; e" N# W* w" [6 d7 Y$ Q
</table></td></form>1 n) u$ ?( l+ a6 X$ p( ]
</tr>( o/ X4 `, ?9 ?9 Y1 x* ^2 {
</table>
3 [/ T7 \. Y3 g) N& [" S, F' M( M/ R2 n: c
处理文件是这样
7 ^9 T1 \/ `6 [5 r2 x/ o5 ]<?) l& K3 K, [) K8 [: R
require_once('conn.php');
$ r, C. C% x+ ]/ Dsession_start();" S4 H0 X" S2 n
$username=$_POST['username'];* ^$ u8 A/ `- ?/ I2 h! R4 P
$password=$_POST['password'];
l* t- a$ n$ o7 A( R: d$exec="select * from admin where username='".$username."'";, z* S3 ^; l {8 i1 Q: n1 ?4 i
if($result=mysql_query($exec))7 ~) I+ a$ L. p& i! ~5 [
{
7 J, N" J* C! T8 Dif($rs=mysql_fetch_object($result))0 `! N' c% s2 E4 j! z% O ]
{, k) |- c- ^9 {* A& m# v" v/ ?
if($rs->password==$password)
" {+ V9 G( q* h* L% R{$ l, b- D4 S, F0 x# f" `/ G7 b
$_SESSION['adminname']=$username; x4 W: w: }& s Z; P1 v' O& T
header("location:index.php");
x% |( q9 N- Q/ Q; p}
7 q3 h+ k1 C1 x; ^& {& Y# Velse% D2 k$ s6 t P! l
{, g8 W0 Q8 t9 y+ \+ @
echo "<script>alert('Password Check Error!');location.href='login.php';</script>";6 t- j6 d% _. Z0 H$ H% E
}
# R2 A+ k" ^! U8 E; u: f4 i1 `}# N0 n' O v5 ~9 E# g
else0 g* q# u/ c$ h0 r* e
{
6 Y# e9 k' f: _9 E) D2 ?, m1 Z2 zecho "<script>alert('Username Check Error!');location.href='login.php';</script>";
+ }2 r; u) I2 c* X; G9 i& _}
& {5 X6 Q8 H2 y8 D$ Q& r}
% R, g* c0 a9 Gelse* y, d$ c' r l6 r3 S
{! Y1 U- _) e2 D* U) E- A
echo "<script>alert('Database Connection Error!');location.href='login.php';</script>";4 K# {* t. z4 Y
}
5 R" d4 Z- h* s% b |+ I/ v N" ?; Q# o1 W$ a3 L' V9 ]2 }
?>
" l4 D6 i9 W) n3 f* `0 Z2 k. K7 @8 H }( b$ t
conn.php是这样:; [" f6 {7 Y; z) q- t( C# N
<?
4 \7 S ^7 W1 h: k! Z, j$conn=mysql_connect ("127.0.0.1", "", "");* V$ ~8 m* h4 }: D$ y) ~
mysql_select_db("shop"); 9 f) d1 X2 R- {+ m, u: D$ J
?>
2 b) s* j4 x, b; T9 y- B" t; w% W( m/ I4 C# A+ x' u% D# `4 Z
由于 $_SESSION['adminname']=$username;我们可以这样写验证是否登陆语句的文件:checkadmin.asp
" T' f7 s1 J- c<?
4 @$ k; p$ Q3 ~session_start();+ Q9 M; m* C1 s/ |' ]1 W
if($_SESSION['adminname']=='')
4 T* z! S# l$ }! W0 w- ?* ]4 e{
4 S2 D7 i5 d g# s+ {echo "<script>alert('Please Login First');location.href='login.php';</script>";& e, |4 |4 J* q
}
# i9 D: `) N1 c) o% B- S# h?> $ N- o& ?1 ^. P2 A5 y3 c% U
十天学会php之第七天就说到这里,第八天说一下怎么弄一个分页。 |
|