By GokiSoft.com|
20:02 12/01/2024|
Học PHP
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
Viết chương trình thực hiện các chức năng sau
- Thiết kế trang đăng ký tài khoản gồm các trường : họ tên, email, password, địa chỉ
-> khi người dùng click đăng ký => thêm thông tin vừa đăng ký vào cookie
- Thiết kế trang đăng nhập => gồm 2 trương email và mật khẩu => khi người dùng vào thông tin đúng vs thông tin trang đăng ký => chuyển sang trang welcome
Login sai thì hiển thị thông báo lỗi trên trang đăng nhập.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Trần Mạnh Dũng [T1907A]](https://www.gravatar.com/avatar/ee4661d1f9133c241d6c8287c7ea8ceb.jpg?s=80&d=mm&r=g)
Trần Mạnh Dũng
2020-06-19 07:01:12
#register.php
<?php
if (!empty($_POST['fullname'])){
if(isset($_POST['fullname'])){
$fullname = $_POST['fullname'];
}
if(isset($_POST['email'])){
$email = $_POST['email'];
}
if(isset($_POST['password'])){
$password = $_POST['password'];
}
if(isset($_POST['address'])){
$address = $_POST['address'];
}
setcookie('fullname',$fullname,time()+3600,'/');
setcookie('email',$email,time()+3600,'/');
setcookie('password',$password,time()+3600,'/');
setcookie('address',$address,time()+3600,'/');
echo "Register Success";
header('Location: login.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Register</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 pt-5">Register</h2>
</div>
<div class="panel-body">
<form action="login.php" method="post">
<div class="form-group">
<label for="usr">User Name:</label>
<input required="true" type="text" class="form-control" id="usr" name="fullname">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input required="true" type="email" class="form-control" id="email" name="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input required="true" type="password" class="form-control" id="pwd" name="password">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" name="address">
</div>
<button class="btn btn-success">Register</button>
</form>
</div>
</div>
</div>
</body>
</html>
#login.php
<?php
var_dump($_COOKIE);
if(!empty($_POST)){
$fullname = $_POST['fullname'];
$password = $_POST['password'];
//echo $email.$password;
$cookieFullname = $_COOKIE['fullname'];
$cookiePass = $_COOKIE['password'];
//echo '<br/>'.$cookieFullname.$cookiePass;
if($fullname == $cookieFullname && $password == $cookiePass){
header('Location: welcome.php');
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</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 pt-5">Login</h2>
</div>
<div class="panel-body">
<form action="" method=" post">
<div class="form-group">
<label for="usr">User Name:</label>
<input required="true" type="text" class="form-control" id="usr" name="fullname">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input required="true" type="password" class="form-control" id="pwd" name="password">
</div>
<button name="login" class="btn btn-success">Login</button>
</form>
</div>
</div>
</div>
</body>
</html>
#welcome.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2>savbksajbvka</h2>
</body>
</html>
![Luong Dinh Dai [T1907A]](https://www.gravatar.com/avatar/ca08fa4090e1038e541197564747f93c.jpg?s=80&d=mm&r=g)
Luong Dinh Dai
2020-06-19 05:01:40
#register.php
<!DOCTYPE html>
<?php
require_once('Dangki.php');
?>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form action="" method="POST">
<div class="form-group">
<label for="fullname">Ho Ten:</label>
<input type="text" class="form-control" name="fullname" id="fullname">
</div>
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" name="email" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" name="password" id="password">
</div>
<div class="form-group">
<label for="address">Address</label>
<input type="text" class="form-control" name="address" id="address">
</div>
<button type="submit" id="insertData" class="btn btn-default">Register</button>
</form>
<p id="showdata"></p>
</div>
</body>
</html>
#wellcome.php
<!DOCTYPE html>
<html>
<head>
<title>WellCome Login Success</title>
</head>
<body>
<h1>WellCome Login Success</h1>
</head>
</body>
</html>
#Dangki.php
<?php
var_dump($_COOKIE);
if (!empty($_POST['fullname'])){
if(isset($_POST['fullname'])){
$fullname = $_POST['fullname'];
}
if(isset($_POST['email'])){
$email = $_POST['email'];
}
if(isset($_POST['password'])){
$password = $_POST['password'];
}
if(isset($_POST['address'])){
$address = $_POST['address'];
}
setcookie('fullname',$fullname,time()+3600,'/');
setcookie('email',$email,time()+3600,'/');
setcookie('password',$password,time()+3600,'/');
setcookie('address',$address,time()+3600,'/');
echo "Dang ki thanh cong";
header('Location: login.php');
}
?>
#DangNhap.php
<?php
//var_dump($_COOKIE);
if(!empty($_POST)){
$email = $_POST['email'];
$password = $_POST['password'];
echo $email.$password;
$cookieEmail = $_COOKIE['email'];
$cookiePass = $_COOKIE['password'];
echo '<br/>'.$cookieEmail.$cookiePass;
// echo "co vao day";
if($email == $cookieEmail && $password == $cookiePass){
header('Location: wellcome.php');
}
}
?>
#login.php
<!DOCTYPE html>
<?php
require_once('DangNhap.php');
?>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form action="" method="POST">
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" name="email" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" name="password" id="password">
</div>
<button type="submit" id="submit" class="btn btn-default">Login</button>
</form>
</div>
</body>
</html>
![Trần Ngọc Hải [T1907A]](https://www.gravatar.com/avatar/9f7a27af52cbf0d52c32019e1354d60a.jpg?s=80&d=mm&r=g)
Trần Ngọc Hải
2020-06-19 02:24:34
Login.php
<?php
if (isset($_COOKIE['status']) && $_COOKIE['status'] == 'success') {
header('Location: welcome.php');
die();
}
if (!empty($_POST)) {
$CheckEmail = $CheckPassword = '';
if (isset($_POST['fullname'])) {
setcookie('fullname', $_POST['fullname'], time() + 500, '/');
}
if (isset($_POST['email'])) {
setcookie('email', $_POST['email'], time() + 500, '/');
}
if (isset($_POST['address'])) {
setcookie('address', $_POST['address'], time() + 500, '/');
}
if (isset($_POST['password'])) {
setcookie('password', $_POST['password'], time() + 500, '/');
}
if (isset($_POST['con_pass'])) {
setcookie('con_pass', $_POST['con_pass'], time() + 500, '/');
}
if (isset($_POST['CheckEmail']) && isset($_POST['CheckPassword'])) {
if (($_COOKIE['email'] == $_POST['CheckEmail']) && ($_COOKIE['password'] == $_POST['CheckPassword'])) {
setcookie('status', 'success', time() + 500, '/');
header('Location: welcome.php');
die();
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login form </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">Email :</label>
<input required="true" type="email" class="form-control" id="usr" name="CheckEmail">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input required="true" type="password" class="form-control" id="pwd" name="CheckPassword">
</div>
<button class="btn btn-success">Login</button>
</form>
</div>
</div>
</div>
</body>
</html>
Register.php<!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">Registation Form - Input User's Detail Information</h2>
</div>
<div class="panel-body">
<form method="post" action="login.php">
<div class="form-group">
<label for="usr">Full Name:</label>
<input required="true" type="text" class="form-control" id="usr" name="fullname">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input required="true" type="email" class="form-control" id="email" name="email">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" name="address">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input required="true" type="password" class="form-control" id="pwd" name="password">
</div>
<div class="form-group">
<label for="confirmation_pwd">Confirmation Password:</label>
<input required="true" type="password" class="form-control" id="con_pass" name="con_pass">
</div>
<button class="btn btn-success">Register</button>
</form>
</div>
</div>
</div>
</body>
</html>
cookie.php<?php
setcookie('status', 'success', time()-500, '/');
header('Location: login.php');
die();
welcome.php<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome</h1>
</body>
</html>
![Phí Văn Long [T1907A]](https://www.gravatar.com/avatar/5db166b7b74443c5c221e4c0068d6da9.jpg?s=80&d=mm&r=g)
Phí Văn Long
2020-06-18 16:43:20
<?php
setcookie('status', 'success', time()-300, '/');
header('Location: login.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">Registation Form - Input User's Detail Information</h2>
</div>
<div class="panel-body">
<form method="post" action="login.php">
<div class="form-group">
<label for="usr">Full Name:</label>
<input required="true" type="text" class="form-control" id="usr" name="fullname">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input required="true" type="email" class="form-control" id="email" name="email">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" name="address">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input required="true" type="password" class="form-control" id="pwd" name="password">
</div>
<div class="form-group">
<label for="confirmation_pwd">Confirmation Password:</label>
<input required="true" type="password" class="form-control" id="confirmation_pwd" name="confirmation_pwd">
</div>
<button class="btn btn-success">Register</button>
</form>
</div>
</div>
</div>
</body>
</html>
<?php
if (isset($_COOKIE['status']) && $_COOKIE['status'] == 'success') {
header('Location: welcome.php');
die();
}
if (!empty($_POST)) {
$CheckEmail = $CheckPassword = '';
if (isset($_POST['fullname'])) {
setcookie('fullname', $_POST['fullname'], time() + 300, '/');
}
if (isset($_POST['email'])) {
setcookie('email', $_POST['email'], time() + 300, '/');
}
if (isset($_POST['address'])) {
setcookie('address', $_POST['address'], time() + 300, '/');
}
if (isset($_POST['password'])) {
setcookie('password', $_POST['password'], time() + 300, '/');
}
if (isset($_POST['confirmation_pwd'])) {
setcookie('confirmation_pwd', $_POST['confirmation_pwd'], time() + 300, '/');
}
if (isset($_POST['CheckEmail']) && isset($_POST['CheckPassword'])) {
if (($_COOKIE['email'] == $_POST['CheckEmail']) && ($_COOKIE['password'] == $_POST['CheckPassword'])) {
setcookie('status', 'success', time() + 300, '/');
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">Email :</label>
<input required="true" type="email" class="form-control" id="usr" name="CheckEmail">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input required="true" type="password" class="form-control" id="pwd" name="CheckPassword">
</div>
<button class="btn btn-success">Login</button>
</form>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>HII</title>
</head>
<body>
<h1>Heloo</h1>
<a href="logout.php">Log out</a>
</body>
</html>
![thienphu [T1907A]](https://www.gravatar.com/avatar/c4573ea65e411176c1852fd8584f1ab1.jpg?s=80&d=mm&r=g)
thienphu
2020-06-18 09:44:47
#actionRegister.php
<?php
if (!empty($_POST['fullname'])){
if(isset($_POST['fullname'])){
$fullname = $_POST['fullname'];
}
if(isset($_POST['email'])){
$email = $_POST['email'];
}
if(isset($_POST['password'])){
$password = $_POST['password'];
}
if(isset($_POST['address'])){
$address = $_POST['address'];
}
setcookie('fullname',$fullname,time()+3600,'/');
setcookie('email',$email,time()+3600,'/');
setcookie('password',$password,time()+3600,'/');
setcookie('address',$address,time()+3600,'/');
//echo $fullname.$email.$password.$address;
echo "Dang ki thanh cong";
}
?>
#checkLogin.php
<?php
if (!empty($_POST)){
$email = $_POST['email'];
$password = $_POST['password'];
$cookieEmail = $_COOKIE['email'];
$cookiePass = $_COOKIE['password'];
if($email == $cookieEmail && $password == $cookiePass){
echo 1;
}else{
echo 0;
}
}
?>
#login.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form action="" method="POST">
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" name="email" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" name="password" id="password">
</div>
<button type="submit" id="submit" class="btn btn-default">Login</button>
</form>
</div>
<script type="text/javascript">
$('#submit').on('click',function(){
var email = $('#email').val()
var password = $('#password').val()
if(email==''|| password==''){
alert('nhập đầy đủ các trường')
}else{
// alert(email+password)
$.post('checkLogin.php',
{
email:email,
password:password
}
,function(data){
if(data==1){
alert("login sucess")
window.location.href = "wellcome.php";
}else{
alert("login faild")
}
}
)
}
})
</script>
</body>
</html>
#register.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form action="" method="POST">
<div class="form-group">
<label for="fullname">Ho Ten:</label>
<input type="text" class="form-control" name="fullname" id="fullname">
</div>
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" name="email" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" name="password" id="password">
</div>
<div class="form-group">
<label for="address">Address</label>
<input type="text" class="form-control" name="address" id="address">
</div>
<button type="submit" id="insertData" class="btn btn-default">Register</button>
</form>
<p id="showdata"></p>
</div>
<script type="text/javascript">
$('#insertData').on('click',function(){
var fullname = $('#fullname').val();
var email = $('#email').val();
var password = $('#password').val();
var address = $('#address').val();
if(fullname =='' || email== ''|| password==''||address==''){
alert('insert full data')
}
else{
// alert(fullname+email+password+address)
$.post( 'actionRegister.php',
{
fullname: fullname,
email:email,
password:password,
address:address
}
, function(data) {
alert(data)
window.location.href = "login.php";
})
}
})
</script>
</body>
</html>
#wellcome.php
<!DOCTYPE html>
<html>
<head>
<title>WellCome Login Success</title>
</head>
<body>
<h1>WellCome Login Success</h1>
</head>
</body>
</html>
![Đỗ Văn Huấn [T1907A]](https://www.gravatar.com/avatar/04c40301dd027839d265b3c3c9dc6e6b.jpg?s=80&d=mm&r=g)
Đỗ Văn Huấn
2020-06-18 03:30:48
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-latest.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.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.5.0/js/bootstrap.min.js"></script>
<title>Document</title>
</head>
<body>
<div class="container">
<form method="post" action="login.php">
<div class="form-group">
<label for="name">
<h6>Full name: </h6>
</label>
<input type="text" name="name" class="form-control" placeholder="Enter name" id="name">
</div>
<div class="form-group">
<label for="address">
<h6>Address: </h6>
</label>
<input type="text" name="address" class="form-control" placeholder="Enter address" id="address">
</div>
<div class="form-group">
<label for="user">
<h6>Username: </h6>
</label>
<input type="text" name="user" class="form-control" placeholder="Enter username" id="user">
</div>
<div class="form-group">
<label for="pass">
<h6>password: </h6>
</label>
<input type="password" name="pass" class="form-control" placeholder="Enter pass" id="pass">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</body>
</html>
<?php
if (isset($_COOKIE['style']) && $_COOKIE['style'] == 'success') {
header('Location: welcome.php');
die();
}
$check_user = $check_pass = $option_check = $option_pass = $option_user = '';
if (!empty($_POST)) {
if (isset($_POST['name'])) {
setcookie('fullname', $_POST['name'], time() + 180, '/');
}
if (isset($_POST['address'])) {
setcookie('address', $_POST['address'], time() + 180, '/');
}
if (isset($_POST['user'])) {
setcookie('user', $_POST['user'], time() + 180, '/');
}
if (isset($_POST['pass'])) {
setcookie('pass', $_POST['pass'], time() + 180, '/');
}
}
if (!empty($_GET)) {
if (isset($_GET['check-user']) && isset($_GET['check-pass'])) {
if (($_COOKIE['user'] == $_GET['check-user']) && ($_COOKIE['pass'] == $_GET['check-pass'])) {
setcookie('style', 'success', time() + 180, '/');
header('Location: welcome.php');
die();
} else {
$option_check = '<p style="color:red;">User or password entered incorre</p>';
}
} else {
if ($_GET['check-user'] == '') {
$option_user = '<p style="color:red;">Enter user to login</p>';
}
if ($_GET['check-pass'] == '') {
$option_pass = '<p style="color:red;">Enter pass to login</p>';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-latest.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.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.5.0/js/bootstrap.min.js"></script>
<title>Document</title>
</head>
<body>
<?= $option_check ?>
<div class="container">
<form method="get">
<div class="form-group">
<label for="user">
<h6>Username:</h6> <?= $option_user ?>
</label>
<input type="text" required="true" name="check-user" class="form-control" placeholder="Enter username" id="check-user">
</div>
<div class="form-group">
<label for="pass">
<h6>password: </h6><?= $option_pass ?>
</label>
<input type="password" required="true" name="check-pass" class="form-control" placeholder="Enter pass" id="check-pass">
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
</div>
</body>
</html>
<?php
$name = '';
if (isset($_COOKIE['fullname'])) {
$name = $_COOKIE['fullname'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2>Welcome to <?= $name ?></h2>
<a name="" id="" class="btn btn-primary" href="logout.php" role="button">Log out</a>
</body>
</html>
<?php
setcookie('style', 'success', time() - 180, '/');
header('Location: login.php');
die();
?>
![Trương Công Vinh [T1907A]](https://www.gravatar.com/avatar/223a7e3a46f4a747f81b921fe023fcc4.jpg?s=80&d=mm&r=g)
Trương Công Vinh
2020-06-17 15:11:25
<?php
if (isset($_COOKIE['status']) && $_COOKIE['status'] == 'success'){
header('Location: welcome.php');
die();
}
$user = $pass = '';
$_name=$_password='abc';
if (!empty($_POST)) {
// code...
if (isset($_POST['name'])) {
// code...
setcookie('name',$_POST['name'],time()+160, '/');
}
if (isset($_POST['email'])) {
// code...
setcookie('email',$_POST['email'],time()+160, '/');
}
if (isset($_POST['password'])) {
// code...
setcookie('password',$_POST['password'],time()+160, '/');
}
if (isset($_POST['address'])) {
// code...
setcookie('address',$_POST['address'],time()+160, '/');
}
if (isset($_POST['user'])) {
// code...
$user = $_POST['user'];
}
if (isset($_POST['pass'])) {
// code...
$pass = $_POST['pass'];
}
if (isset($_COOKIE['name'])){
$_name = $_COOKIE['name'];
}
if (isset($_COOKIE['password'])){
$_password = $_COOKIE['password'];
}
if ($user == $_name && $pass == $_password ){
// code...
setcookie('status','success',time()+60, '/');
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">Registation Form - Input User's Detail Information</h2>
</div>
<div class="panel-body">
<form method="post">
<div class="form-group">
<label for="usr">Username :</label>
<input required="true" type="text" name="user" class="form-control" id="usr">
</div>
<div class="form-group">
<label for="pwr">password:</label>
<input required="true" type="password" name="pass" class="form-control" id="pwr">
</div>
<button class="btn btn-success" type="submit">Login</button>
</div>
</form>
</div>
</div>
</body>
</html>
<!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">Registation Form - Input User's Detail Information</h2>
</div>
<div class="panel-body">
<form class="" action="input.php" method="post">
<div class="form-group">
<label for="usr">Username:</label>
<input required="true" type="text" name="name" class="form-control" id="usr">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input required="true" type="email" name="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="pwr">password:</label>
<input required="true" type="password" name="password" class="form-control" id="pwr">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input required="true" type="text" class="form-control" id="address">
</div>
<button class="btn btn-success" type="submit">Register</button>
</div>
</form>
</div>
</div>
</body>
</html>
<?php
if (isset($_COOKIE['status'])) {
// code...
setcookie('status','success',time()-60, '/');
}
header('Location : input.php');
die();
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
Login success
<a href="logOut.php">logOut</a>
</body>
</html>