By GokiSoft.com| 09:39 15/04/2022|
Học PHP

[Video] Tìm hiểu về cookie trong PHP/MySQL - Tìm hiểu qua ví dụ quản lý đăng ký & đăng nhập PHP/MySQL

Tìm hiểu về cookie trong PHP/MySQL - Tìm hiểu qua ví dụ quản lý đăng ký & đăng nhập PHP/MySQL




#readme.txt


- Tạo 1 page: register.php -> lưu thông tin vào cookie
- Tạo 1 page: login.php -> click login -> lấy thông $_POST kiểm tra dữ liệu trong cookie <-> Khớp nhau -> login thành công | ngược lại -> failed


#cookie.php


<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Cookie Tutorial</title>
</head>
<body>
<?php
// Lưu ý: Cookie quản lý dữ liệu theo kiểu key & value
// B1. Thêm dữ liệu vào cookie
// Lưu thông tinh sinh viên vào cookie: fullname = TRAN VAN A, age = 22, email = a@gmail.com
setcookie('fullname', 'TRAN VAN A', time() + 60 * 60, "/");
setcookie('age', 22, time() + 60 * 60, "/");
setcookie('email', 'a@gmail.com', time() + 60 * 60, "/");

// B2. Lấy dữ liệu từ cookie ra
var_dump($_COOKIE);
// $fullname = $email = $age = "";
// if(isset($_COOKIE['fullname'])) {
// 	$fullname = $_COOKIE['fullname'];
// }
// if(isset($_COOKIE['email'])) {
// 	$email = $_COOKIE['email'];
// }
// if(isset($_COOKIE['age'])) {
// 	$age = $_COOKIE['age'];
// }
// echo $fullname.'-'.$email.'-'.$age;

// B3. Sửa thông tin nội dung cookie
// setcookie('fullname', 'OKOK', time() + 60 * 60, "/");

// B4. Xoa cookie đi như thế nào
// setcookie('fullname', '', time(), "/");
// setcookie('age', '', time(), "/");
// setcookie('email', '', time(), "/");
?>
</body>
</html>


#login.php


<?php
$email = $pwd = $msg = "";
$error = [
	'email' => '',
	'pwd' => ''
];

if(!empty($_POST)) {
	//Lấy thông tin gửi từ form login gửi lên server
	$email = $_POST['email'];
	$pwd = $_POST['pwd'];

	//Lấy thông tin đã lưu trữ trong cookie
	$cookieEmail = $cookiePwd = "";
	if(isset($_COOKIE['email'])) {
		$cookieEmail = $_COOKIE['email'];
	}
	if(isset($_COOKIE['pwd'])) {
		$cookiePwd = $_COOKIE['pwd'];
	}

	//Kiểm tra dữ liệu form gửi lên & cookie
	if($email == $cookieEmail && $pwd == $cookiePwd) {
		//Login thanh cong
		header('Location: show.php');
		die();
	} else {
		if($email != $cookieEmail) {
			$error['email'] = '<p style="color: red">Email khong chinh xac</p>';
		}

		if($pwd != $cookiePwd) {
			$error['pwd'] = '<p style="color: red">Pwd khong chinh xac</p>';
		}
		//Login failed
		// $msg = '<h1 style="text-align: center; color: red;">Đăng Nhập Thất Bại</h1>';
		// $msg = '<script type="text/javascript">
				// 	alert(\'Đăng Nhập Thất Bại\')
				// </script>';
	}
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Login Page</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<form method="post">
		<?=$msg?>
		<div class="form-group">
			<label>Email: </label>
			<input required type="email" name="email" class="form-control">
			<?=$error['email']?>
		</div>
		<div class="form-group">
			<label>Password: </label>
			<input required type="password" name="pwd" class="form-control">
			<?=$error['pwd']?>
		</div>
		<div class="form-group">
			<p>
				<a href="register.php">Create a new account</a>
			</p>
			<button class="btn btn-success">Login</button>
		</div>
	</form>
</div>
</body>
</html>


#register.php


<?php
$fullname = $birthday = $email = $address = $pwd = "";

if(!empty($_POST)) {
	$fullname = $_POST['fullname'];
	$birthday = $_POST['birthday'];
	$email = $_POST['email'];
	$address = $_POST['address'];
	$pwd = $_POST['pwd'];

	// dieu kien -> thoa man -> luu xuong cookie
	setcookie("fname", $fullname, time() + 7 * 24 * 60 * 60, "/");
	setcookie("birthday", $birthday, time() + 7 * 24 * 60 * 60, "/");
	setcookie("email", $email, time() + 7 * 24 * 60 * 60, "/");
	setcookie("address", $address, time() + 7 * 24 * 60 * 60, "/");
	setcookie("pwd", $pwd, time() + 7 * 24 * 60 * 60, "/");
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Register Page</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<form method="post">
		<div class="form-group">
			<label>Full Name: </label>
			<input required type="text" name="fullname" class="form-control">
		</div>
		<div class="form-group">
			<label>Birthday: </label>
			<input type="date" name="birthday" class="form-control">
		</div>
		<div class="form-group">
			<label>Email: </label>
			<input required type="email" name="email" class="form-control">
		</div>
		<div class="form-group">
			<label>Address: </label>
			<input type="text" name="address" class="form-control">
		</div>
		<div class="form-group">
			<label>Password: </label>
			<input required type="password" name="pwd" class="form-control">
		</div>
		<div class="form-group">
			<p>
				<a href="login.php">I have a account</a>
			</p>
			<button class="btn btn-success">Register</button>
		</div>
	</form>
</div>
</body>
</html>


#show.php


<?php
$fullname = $birthday = $email = $address = $pwd = "";

if(isset($_COOKIE['fname'])) {
	$fullname = $_COOKIE['fname'];
}
if(isset($_COOKIE['birthday'])) {
	$birthday = $_COOKIE['birthday'];
}
if(isset($_COOKIE['email'])) {
	$email = $_COOKIE['email'];
}
if(isset($_COOKIE['address'])) {
	$address = $_COOKIE['address'];
}
if(isset($_COOKIE['pwd'])) {
	$pwd = $_COOKIE['pwd'];
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Login Page</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<ul>
		<li>Full Name: <?=$fullname?></li>
		<li>Birthday: <?=$birthday?></li>
		<li>Email: <?=$email?></li>
		<li>Pwd: <?=$pwd?></li>
		<li>Address: <?=$address?></li>
	</ul>
</div>
</body>
</html>


Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó