By GokiSoft.com|
19:38 17/05/2022|
Học PHP
[Source Code] 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 - Session - C2110L
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
#login.php
<?php
$email = $pwd = $msg = "";
if(!empty($_POST)) {
//Lay du lieu gui tu client len server
$email = $_POST['email'];
$pwd = $_POST['pwd'];
//Luu thong tin xuong cookie
$cookieEmail = $cookiePwd = "";
if(isset($_COOKIE['email'])) {
$cookieEmail = $_COOKIE['email'];
}
if(isset($_COOKIE['pwd'])) {
$cookiePwd = $_COOKIE['pwd'];
}
if($email == $cookieEmail && $pwd == $cookiePwd) {
header('Location: welcome.php');
die();
} else {
$msg = "Login failed!!!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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">
<h1 style="text-align: center; color: red;"><?=$msg?></h1>
<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 a new account</a></p>
<button class="btn btn-success">Login</button>
</div>
</form>
</div>
</body>
</html>
#register.php
<?php
$fullname = $email = $pwd = $address = "";
if(!empty($_POST)) {
//Lay du lieu gui tu client len server
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$pwd = $_POST['pwd'];
$address = $_POST['address'];
//Luu thong tin 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, '/');
setcookie('address', $address, time() + 7*24*60*60, '/');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Register Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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>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">
<label>Address: </label>
<input required type="text" name="address" 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>
#welcome.php
<?php
$fullname = $email = $address = "";
if(isset($_COOKIE['fullname'])) {
$fullname = $_COOKIE['fullname'];
}
if(isset($_COOKIE['email'])) {
$email = $_COOKIE['email'];
}
if(isset($_COOKIE['address'])) {
$address = $_COOKIE['address'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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">
<table class="table table-bordered">
<thead>
<tr>
<th>Full Name</th>
<td><?=$fullname?></td>
</tr>
<tr>
<th>Email</th>
<td><?=$email?></td>
</tr>
<tr>
<th>Address</th>
<td><?=$address?></td>
</tr>
</thead>
</table>
</div>
</body>
</html>
#test.php
<?php
session_start();
// Luu thong tin
// $_SESSION['fullname'] = 'TRAN VAN B';
// $_SESSION['address'] = 'Ha Noi';
// Lay thong tin
var_dump($_SESSION);
// echo $_SESSION['fullname'];
// Sua thong tin
// $_SESSION['fullname'] = 'OKOK';
// Xoa du lieu
// unset($_SESSION['fullname']);
// session_destroy();
// $_SESSION = [];
?>
<!DOCTYPE html>
<html>
<head>
<title>Session PHP</title>
<meta charset="utf-8">
</head>
<body>
<h1>Welcome to learn Session</h1>
</body>
</html>
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![GokiSoft.com [Teacher]](https://www.gravatar.com/avatar/fc6ba9324e017d540af3613b3a77dd21.jpg?s=80&d=mm&r=g)
GokiSoft.com
2022-05-17 14:03:23
#welcome.php
<?php
session_start();
$fullname = $email = $address = "";
if(isset($_SESSION['fullname'])) {
$fullname = $_SESSION['fullname'];
}
if(isset($_SESSION['email'])) {
$email = $_SESSION['email'];
}
if(isset($_SESSION['address'])) {
$address = $_SESSION['address'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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">
<table class="table table-bordered">
<thead>
<tr>
<th>Full Name</th>
<td><?=$fullname?></td>
</tr>
<tr>
<th>Email</th>
<td><?=$email?></td>
</tr>
<tr>
<th>Address</th>
<td><?=$address?></td>
</tr>
</thead>
</table>
</div>
</body>
</html>
#register.php
<?php
session_start();
// var_dump($_SESSION);
$fullname = $email = $pwd = $address = "";
if(!empty($_POST)) {
//Lay du lieu gui tu client len server
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$pwd = $_POST['pwd'];
$address = $_POST['address'];
//Luu thong tin 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, '/');
// setcookie('address', $address, time() + 7*24*60*60, '/');
$_SESSION['fullname'] = $fullname;
$_SESSION['email'] = $email;
$_SESSION['pwd'] = $pwd;
$_SESSION['address'] = $address;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Register Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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>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">
<label>Address: </label>
<input required type="text" name="address" 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
session_start();
$email = $pwd = $msg = "";
if(!empty($_POST)) {
//Lay du lieu gui tu client len server
$email = $_POST['email'];
$pwd = $_POST['pwd'];
//Luu thong tin xuong cookie
$sEmail = $sPwd = "";
if(isset($_SESSION['email'])) {
$sEmail = $_SESSION['email'];
}
if(isset($_SESSION['pwd'])) {
$sPwd = $_SESSION['pwd'];
}
if($email == $sEmail && $pwd == $sPwd) {
header('Location: welcome.php');
die();
} else {
$msg = "Login failed!!!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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">
<h1 style="text-align: center; color: red;"><?=$msg?></h1>
<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 a new account</a></p>
<button class="btn btn-success">Login</button>
</div>
</form>
</div>
</body>
</html>