天天看点

php案例: cookie实现自动登陆

浣???锛???涓?璐???涓轰?浜?涓?瀹?51cto(涓?瀹跺??涓????ヤ??? TOP绾?汉) ?块??浜?涓?瀹跺??涓?

??绔???褰?

  • ??????瑷?????
  • ????auto_login.php????
  • ????user_center.php????
  • ????logout.php????
  • ????????锛?????

??瑷?

auto_login.php

<!DOCTYPE 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">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
    <title>?ㄦ?风?婚??</title>

    <style>
        #showTime{
            width:291px;
            border:1px solid #ccc;
            line-height:23px;
            position:absolute;
            left:500px;
            top:48px;
            text-align:center;
        }
    </style>
</head>
<body>

<div id="login">

    <ul>
        <form id="myForm" name="myForm"  method="post" action='user_center.php'>
            <li>
                <tt>
                    <label id="log_type" for="email">???????</label>
                </tt>
                <div>
                    <input id="username" name="username" type="text" class="cell" value='<?php
                    echo $_COOKIE['username']?>'/>
                </div>
            </li>
            <li>
                <tt>
                    <label for="password">瀵?????:</label>
                </tt>
                <div>
                    <input id="password" name="password" type="password" class="cell"  value='<?php
                    echo $_COOKIE['userpwd']?>'/>//濡???cookie杩?瀛??ㄧ??璇?锛???杩?$_COOKIE['userpwd']??浠ヨ?峰??cookie???㈢??userpwd??                </div>
            </li>
            <li>
                <div>
                    <input id="auto_login" name="auto_login" type="checkbox" value="on"/>
                    <label for="auto_login">涓?娆¤???ㄧ?诲?</label>
                </div>
            </li>
            <li>
                <div>
                    <input id="sub" type="submit" value='??浜?/>
                </div>
            </li>
    </form>
</ul>
</div>

</body>
</html>      

user_center.php

<?php
  if(!empty($_POST['username'])){//?????ㄦ?峰??瀛??ㄧ??璇?锛??ц?if???㈢??浠g??
    if($_POST['username'] =='admin' && $_POST['password'] == 'admin'){
    //?ㄦ?峰??+瀵???==admin?ц?if???㈢??浠g??
      if($_POST['auto_login'] == 'on'){//==on浠h〃?逛????ㄧ?诲?checkbox
        setcookie('username',$_POST['username'],time()+3600*7*24);
        //绗?涓?涓????版??cookie????瀛???绗?浜?涓????版???ㄦ?峰??锛?绗?涓?涓????版??杩????堕??        setcookie('userpwd',$_POST['password'],time()+3600*7*24);
        echo "<script>alert('寮??????ㄧ?婚??????锛?');</script>";
      }
  ?>
    <div style='margin:auto;width:300px;height:30px;line-height:30px;text-align:center;color:#fff;background:#ccc;'>浣跨??ookie瀹??拌???ㄧ?婚??</div>
    <div style='margin:auto;width:300px;border:1px solid #ccc;height:30px;line-height:30px;text-align:center;color:blue;background:#fff;'>
    褰????ㄦ?凤?<?echo $_POST['username'];?>,<a href='auto_login.php'>杩???涓婚〉</a>
    </div>
    <?
    }
  }
  娉ㄩ??:echo "<a href='logout.php?logout=zhuxiao'>娉ㄩ???婚??</a>";
  ?>      

logout.php

<?php
if($_GET['logout']){
  setcookie('username','',time()-1);//绗?涓?涓????颁唬琛ㄨ????堕?翠负璐??般??灏遍??姣?cookie浜?
  setcookie('userpwd','',time()-1);//绗?涓?涓????版??褰????堕?磋?-1.浠h〃娌℃???堕??  echo "<script>alert('娉ㄩ??????!');location='auto_login.php';</script>";//杩???棣?椤?}
?>      

????锛?