php用戶登入與注銷(cookie)


登入界面

<?php
    header('Content-type:text/html;charset=utf-8');   
    if(isset($_COOKIE['username']) && $_COOKIE['username']==='zeng'){
        exit('您已經登入了,請不要重新登入');
    }

    if(isset($_POST['submit'])){
        if(isset($_POST['username']) && isset($_POST['password']) && $_POST['username']=='zengguanling' && $_POST['password']=='123456' ){
            if(setcookie('username',$_POST['username'],  time()+3600)){
                header('location:skip.php?url=index.php&info=登入成功!3秒后跳轉到首面');
            }  else {
                echo 'cookies設置失敗';
            }
        }  else {
            header('location:skip.php?url=login.php&info=對不起,用戶名活密碼填寫錯誤!3秒后跳轉到登入頁面');
        }
    }
?>
<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <title>請登入</title>
    </head>
    <body>
        <form method="post" action="">
            姓名:<input type="text" name="username" />
            密碼:<input type="password" name="password"/>
            <input type="submit" name="submit" value="登入"/>
        </form>
    </body>
</html>

跳轉處理頁面skip.php

<?php
    if(!isset($_GET['url']) || !isset($_GET['info'])){
        exit();
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="refresh" content="3,URL=<?php echo $_GET['url'] ?>"/>
        <title>正在跳轉中...</title>
    </head>
    <body>
        <div><?php echo $_GET['info'] ?></div>
    </body>
</html>

登入首頁index.php

<?php
    header('Content-type:text/html;charset=utf-8');    
    if(isset($_COOKIE['username']) && $_COOKIE['username']==='zeng'){
        echo "您好!{$_COOKIE['username']},歡迎回來!";
        echo "<a href='logout.php'>注銷</a>";
    }  else {
        echo "<a href='login.php'>請登入</a>";
    }
?>

注銷處理界面logout.php

<?php
    header('Content-type:text/html;charset=utf-8');
    if(isset($_COOKIE['username']) && $_COOKIE['username']==='zeng'){
        if(setcookie('username',$_POST['username'],time()-3600)){
            header('location:skip.php?url=index.php&info=注銷成功,正在跳轉!');
        }else{
            header('location:skip.php?url=index.php&info=注銷失敗,請稍后重試!');
        }
    }
?>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM