[PHP從小白到大牛]-024 PHP-會話控制實戰, 登陸註冊

邏輯圖

index.php

<?php 

session_start();

// var_dump($_SESSION);die;

// 判斷一下是否登陸, 若是沒有登陸, 跳轉回登陸頁
if(!isset($_COOKIE['username']) && !isset($_SESSION['username'])){
	header('location:login.php');
}
?>


<!DOCTYPE html>
<html>
	<head>
		<title>主頁</title>
		<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
	</head>
	<body>
		<div class="container">
			<nav class="navbar navbar-light bg-light">
				<a class="navbar-brand">
                    使用 Cookie 和 Session 實現會話控制
                </a>
                <a href="action.php?action=logout">
                    <button class="btn btn-outline-danger my-2 my-sm-0"
                            type="button">
                        註銷
                    </button>
                </a>
			</nav>
			<div class="d-flex justify-content-around mt-5">
				<div class="card col-5">
					<div class="card-body">
						<h5 class="card-title">
                            會話控制實戰內容一
                        </h5>
						<h6 class="card-subtitle mb-2 text-muted">
                            SESSION 部分
                        </h6>
						<p class="card-text">
                            實現用戶認證功能,用戶登陸、退出與身份識別
                        </p>
					</div>
				</div>
				<div class="card col-5">
					<div class="card-body">
						<h5 class="card-title">
                            會話控制實戰內容二
                        </h5>
						<h6 class="card-subtitle mb-2 text-muted">
                            COOKIE 部分
                        </h6>
						<p class="card-text">
                            實現登陸記住用戶功能,七天免登陸認證
                        </p>
					</div>
				</div>
			</div>
			<div class="d-flex justify-content-around mt-4">
				<div class="card col-5">
					<div class="card-body">
						<h5 class="card-title">
                            會話控制實戰內容一
                        </h5>
						<h6 class="card-subtitle mb-2 text-muted">
                            SESSION 部分
                        </h6>
						<p class="card-text">
                            實現用戶認證功能,用戶登陸、退出與身份識別
                        </p>
					</div>
				</div>
				<div class="card col-5">
					<div class="card-body">
						<h5 class="card-title">
                            會話控制實戰內容二
                        </h5>
						<h6 class="card-subtitle mb-2 text-muted">
                            COOKIE 部分
                        </h6>
						<p class="card-text">
                            實現登陸記住用戶功能,七天免登陸認證
                        </p>
					</div>
				</div>
			</div>
			<div class="d-flex justify-content-center mt-5">
				<div class="alert alert-light" role="alert">
					Powered by 雲和數據
				</div>
			</div>
		</div>
	</body>
</html>
複製代碼

login.php

<!DOCTYPE html>
<html>
	<head>
		<title>登錄頁</title>
		<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
	</head>
	<body>
		<div class="container">
			<div class="card col-12 mt-5">
				<div class="card-body">
					<h4 class="card-title">
                        用戶登陸
                    </h4>
					<div class="col-12 mt-4 d-flex justify-content-center">
						<form class="col-8" method="post" action="action.php">
                            <input type="hidden" name="action" value="login">
							<div class="form-group">
								<label for="username">用戶名</label>
								<input type="text"
                                       class="form-control"
                                       id="username"
                                       name="username"
                                       placeholder="請輸入用戶名">
							</div>
							<div class="form-group">
								<label for="password">密碼</label>
								<input type="password"
                                       class="form-control"
                                       id="password"
                                       name="password"
                                       placeholder="請輸入密碼">
							</div>
							<div class="form-group form-check">
								<input type="checkbox"
                                       class="form-check-input"
                                       id="remember"
                                       name="remember">
								<label class="form-check-label"
                                       for="remember">
                                    在這臺電腦上記住個人登陸狀態
                                </label>
							</div>
							<button type="submit"
                                    class="btn btn-primary">
                                登陸
                            </button>
						</form>
					</div>
				</div>
			</div>
			<div class="d-flex justify-content-center mt-5">
				<div class="alert alert-light" role="alert">
					Powered by 雲和數據
				</div>
			</div>
		</div>
	</body>
</html>
複製代碼

action.php

<?php

session_start();

switch ($_REQUEST['action']) {
	case 'login':
	login();

	break;
	case 'logout':
	logout();
}


function login(){

	$username = $_POST['username'];
	$password = $_POST['password'];
	if (array_key_exists('remember', $_POST)) {
		$remember = $_POST['remember'];
	}	else{
		$remember = false;
	}

	if ($username !== 'yunhe' || $password !== "123456") {
		echo "<script> alert('用戶名或密碼不正確!'); window.location='login.php'; </script>";
		return;
	}

	if($remember){
		setcookie('username',$username,time()+7*24*3600);
	}


	$_SESSION['username'] = $username;
	header("location:index.php");


}

function logout(){
	session_unset();
	setcookie('username','',time()-1);
	header('location:login.php');
}
複製代碼
相關文章
相關標籤/搜索