Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Cần trợ giúp tạo tập lệnh đăng ký / đăng nhập người dùng tùy chỉnh

Đối với ví dụ này, tôi sẽ bỏ qua các câu lệnh đã chuẩn bị sẵn, nhưng bạn sẽ cần thực hiện một số nghiên cứu về ngăn chặn SQL-injection.

Đầu tiên, bạn cần một biểu mẫu để người dùng sử dụng để đăng nhập. Đây là biểu mẫu cơ bản sẽ có trên trang có tên NewUser.html:

<form action="AddUser.php" method="POST">
<p>Enter A Username: </p>
<input type="text" name="User" maxlength="20" size="10">
<br />
<p>Enter A Password: </p>
<input type="password" name="Password" maxlength="40" size="10">
<br />
<p>Enter Password Again: </p>
<input type="password" name="PasswordX2" maxlength="40" size="10">
<br />
<input type="submit" value="Create Account">
</form>

Tất nhiên, bạn có thể thêm các trường khác, chẳng hạn như địa chỉ email, v.v.- nhưng tôi giữ cho nó đơn giản.

Bây giờ chúng ta hãy truy cập trang AddUser.php:

<?php

//Now let's grab our $_POST data from our form and assign them to variables...
$User = $_POST['User'];
$PW = $_POST['Password'];
$PW2 = $_POST['PasswordX2'];

//Check whether user put anything in the fields for user or passwords
if (!$User || !$PW || !$PW2) {
echo "You have not entered all the needed info. Please try again.";
exit();
}

//Check if passwords match
if ($PW <> $PW2) {
echo "Your passwords do not match. Please go back and try again.";
exit();
}

//Now we want to be good stewards of passwords and information so let's hash that password
$hash = password_hash($PW, PASSWORD_BCRYPT);

//Open your connection to database
$dbconnect-> blah blah(make your database connection here)....

//Now let's insert the new user into the database - remember do not do this without SQL-injection prevention. I'm just giving you the basics.
$sql = "INSERT INTO UsersTable (UserName, Password)
VALUES ('".$User."', '".$hash."')";

//Verify Successful Entry
if (mysqli_query($dbconnect,$sql)) {
echo "User Added Successfully";
} else {
echo "Error Creating User: " . mysqli_error($dbconnect);
}

echo "<br /><p>Please go to the main page to login now.</p>";
?>

Vì vậy, người dùng hiện đã được tạo, mật khẩu đã được băm với một muối và được chèn vào DB ... đừng quên SQL-injection.

Bây giờ bạn sẽ có một biểu mẫu rất giống với biểu mẫu NewUser.html để đăng nhập, nhưng nó sẽ không yêu cầu nhập mật khẩu hai lần. Giả sử biểu mẫu đăng nhập đó đưa người dùng đến một trang có tên là login.php:

<?php
session_start(); //starts a session for tracking user on each page - this has to be on every page

//Let's get our variables from the POST data- will be identical to before most likely
$User = $_POST['User'];
$PW = $_POST['Password'];

//Open your connection to database
$dbconnect-> blah blah(make your database connection here)....

//Let's see if the username and password matches what we have in the database
$sql = "SELECT UsersTable.UserName, UsersTable.Password
FROM UsersTable
WHERE UsersTable.UserName = '$User'";
$result = $dbconnect->query($sql);

//Let's get the hashed password that corresponds to that username
$row = $result->fetch_assoc();
$HashedPassword = $row['Password'];

//Let's verify the password is correct
if (password_verify($PW, $HashedPassword))
{

//if it is correct(true) this will now happen
$_SESSION['verified_user'] = $User; //registers user by storing it in a SESSION
}
else {
echo "Login failed. Try again.";
exit();
}
?>

Chỉ là một mẹo nhỏ, nếu bạn muốn thêm cấp độ truy cập, bạn có thể lưu trữ một vị trí trong cơ sở dữ liệu với số truy cập (ví dụ:1, 2, 3) và sau khi đăng nhập thành công, bạn sẽ chỉ định một $ _SESSION khác đại diện cho cấp độ truy cập của họ và cấp cho họ quyền truy cập vào các phần nhất định mà bạn cho phép.

Bây giờ khi họ điều hướng đến các trang khác trên trang web của bạn, phiên của họ sẽ được xác minh như sau:

Ví dụPage.php

<?php
session_start();

if (isset($_SESSION['verified_user'])) {
//User is verified and whatever is here will be visible and happen- YAY!
}
else {
echo "You are not logged in and cannot see this page.";
}
?>

Chỉ cần có thói quen bắt đầu một phiên trên mọi trang mà chỉ những người đã đăng nhập mới được phép truy cập. Các phiên được ghi nhớ từ trang này sang trang khác.

Đừng quên cung cấp cho họ một trang đăng xuất sẽ phá hủy phiên:logout.php

<?php
session_start();

unset($_SESSION['verified_user']);
session_destroy();
echo "You are logged out.";
?>


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Làm cách nào để nhập MySql Connector vào Unity Project?

  2. Sử dụng char làm khóa chính / khóa ngoại có phải là không?

  3. Cách xử lý múi giờ MySQL trong script

  4. Đã cố đọc lỗi cuối luồng trong MySQL

  5. Cách lấy hình ảnh từ một bảng với các văn bản cột khác bằng PHP