By GokiSoft.com| 19:59 07/05/2022|
Học PHP

[Source Code] Tìm hiểu cookie & ứng dụng trong thực tế - Khoá học PHP/MySQL - C2110L



#readme.txt


Nội dung kiến thức:
	- Khái niệm cookie là gì?
	- Ứng dụng cookie trong bài toán thực tế
		- register.php -> save cookie
		- login.php -> verify du lieu nhap vao & cookie -> khop nhau -> login thanh cong | failed


#vidu.php


<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Cookie in PHP</title>
</head>
<body>
<?php
//Gia su quan ly thong tin sach: title = 'LAP TRINH C', author = 'Aptech', price = 100000
// B1) Them noi dung du lieu vao cookie
// setcookie('title', 'LAP TRINH C', time() + 7*24*60*60, '/');
// setcookie('author', 'Aptech', time() + 7*24*60*60, '/');
// setcookie('price', '100000', time() + 7*24*60*60, '/');

// B2) Lay du lieu tu cookie ra
// var_dump($_COOKIE);
$title = $author = $price = '';
if(isset($_COOKIE['title'])) {
	$title = $_COOKIE['title'];
}
if(isset($_COOKIE['author'])) {
	$author = $_COOKIE['author'];
}
if(isset($_COOKIE['price'])) {
	$price = $_COOKIE['price'];
}
echo "$title - $author - $price";

// B3) Sua noi dung cookie
// setcookie('title', 'HTML/CSS/JS', time() + 7*24*60*60, '/');

// B4) Xoa noi dung cookie
// setcookie('price', '', time(), '/');

?>
</body>
</html>


#show.php


<?php
$fullname = $email = $pwd = '';

if(isset($_COOKIE['fullname'])) {
	$fullname = $_COOKIE['fullname'];
}

if(isset($_COOKIE['email'])) {
	$email = $_COOKIE['email'];
}

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>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}

		.card {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<table class="table table-bordered">
		<tr>
			<td>Full Name</td>
			<td><?=$fullname?></td>
		</tr>
		<tr>
			<td>Email</td>
			<td><?=$email?></td>
		</tr>
		<tr>
			<td>Password</td>
			<td><?=$pwd?></td>
		</tr>
	</table>
</div>
</body>
</html>


#register.php


<?php
if(!empty($_POST)) {
	$fullname = $_POST['fullname'];
	$email = $_POST['email'];
	$pwd = $_POST['pwd'];

	//Luu xuong cookie
	setcookie('fullname', $fullname, time() + 7*24*60*60, '/');
	setcookie('email', $email, 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>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}

		.card {
			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>Email: </label>
			<input required type="email" name="email" 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>


#login.php


<?php
if(!empty($_POST)) {
	$email = $_POST['email'];
	$pwd = $_POST['pwd'];

	//verify du lieu luu trong cookie
	$cookieEmail = $cookiePwd = "";
	if(isset($_COOKIE['email'])) {
		$cookieEmail = $_COOKIE['email'];
	}
	if(isset($_COOKIE['pwd'])) {
		$cookiePwd = $_COOKIE['pwd'];
	}

	if($email == $cookieEmail && $pwd == $cookiePwd) {
		// echo 'Login thanh cong';
		header('Location: show.php');
		die();
	} else {
		echo 'Login failed';
	}
}
?>

<!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>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}

		.card {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<form method="post">
		<div class="form-group">
			<label>Email: </label>
			<input required type="email" name="email" 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="register.php">Create new a account</a>
			</p>
			<button class="btn btn-success">Register</button>
		</div>
	</form>
</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 đó