By GokiSoft.com|
20:23 28/02/2022|
Học PHP
[Video]Sử dụng cookie trong PHP - quản lý đăng ký & đăng nhập tài khoản trong PHP - Lập Trình PHP - C2108L
Sử dụng cookie trong PHP - quản lý đăng ký & đăng nhập tài khoản trong PHP - Lập Trình PHP
#process_login.php
<?php
$msg = "";
if(!empty($_POST)) {
$email = $pwd = "";
if(isset($_POST['email'])) {
$email = $_POST['email'];
}
if(isset($_POST['pwd'])) {
$pwd = $_POST['pwd'];
}
// Lay noi dung luu tru trong cookie
$emailCookie = $pwdCookie = "";
if(isset($_COOKIE['email'])) {
$emailCookie = $_COOKIE['email'];
}
if(isset($_COOKIE['pwd'])) {
$pwdCookie = $_COOKIE['pwd'];
}
if($email == $emailCookie && $pwd == $pwdCookie) {
//login thanh cong -> chuyen sang trang welcome
header('Location: welcome.php');
die();
}
$msg = "Danh Nhap That Bai";
}
#process_register.php
<?php
$fullname = $email = $address = $pwd = "";
if(!empty($_POST)) {
if(isset($_POST['fullname'])) {
$fullname = $_POST['fullname'];
}
if(isset($_POST['email'])) {
$email = $_POST['email'];
}
if(isset($_POST['address'])) {
$address = $_POST['address'];
}
if(isset($_POST['pwd'])) {
$pwd = $_POST['pwd'];
}
setcookie('fullname', $fullname, time() + 24 * 60 * 60, '/');
setcookie('email', $email, time() + 24 * 60 * 60, '/');
setcookie('address', $address, time() + 24 * 60 * 60, '/');
setcookie('pwd', $pwd, time() + 24 * 60 * 60, '/');
// Tu dong chuyen sang trang login.php
header('Location: login.php');
die();
}
#process_welcome.php
<?php
$fullname = $email = $address = $pwd = "";
if(isset($_COOKIE['fullname'])) {
$fullname = $_COOKIE['fullname'];
}
if(isset($_COOKIE['email'])) {
$email = $_COOKIE['email'];
}
if(isset($_COOKIE['address'])) {
$address = $_COOKIE['address'];
}
if(isset($_COOKIE['pwd'])) {
$pwd = $_COOKIE['pwd'];
}
#login.php
<?php
require_once('form/process_login.php');
// include_once('form/process_login.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Page</title>
<!-- Bootstrap -> thiet ke GUI -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<style type="text/css">
h2 {
text-align: center;
}
.form-group {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<h2>Login Form</h2>
<h2 style="color: red"><?=$msg?></h2>
<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>Mat Khau: </label>
<input required type="password" name="pwd" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-success">Dang Nhap</button>
</div>
</form>
</div>
</body>
</html>
#register.php
<?php
require_once('form/process_register.php');
// include_once('form/process_register.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Register Page</title>
<!-- Bootstrap -> thiet ke GUI -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<style type="text/css">
h2 {
text-align: center;
}
.form-group {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<h2>Register Form</h2>
<form method="post" onsubmit="return validateData();">
<div class="form-group">
<label>Ho & Ten: </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>Dia Chi: </label>
<input required type="text" name="address" class="form-control">
</div>
<div class="form-group">
<label>Mat Khau: </label>
<input required type="password" name="pwd" class="form-control">
</div>
<div class="form-group">
<label>Xac Thuc Mat Khau: </label>
<input required type="password" name="confirmPwd" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-success">Dang Ky</button>
<button class="btn btn-warning" type="reset">Nhap Lai</button>
</div>
</form>
</div>
<script type="text/javascript">
function validateData() {
if($('[name=pwd]').val() != $('[name=confirmPwd]').val()) {
alert('Mat khau khong khop, vui long kiem tra thong tin nhap')
return false
}
return true
}
</script>
</body>
</html>
#test.php
<?php
include_once('test2.php');
include_once('test2.php'); //ignore
include_once('test2.php'); //ignore
include_once('test2.php'); //ignore
include_once('test2.php'); //ignore
include('test2.php');
include('test2.php');
include('test2.php');
include('test2.php');
include('test2.php');
#test2.php
<?php
echo '<br/>Hello World!!!';
#welcome.php
<?php
require_once('form/process_welcome.php');
// include_once('form/process_welcome.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Welcome Page</title>
<style type="text/css">
h1, h2 {
text-align: center;
}
</style>
</head>
<body>
<h1>Ho & Ten: <font color=RED><?=$fullname?></font></h1>
<h1>Email: <font color=RED><?=$email?></font></h1>
<h1>Dia Chi: <font color=RED><?=$address?></font></h1>
<h1>Mat Khau: <font color=RED><?=$pwd?></font></h1>
<h2>Dang Nhap Thanh Cong</h2>
</body>
</html>
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)