php+html實現用戶登錄退出


隨着滲透學習,逐漸意識到了學會開發也是非常重要的,僅僅是看懂感覺還是差了一些,所以寫一寫php的開發,這套程序目前並未有較完整的功能,之后會不斷進行完善

登錄頁面.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test1</title>
</head>
<body background="http://img4.imgtn.bdimg.com/it/u=1806795254,1936125128">
 <form action="login.php" method="post">
     <fieldset>
         <!--fieldest 組合表單中的相關元素 其中legend是該表中的標題-->
         <legend>用戶登錄</legend>
         <ul>
             <li>
                 <label>username:</label>
                 <input type="text" name="username">
             </li>
             <li>
                 <label>password:</label>
                 <input type="password" name="password">
             </li>
             <li>
                 <label></label>
                 <input type="checkbox" name="rem" value="yes">in the next week ,auto login
             </li>
             <li>
                 <label> </label>
                 <input type="submit" name="login" value="登錄">
             </li>
         </ul>
     </fieldset>
 </form>

 <!--鼠標特效-->
 <script src="https://blog-static.cnblogs.com/files/ophxc/shubiao.js"></script>
 <canvas width="1777" height="841" style="position: fixed; left: 0px; top: 0px; z-index: 2147483647; pointer-events: none;"></canvas>
 <!--鼠標特效 end-->


 <!-- Music-->
 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer@1.10.0/dist/APlayer.min.css">
 <script src="https://blog-static.cnblogs.com/files/zouwangblog/APlayer.min.js"></script>
 <script src="https://unpkg.com/meting@1.2/dist/Meting.min.js"></script>
 <div id="player" class="aplayer aplayer-withlist aplayer-fixed" data-id="2878443703" data-server="netease" data-type="playlist" data-order="random" data-fixed="true" data-listfolded="true" data-theme="orange"></div>
 <!-- Music end -->

 <img src="http://www.4kbizhi.com/d/file/2020/01/12/small00383295ltt1578760712.jpg" class="desc_img">
</body>
</html>

logout.php

<?php

header('Content-type:text/html; charset=utf-8');

session_start();

$username = $_SESSION['username'];  
$_SESSION = array();
session_destroy();

setcookie('username', '', time() - 99);
setcookie('code', '', time() - 99);

echo "歡迎下次光臨, " . $username . '<br>';
echo "<a href='登錄頁面.html'>重新登錄</a>";  

login.php

<?php
header('Content-type:text/html; charset=utf-8');

session_start();
highlight_file(login.php);
if(isset($_POST['username'])) {
    $username = trim($_POST['username']);
    #移除字符串兩側的空格
    $password = trim($_POST['password']);
    if (($username == "") || ($password == '')) {
        header('refresh:5; url=login.html');   # 若為空,視為未填寫,提示錯誤,並3秒后返回登錄界面
        echo "密碼或用戶名不能為空";
        exit;
    } else if (($username != 'username') && ($password != 'password')) {
        header('refresh:3;url=login.html');
        echo "用戶名或密碼錯誤";
        exit;
    } else if (($username = 'username') && ($password = 'password')) {
        #session存取
        $_SESSION['username'] = $username;
        $_SESSION['login'] = 1;
        if ($_POST['remember'] == "yes") {
            setcookie('username', $username, time() + 7 * 24 * 60 * 60); #設置cookie存在時間
            setcookie('code', md5($username . md5($password)), time() + 7 * 24 * 60 * 60);
        } else {
            setcookie('username', time() - 999);
            setcookie('code', time() - 999);
        }
        header('location:index.php');
    }
}
?>

index.php

<?php
header('Content-type:text/html; charset=utf-8');
session_start();

// 判斷Cookie
if (isset($_COOKIE['username'])) {
    # 若記住了用戶信息,則直接傳給Session
    $_SESSION['username'] = $_COOKIE['username'];
    $_SESSION['islogin'] = 1;
}
if (isset($_SESSION['islogin'])) {
    // 若已經登錄
    echo "welcome " . $_SESSION['username'] . '!<br>';
    echo "<a href='logout.php'>注銷</a>";
} else {
    // 若沒有登錄
    echo "fail!<a href='登錄頁面.html'>登錄</a>";
}
?>


免責聲明!

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



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