Góc Lập Trình > Học PHP

CÂU HỎI

Làm thế nào có thể đăng ký tài khoản và lưu thông tin tài khoản đã đăng ký vào Cookie? Sau đó tạo ra 1 form đăng nhập, nếu nhập đúng thông tin đã đăng ký trước đó thì chuyển sang màn hình welcome, nếu sai thì yêu cầu nhập lại thông tin???

VIDEO HƯỚNG DẪN

TRẢ LỜI

Hướng Dẫn

Bước 1. Tạo file Cookie.php


<?php
session_start();
require_once ("register.php");

register();
?>
<!DOCTYPE html>
<html>
<head>
	<title>PHP Tutorial</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<form action="" method="POST">
			<div class="form-group">
				<label for="email">Ho Ten:</label>
				<input type="text" class="form-control" name="fullname">
			</div>
			<div class="form-group">
				<label for="email">User Name:</label>
				<input type="text" class="form-control" name="username">
			</div>
			<div class="form-group">
				<label for="pwd">Password:</label>
				<input type="password" class="form-control" name="password">
			</div>
			<div class="form-group">
				<label for="email">Email address:</label>
				<input type="email" class="form-control" name="email">
			</div>
			<div class="form-group">
				<label for="email">Phone Number:</label>
				<input type="tel" class="form-control" name="phone_number">
			</div>
			<button type="submit" class="btn btn-default">Register</button>
		</form>
	</div>
</body>
</html>


Bước 2 : Tạo file register.php


<?php
function register() {
	if (!empty($_POST)) {
		$fullname     = $_POST['fullname'];
		$username     = $_POST['username'];
		$password     = $_POST['password'];
		$email        = $_POST['email'];
		$phone_number = $_POST['phone_number'];

		setcookie("fullname", $fullname, time() + 7 * 24 * 60 * 60, "/");
		setcookie("username", $username, time() + 7 * 24 * 60 * 60, "/");
		setcookie("password", $password, time() + 7 * 24 * 60 * 60, "/");
		setcookie("email", $email, time() + 7 * 24 * 60 * 60, "/");
		setcookie("phone_number", $phone_number, time() + 7 * 24 * 60 * 60, "/");

		header("Location: bai5.php");
	}
}


Bước 3 : Tạo file bai5.php


<?php
session_start();

require_once ("login.php");

login();
?>
<!DOCTYPE html>
<html>
<head>
	<title>PHP Tutorial</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/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">
			</div>
			<div class="form-group">
			<label for="pwd">Password:</label>
			<input type="password" class="form-control" name="password">
			</div>
			<button type="submit" class="btn btn-default">Submit</button>
		</form>
	</div>
</body>
</html>


Bước 4: Tạo file login.php


<?php

function login() {
	if (!empty($_POST)) {
		$cookieEmail = $_COOKIE['email'];
		$cookiePassword = $_COOKIE['password'];

		$email    = $_POST['email'];
		$password = $_POST['password'];


		if ($email == $cookieEmail && $password == $cookiePassword) {
			header("Location: welcome.php");
		}
	}
}


Bước 5: Tại file welcome.php.

Bạn gõ gì cũng được




Đã sao chép!!!