admin.php :
運行代碼框
[Ctrl+A 全部選擇 提示:你可先修改部分代碼,再按運行]
下面解釋:
function showf($mode=1){ //--------------------->這個小函數(shù)是輸出管理員登陸表單的,2中模式,和發(fā)表留言的思路一樣 if ($mode==1) {$admin_f="admin.php";} else { $admin_f="admin.php?doaction=sys"; } ?> <form action="<?=$admin_f?>" method="post" name="newform" id="newform"> <label>用戶:<input name="username" type="text" size="10" maxlength="10" /></label> <label>密碼:<input name="password" type="text" size="20" maxlength="10" /></label> <label><input maxlength="20" type="submit" name="Submit" value=" 登陸" > </label> </form> <?}?>
<?php $admin = false; //--------------------->防止全局變量造成安全隱患 session_start(); //--------------------->啟動會話,這步必不可少
if (isset($_SESSION["admin"]) ) { //--------------------->判斷是否登陸,如果session["admin"]不為空,已經(jīng)登陸.深度是 1 echo "<h4>您已經(jīng)成功登陸--></h4>";
if(isset($_GET['doaction'])) { //--------------------->登陸成功后,提交字段action不為空時執(zhí)行,深度 2
$doaction = $_GET['doaction']; switch($doaction){ case 'sys': //--------------------->登陸成功后,提交字段action為"sys"執(zhí)行,深度 3
echo"系統(tǒng)設(shè)置-->"; if($Submit) { foreach ($posts as $key => $value) { $posts[$key] = trim($value); } $password=md5($password); $conn->execute("update [php_admin] set [szd_user]='$username',[szd_password]='$password'"); echo "成功,2秒后返回"; echo "<meta http-equiv=\"refresh\" content=\"1;URL=admin.php\">"; }else{ showf(2); } break; case 'logout': //--------------------->登陸成功后,提交字段action為"logout"執(zhí)行清除session,深度 3
session_start(); unset($_SESSION["admin"]); header('location:index.php'); die; break; case 'delete': //--------------------->登陸成功后,提交字段action為"delete"執(zhí)行刪除留言,深度 3 $conn->execute("delete * from [php_guest] where id=$id") ; echo "刪除成功,2秒后返回"; echo "<meta http-equiv=\"refresh\" content=\"1;URL=index.php\">"; die; break; case 'editor': //--------------------->登陸成功后,提交字段action為"editor"執(zhí)行編輯留言,深度 3 if($Submit) { //--------------------->登陸成功后,提交字段action為"editor"而且表單已經(jīng)提交時執(zhí)行,深度 4
$posts = $_POST; foreach ($posts as $key => $value) { $posts[$key] = trim($value); } $conn->execute("update [php_guest] set [szd_user]='$user',[szd_title]='$title',[szd_qq]='$qq',[szd_content]='$ly',[szd_rep]='$rep' where id=$id"); header("location:index.php"); } else { //--------------------->登陸成功后,提交字段action為"editor"而且表單已經(jīng)沒有執(zhí)行,深度 4 $rs=$conn->execute("select * from [php_guest] where id=$id"); if($rs->eof and $rs->bof){ header("location:index.php"); die;} require_once('postfunction.php'); postfunction($rs->fields[1],$rs->fields[3],$rs->fields[2],$rs->fields[4],$rs->fields[0],$rs->fields[6],2); } default: break; } }else { //--------------------->登陸成功后,提交字段action為空時執(zhí)行,深度 2
?> <ul> <li><a href="?doaction=sys">系統(tǒng)設(shè)置</a></li> <li><a href="?doaction=logout">退出登陸</a></li> </ul> <?} } else { //--------------------->判斷:如果session["admin"]為空,沒登陸.深度是 1 ?> <?php if($Submit){ //--------------------->判斷表單提交后.深度是 2
$posts = $_POST; //--------------------->清除一些空白符號.深度是 3 foreach ($posts as $key => $value) { $posts[$key] = trim($value); } $password = md5($posts["password"]); //--------------------->自帶md5加密
$username = $posts["username"]; if (empty($password)||empty($username)) {echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">"; echo "<script language=javaScript> \n alert(\"輸入必要的信息\") \n</script>"; echo "<script language=javascript>javascript:history.back()</script>"; exit();}
$query = "SELECT id FROM [php_admin] WHERE [szd_password] = '$password' and [szd_user]='$username'"; $userInfo = $conn->execute($query); //--------------------->取得查詢結(jié)果,深度是 4 if (!($userInfo->eof and $userInfo->bof )) { //--------------------->如果有符合輸入的記錄,登陸成功 session_start(); //--------------------->當驗證通過后,啟動 Session $_SESSION["admin"] = 1; //--------------------->登陸成功的 admin 變量,并賦值 1 header('location:admin.php'); } else { //--------------------->如果發(fā)生eof 和 bof 錯誤,用戶名或者密碼錯,報錯,深度是 4 echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">"; echo "<script language=javaScript> \n alert(\"密碼錯誤或者用戶名錯誤\") \n</script>"; echo "<script language=javascript>javascript:history.back()</script>"; exit(); } } else { //--------------------->用戶沒登陸而且表單沒提交,輸出表單.深度是 2 showf(); } ?> <?}?>
如圖,管理員沒登陸:
管理員登陸成功:
管理員登陸成功以后首頁出現(xiàn)了超連接:
出處:藍色理想
責任編輯:moby
上一頁 發(fā)表留言,處理留言頁面 下一頁 使留言支持UBB
◎進入論壇網(wǎng)絡(luò)編程版塊參加討論
|