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

[Video] Tìm hiểu về cookie trong PHP/MySQL - Khóa học PHP/MySQL

#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
// B1) Thêm nội dung vào cookie
// Lưu thông tin quyển sách vào cookie: title = Lap Trinh C, author: TRAN VAN DIEP, price = 100000
setcookie("title", "Lap Trinh C", time() + 600, "/");
setcookie("author", "TRAN VAN DIEP", time() + 600, "/");
setcookie("price", 100000, time() + 600, "/");

// B2) Lấy thông tìn 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) Sửa thông tin cookie
setcookie("title", "HTML/CSS/JS", time() + 600, "/");

// B4) Xóa thông tin cookie
setcookie("title", "HTML/CSS/JS", time(), "/");

?>
</body>
</html>


#login.php


<?php
$email = $pwd = "";

if(!empty($_POST)) {
	//Lay du lieu gui tu client len server
	$email = $_POST['email'];
	$pwd = $_POST['pwd'];

	//Lay thong tin nguoi dung tu cookie
	$emailCookie = $pwdCookie = "";
	if(isset($_COOKIE['email'])) {
		$emailCookie = $_COOKIE['email'];
	}
	if(isset($_COOKIE['pwd'])) {
		$pwdCookie = $_COOKIE['pwd'];
	}

	if($email == $emailCookie && $pwdCookie == $pwd) {
		//Login thanh cong
		// echo 'Dang nhap thanh cong';
		header('Location: show.php');
		die();
	} else {
		//Login fail
		echo 'Dang nhap that bai';
	}
}
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Register PHP</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>Email: </label>
			<input type="email" name="email" class="form-control">
		</div>
		<div class="form-group">
			<label>Password: </label>
			<input type="password" name="pwd" class="form-control">
		</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>


#readme.txt


Nội dung học:
	- Tìm hiểu lý thuyết
	- Ứng dụng:
		- Đăng ký tài khoản -> register.php -> lưu thông tin xuống cookie
		- Đăng nhập -> login.php -> lấy dữ liệu từ POST & lấy dữ liệu từ Cookie -> check xem thông tin khớp nhau không ? logn thành công -> show.php | ngược lại -> Hiển thị lỗi
		- Lấy thông tài khoản
			- Đọc thông tin từ cookie -> Hiển thị lên web thôi?


#register.php


<?php
$fullname = $username = $email = $pwd = "";

if(!empty($_POST)) {
	//Lay du lieu gui tu client len server
	$fullname = $_POST['fullname'];
	$username = $_POST['username'];
	$email = $_POST['email'];
	$pwd = $_POST['pwd'];

	//Luu thong tin xuong coookie
	setcookie('fname', $fullname, time() + 7*24*60*60, '/');
	setcookie('username', $username, 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 PHP</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 type="text" name="fullname" class="form-control">
		</div>
		<div class="form-group">
			<label>User Name: </label>
			<input type="text" name="username" class="form-control">
		</div>
		<div class="form-group">
			<label>Email: </label>
			<input type="email" name="email" class="form-control">
		</div>
		<div class="form-group">
			<label>Password: </label>
			<input 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 = $username = $email = $pwd = "";

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

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

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>Profile Page</title>
</head>
<body>
<ul>
	<li>Full Name: <?=$fullname?></li>
	<li>User Name: <?=$username?></li>
	<li>Email: <?=$email?></li>
	<li>Pwd: <?=$pwd?></li>
</ul>
</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 đó