By GokiSoft.com| 16:38 17/06/2020|
Học PHP

[Share Code] Tìm hiểu cookie trong PHP - Lớp T1907A

#cookie.php


<!DOCTYPE html>
<html>
<head>
	<title>Cookie Tutorial</title>
</head>
<body>
	<h1>Welcome to learn Cookie PHP</h1>

<?php
// var_dump($_COOKIE);
// Them du lieu vao cookie
// setcookie('age', '21', time()+24*60*60, '/')
// Xoa key trong cookie
// setcookie('age', '', time()-1000, '/')

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

?>
</body>
</html>


#login.php


<?php
if (isset($_COOKIE['status']) && $_COOKIE['status'] == 'success') {
	header('Location: welcome.php');
	die();
}

if (!empty($_POST)) {
	$uname = $pwd = '';
	if (isset($_POST['username'])) {
		$uname = $_POST['username'];
	}

	if (isset($_POST['password'])) {
		$pwd = $_POST['password'];
	}

	if ($uname == 'admin' && $pwd == '123') {
		setcookie('status', 'success', time()+30, '/');

		header('Location: welcome.php');
		die();
	}
}
?>

<!DOCTYPE html>
<html>
<head>
	<title>Registation Form * Form Tutorial</title>
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h2 class="text-center">Login</h2>
			</div>
			<div class="panel-body">
				<form method="post">
					<div class="form-group">
					  <label for="usr">User Name:</label>
					  <input required="true" type="text" class="form-control" id="usr" name="username">
					</div>
					<div class="form-group">
					  <label for="pwd">Password:</label>
					  <input required="true" type="password" class="form-control" id="pwd" name="password">
					</div>
					<button class="btn btn-success">Login</button>
				</form>
			</div>
		</div>
	</div>
</body>
</html>


#logout.php


<?php
setcookie('status', 'success', time()-30, '/');

header('Location: login.php');
die();


#welcome.php


<!DOCTYPE html>
<html>
<head>
	<title>Welcome</title>
</head>
<body>
	<h1>Welcome to login</h1>
	<a href="logout.php">Thoat</a>
</body>
</html>


Tags:

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

5

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