php編寫用戶登陸和登出代碼 我看這里php的東東好象不是很多,所以貼一個自己用php寫的用戶登陸和登出代碼,數(shù)據(jù)庫是SQLserver <? if(isset($_GET[‘username‘])) { session_start(); $errormsg = ""; $input[‘username‘] = strtolower(trim($_GET[‘username‘])); if($errormsg == "") { include("db_link.php");//你自己SQL數(shù)據(jù)庫所在路徑 $sql = "select user_id, username, status from guestbook where username = ‘".$input[‘username‘]."‘ and status = 1"; $result = mysql_query($sql, $link) or die(‘Query database failed‘); $num = mysql_num_rows($result); if($num < 1) { $errormsg = "用戶名不正確,請重新登錄!"; } else { $row = mysql_fetch_array($result); if($row[‘username‘] == $input[‘username‘]) { $_SESSION[‘s_user_id‘] = $row[‘user_id‘]; $_SESSION[‘s_username‘] = $row[‘username‘]; $_SESSION[‘s_status‘] = $row[‘status‘]; $_SESSION[‘time_last_load‘] = time(); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>管理員登錄</title> <style> button,input{font-size:12px;padding-top:2px;font-family : Arial, Helvetica, sans-serif;} a:link:{color:#666666;} a:visited:{color:#666666;} a:hover:{color:#000000} </style> </head> <body style="font-size: 12px;"> <table width="280" border="0" cellspacing="0" cellpadding="0" align="center" style="font-size: 12px;border:1px solid #639ECE;"> <tr><td height="10"></td></tr> <tr><td height="40" align="center">登錄成功!</td></tr> <tr><td height="10"></td></tr> <tr><td height="22" align="right" bgcolor="#EFFBFF"><a href="javascript:void(null);" onclick="window.close();">關(guān)閉窗口</a> </td></tr> </table> </body> </html> <? } else { $errormsg = "用戶名不正確,請重新登錄!"; } } } if($errormsg <> "") { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>管理員登錄</title> <style> button,input{font-size:12px;padding-top:2px;} </style> </head> <body style="font-size: 12px;"> <table width="280" border="0" cellspacing="0" cellpadding="5" align="center" style="font-size: 12px;border:1px solid #639ECE;"> <tr><td height="10" colspan="2"></td></tr> <form action="userlogin.php"> <tr> <td width="60" align="right">用戶名:</td> <td width="220"><input type="text" name="username" value="<?=$input[‘username‘]?>"></td> </tr> <tr> <td></td> <td align="center"> <input type="submit" value=" 登錄 "></td> </tr> </form> <tr> <td height="10" colspan="2"></td> </tr> <tr> <td height="22" colspan="2" bgcolor="#EFFBFF"><?=$errormsg?></td> </tr> </table> </body> </html> <? } } else { ?> <!--登錄頁面--> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>管理員登錄</title> <style> button,input{ font-size:12px; padding-top:2px; } </style> </head> <body> <table width="280" border="0" cellspacing="0" cellpadding="5" align="center" style="font-size: 12px;border:1px solid #639ECE;"> <tr><td height="10" colspan="2"></td></tr> <form action="userlogin.php"> <tr> <td width="60" align="right">用戶名:</td> <td width="220"><input type="text" name="username"></td> </tr> <tr> <td></td> <td align="center"> <input type="submit" value=" 登錄 "></td> </tr> </form> <tr><td height="10" colspan="2"></td></tr> </table> </body> </html> <? } ?> |
|
||
要用這段代碼的話,可以新建一個名為userlogin的php文件,然后把以上內(nèi)容全部拷進(jìn)去,保存一下就可以了! 我是用session記錄的! 登出代碼(如下),也只需新建一個名為userlogout的php文件,然后拷進(jìn)全部內(nèi)容,保存。 <? session_start(); if(isset($_SESSION[‘s_user_id‘])) { session_unregister(‘s_user_id‘); } if(isset($_SESSION[‘s_username‘])) { session_unregister(‘s_username‘); } if(isset($_SESSION[‘s_status‘])) { session_unregister(‘s_status‘); } if(isset($_SESSION[‘time_last_load‘])) { session_unregister(‘time_last_load‘); } session_destroy; header("Location:index.php");//你想登出后用戶返回的頁面 exit; ?> |