cng vic ng nhp vo h thng. Trong bi ny chng ta s tip tc cng vic pht trin ng dng trn, ngi s dng sau khi ng nhp c nhng thao tc c quyn nh thm, qun l, sa, xo thnh vin ca trang web. (xem li Bi 11: Vit ng dng ng nhp bng PHP & MYSQL). Sau khi ng nhp vo trang admin ngi dng s c y sang trang qun l admin, ti y ch nhng ai ng nhp ng vi username v password trong c s d liu v c quyn hn level l 2 th mi c th truy xut vo trang ny. V vy chng ta cn thao tc kim tra session qun l phin lm vic ca ngi s dng. t on code sau nhng trang bn mun kim tra quyn truy cp ca ngi s dng. <?php session_start(); if (isset($_SESSION['userid']) && { // t?i y th?c thi cc ho?t ?ng
1 2 3 4 5 6 7 8 9 10 11 12 <?php session_start(); if(isset($_SESSION['userid']) && $_SESSION['level'] == 2) { // ti y thc thi cc hot ng khi ng nhp thnh cng. } else { header("location: login.php"); exit(); } ?> Nh vy, ta to trang add_user.php v t on code trn, ng thi vit thm 1 form thc thi cng vic thm thnh vin.
1 2 <form action='add_user.php' method='POST'> Level: <select name='level'> 3 4 5 6 7 8 9 10 <option value='1'>Member</option> <option value='2'>Admin </option> </select><br /> Username: <input type='text' name='username' size='25' /><br /> Password: <input type='password' name='password' size='25' /> <br /> Re-Password: <input type='password' name='re-password' size='25' /><br /> <input type='submit' name='adduser' value='Add New User' /> </form> Tng t nh nhng bi trc, chng ta cng ln lt kim tra tng i tng: - Trc ht cn xem th ngi dng nhn nt Add New User cha ? - Sau li khai bo 2 bin $u v $p, mc nh ban u ta cho 2 bin ny l rng. - K ti ta kim tra xem ngi dng nhp y thng tin cha. - Tip theo l so snh gia password v re-password c ging nhau hay khng. - Cui cng chng ta gn bin $l s bng vi gi tr m khi ngi dng chn level cho user . (xem li Bi 4: X l gi tr form trong PHP) <?php if (isset($_POST['adduser'])) { $u=$p=""; if ($_POST['username'] == NULL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <?php if(isset($_POST['adduser'])) { $u=$p=""; if($_POST['username'] == NULL) { echo "Vui long nhap username<br />"; } else { $u=$_POST['username']; } if($_POST['password'] != $_POST['re-password']) { echo "Password va re-password khong chinh xac<br />"; } else { if($_POST['password'] == NULL ) { echo "Vui long nhap password<br />"; } else { $p=$_POST['password']; } } 28 29 30 $l=$_POST['level']; } ?> K n chng ta li tip tc kim tra xem c tn ti bin $u, $p v $l hay khng (ch khi ngi dng nhp liu th mi tn ti cng lc 3 bin ny). sau khi kim tra s tn ti ca 3 bin ny chng ta tip tc dng PHP kt ni c s d liu kim tra xem username m ngi dng va chn c trng vi username khc trong c s d liu cha. nu trng th phi xut thng bo li yu cu h chn username khc cn nu khng th ta li tin hnh thm thnh vin mi ny vo CSDL. <?php if ($u & $p & $l) { $conn=mysql_connect("localho mysql_select_db("project",$con
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?php if($u & $p & $l) { $conn=mysql_connect("localhost","root","root") or die("can't connect this database"); mysql_select_db("project",$conn); $sql="select * from user where username='".$u."'"; $query=mysql_query($sql); if(mysql_num_rows($query) != "" ) { echo "Username nay da ton tai roi<br />"; } else { $sql2="insert into user(username,password,level) values('".$u."','".$p."','".$l."')"; $query2=mysql_query($sql2); echo "Da them thanh vien moi thanh cong"; } } ?> Ti y ta kt thc thao tc thm mt thnh vin mi vo c s d liu. Sau y l ton b code trong ng dng ny. <?php session_start(); if (isset($_SESSION['userid']) && { if (isset($_POST['adduser']))
1 2 3 4 5 6 <?php session_start(); if(isset($_SESSION['userid']) && $_SESSION['level'] == 2) { if(isset($_POST['adduser'])) { 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 $u = $p =""; if($_POST['username'] == NULL) { echo "Vui long nhap username<br />"; } else { $u=$_POST['username']; } if($_POST['password'] != $_POST['re-password']) { echo "Password va re-password khong chinh xac<br />"; } else { if($_POST['password'] == NULL) { echo "Vui long nhap password<br />"; } else { $p=$_POST['password']; } } $l=$_POST['level']; if($u & $p & $l) { $conn=mysql_connect("localhost","root","root") or die("can't connect this database"); mysql_select_db("project",$conn); $sql="select * from user where username='".$u."'"; $query=mysql_query($sql); if(mysql_num_rows($query) != "" ) { echo "Username nay da ton tai roi<br />"; } else { $sql2="insert into user(username,password,level) values('".$u."','".$p."','".$l."')"; $query2=mysql_query($sql2); echo "Da them thanh vien moi thanh cong"; } } } } ?> <form action='add_user.php' method='POST'> Level: <select name='level'> <option value='1'>Member</option> <option value='2'>Admin </option> </select><br /> 57 58 59 60 61 62 63 64 65 66 67 68 69 Username: <input type='text' name='username' size='25' /><br /> Password: <input type='password' name='password' size='25' /> <br /> Re-Password: <input type='password' name='re-password' size='25' /><br /> <input type='submit' name='adduser' value='Add New User' /> </form> <?php } else { header("location: login.php"); exit(); } ?> bi sau, chng ta li tip tc tm hiu ng dng kt hp PHP v MYSQL cho vic qun l tt c thnh vin trong database ra nh th no.