./handleLogin.php
```
<?php
//get connection to db
$conn = require "./connection.php";
include "./user.php";
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$password = $_POST["txtPassword"];
$name = $_POST["txtFullname"];
try {
$user = new User(1, $name, $password, "");
//id này sẽ kh đc thêm vào nên tạo cho đủ thoi
User::login($conn, $user);
echo "Login successfully!";
}
catch (Exception $e) {
echo $e;
}
}
```
note: $_POST[""] sẽ khóa là thuộc tính name và đc submit lên bằng method post
- kiểu tra đường dẫn liên kết hình ảnh hay tệp chính xác để đảm bảo chức năng đăng kí đăng nhập
**./user.php**
```
<?php
class User {
public $id;
public $name;
public $password;
public $email;
public function __construct ($id, $name, $password, $email) {
$this->id = $id;
$this->name = $name;
$this->password = $password;
$this->email = $email;
}
public static function register ($conn, $user) {
$query = "INSERT INTO users (name, password, email) VALUES(:name, :password, :email)";
$stmt = $conn->prepare($query);
$stmt->bindParam(":name", $user->name);
$stmt->bindParam(":password", $user->password);
$stmt->bindParam(":email", $user->email);
return $stmt->execute();
}
public static function login ($conn, $user) {
$query = "SELECT * FROM users WHERE name = :name AND password = :password";
$stmt = $conn->prepare($query);
$stmt->bindParam(":name", $user->name);
$stmt->bindParam(":password", $user->password);
return $stmt->execute();
}
}
```
- note:
cần mở xampp tạo bẳng với các bảng
__construct() để khởi tạo đối tượng mới User với các thuộc tính đã đc dùng
register(): dùng để thêm 1 ng mới vào cơ sở dữ liệu
- $conn: kết nối đến cơ sở dữ liệu
- $user: đối tượng cần thêm
login():kiểm tra xem thông tin đăng nhập có hợp lệ hay ko
tạo liên kết đến CSDL được tạo trong xampp
connection.php
```
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "taskctf";
$conn;
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
}
return $conn;
?>
kêt nối <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "taskctf";
$conn;
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
}
return $conn;
?>
```
note: $conn: dùng để kết nối mà ko gán giá trị ban đầu
trả về kết nối: ```return $conn;```
handleLogin.php
```
<?php
//get connection to db
$conn = require "./connection.php";
include "./user.php";
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$password = $_POST["txtPassword"];
$name = $_POST["txtFullname"];
try {
$user = new User(1, $name, $password, "");
//id này sẽ kh đc thêm vào nên tạo cho đủ thoi
User::login($conn, $user);
echo "Login successfully!";
}
catch (Exception $e) {
echo $e;
}
}
```
dùng để xử lí đăng nhập của 1 ng dùng từ biểu mẫu
$conn = require "./connection.php"; dùng để kết nối từ tệp connection.php
```if ($_SERVER["REQUEST_METHOD"]=="POST") { ... }``` : sử dụng để kiển tra yêu cầu đến trang có phải post ko để xử lí nhập nhập khi ng dùng điền vào biểu mẫu
```User::login($conn, $user)``` kiểm tra thông tin đăng nhập có hợp lệ hay ko
handleRegister.php
```
<?php
//get connection to db
$conn = require "./connection.php";
include "./user.php";
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$password = $_POST["txtPassword"];
$email = $_POST["txtEmail"];
$name = $_POST["txtFullname"];
try {
$user = new User(1, $name, $password, $email);
//id này sẽ kh đc thêm vào nên tạo cho đủ thoi
User::register($conn, $user);
echo "Register successfully!";
}
catch (Exception $e) {
echo $e;
}
}
```
để đảm bảo trang web có thể hiển thị đúng trên các thiết bị và trình duyệt khác nhau
test.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>
<p>tesrt</p>
</body>
</html>
```
```
<?php
if (isset($_FILES["fileToUpload"])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}}
?>
```
```if (isset($_FILES["fileToUpload"]))``` kiểm tra xem có tệp tin đc tải lên ko
```$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));``` kiểm tra định dạng của tệp tin(JPG, PNG,..) nếu ko phải thì sẽ hủy
```
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
```
cho phép ng dùng chọn và tải 1 tập tin hình ảnh
```action="upload.php``` xử lý dữ liệu biểu mẫu sau khi nó đc gửi đi
tạo tệp hình ảnh upload.php(chứa hình ảnh)