Mysql
 sql >> Baza danych >  >> RDS >> Mysql

Potrzebujesz pomocy w tworzeniu niestandardowego skryptu rejestracji/logowania użytkownika

W tym przykładzie zamierzam pominąć przygotowane instrukcje, ale będziesz musiał przeprowadzić pewne badania dotyczące zapobiegania wstrzykiwaniu SQL.

Najpierw potrzebujesz formularza, którego użytkownik użyje do zalogowania.Oto podstawowy, który będzie znajdował się na stronie o nazwie 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>

Możesz oczywiście dodać inne pola, takie jak adres e-mail itp., Ale staram się to uprościć.

Przejdźmy teraz do strony 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>";
?>

Więc użytkownik został utworzony, hasło zostało zahaszowane solą i wstawione do DB... poważnie nie zapomnij o wstrzykiwaniu SQL.

Teraz będziesz miał formularz, który jest bardzo podobny do formularza NewUser.html do logowania, ale nie będzie wymagał dwukrotnego wpisywania hasła. Załóżmy, że formularz logowania wysyła użytkownika do strony o nazwie 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();
}
?>

Tylko wskazówka, jeśli chcesz dodać poziomy dostępu, możesz zapisać miejsce w bazie danych z numerem dostępu (np.:1, 2, 3), a następnie po pomyślnym zalogowaniu przypisz kolejną $_SESSION, która reprezentuje ich poziom dostępu i daje im dostęp do określonych sekcji, na które zezwalasz.

Teraz, gdy przejdą do innych stron w Twojej witrynie, ich sesja zostanie zweryfikowana w następujący sposób:

Przykładowa strona.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.";
}
?>

Po prostu nabierz zwyczaju rozpoczynania sesji na każdej stronie, do której dostęp mają tylko ci, którzy są zalogowani. Sesje są zapamiętywane ze strony na stronę.

Nie zapomnij dać im strony wylogowania, która zniszczy sesję: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. Python:MySQLdb i biblioteka nie zostały załadowane:libmysqlclient.16.dylib

  2. Czy istnieje narzędzie do analizy statycznej do identyfikacji iniekcji sql dla php/mysql?

  3. Zainstaluj MySQL 5.6 na Ubuntu 20.04

  4. PDO i php — wywołanie funkcji członkowskiej Prepare() na obiekcie niebędącym obiektem

  5. Jak sprawdzić rozmiar bazy danych MySQL w systemie Linux?