本文講述了PHP實現用戶登錄的案例代碼。分享給大家供大家參考,具體如下:
需求分析:
在Web應用開發中,經常需要實現用戶登錄的功能。假設一個名為username的用戶,當該用戶進入網站首頁時,如果還未登錄,則頁面會提示登錄,用戶輸入登錄信息進行驗證,驗證通過進入用戶中心,否則顯示用戶名或密碼錯誤,重新登錄。登錄成功后,用戶還可以單擊“注銷”,回到登錄頁面。
程序設計流程圖:
login.html
!DOCUMENT html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http ://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd html xmlns= http://www.w3.org/1999/xhtml xml:lang= en head meta http-equiv= Content-Type content= text/html;charset=UTF-8 / title 會員登錄 /title style type= text/css ul li{margin:0;padding:0;} form{margin:40px 30px 0;} form li{list-style:none;padding:5px 0;} form li label{float:left;width:70px;text-align:right} form li a{font-size:12px;color:#999;text-decoration:none} .login_btn{border:none;background:#01A4F1;color:#fff;font-size:14px;font-weight:bold;height:28px;line-height:28px;padding:0 10px;cursor:pointer;} form li img{vertical-align:top} /style /head body form action= login.php method= POST fieldset legend 用戶登錄 /legend label for 用戶名: /label input type= text name= username / /li label for 密碼: /label input type= password name= password / /li label for /label input type= checkbox name= remember value= yes / 7天內自動登錄 /li label for /label input type= submit name= login value= 登錄 >
login.php
?php header( Content-Type:text/html;charset=utf-8 session_start(); if(isset($_POST[ login ])) $username = trim($_POST[ username $password = trim($_POST[ password if(($username== )||($password== )) header( refresh:3;url=login.html echo 改用戶名或密碼不能為空,3秒后跳轉到登錄頁面 exit; else if(($username!= username )||($password!= password )) //用戶名或密碼錯誤 header( refresh:3;url=login.html echo 用戶名或密碼錯誤,3秒后跳轉到登錄頁面 exit; else if(($username== username ) ($password== password )) //登錄成功將信息保存到session中 $_SESSION[ username ]=$username; $_SESSION[ islogin ]=1; //如果勾選7天內自動保存,則將其保存到cookie if($_POST[ remember ]== yes ) setcookie( username ,$username,time()+7*24*60*60); setcookie( code ,md5($username.md5($password)),time()+7*24*60*60); else setcookie( username , ,time()-1); setcookie( code , ,time()-1); //跳轉到用戶首頁 header( refresh:3;url=index.php ?
index.php
?php header( Content-Type:text/html;charset=utf-8 session_start(); //首先判斷Cookie是否有記住用戶信息 if(isset($_COOKIE[ username ])) $_SESSION[ username ]=$_COOKIE[ username $_SESSION[ islogin ]=1; if(isset($_SESSION[ islogin ])) //已經登錄 echo $_SESSION[ username ]. :你好,歡迎進入個人中心! br/ echo a href= logout.php 注銷 /a else { //為登錄 echo 你還未登錄,請 a href= login.html 登錄 /a ?
logout.php
?php header( Content-Type:text/html;charset=utf-8 session_start(); //清除session $username=$_SESSION[ username $_SESSION=array(); session_destroy(); //清除cookie setcookie( username , ,time()-1); setcookie( code , ,time()-1); echo $username,歡迎下次光臨 echo 重新 a href= login.html 登錄 /a ?
登錄界面:
以上所述是小編給大家介紹的PHP實現用戶登錄的案例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對php 的支持!
您可能感興趣的文章:
ThinkPHP防止重復提交表單的方法實例分析php實例
PHP使用PDO抽象層獲取查詢結果的方法示例php技巧
PHP分頁顯示的方法分析【附PHP通用分頁類php技巧
以上就是PHP實現用戶登錄的案例代碼php實例的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答