Answer the question
In order to leave comments, you need to log in
PHP registration. Is it the right way of thinking?
I am writing a small news site for practice. While at the stage of writing authorization. The bottom line for me so far is this: There is a main index.htm file in it, only a login form and at the same time (for now) for registration, a login / password pair is entered into the fields, the presence of this pair in the database is checked, if not, it is recorded. I used the session to save the input data, in show.htm I just display for myself what was entered by the user, checking the input parameters in the same file. Appreciate if possible. Point out the mistakes, give some guidance or push the right idea.) PHP files are placed in a separate directory. In Htm files I try to include only the necessary files. Rate the code)
index.htm
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="admin" />
<title>вход</title>
</head>
<body>
<form action="php/session.php" method="post">
<label>Логин:<br /><input type="text" name="login" value="<?php echo filter_var($_SESSION['login'], FILTER_SANITIZE_SPECIAL_CHARS); ?>" /><br /></label>
<label>Пароль:<br /><input type="password" name="pass" value="<?php echo filter_var($_SESSION['pass'], FILTER_SANITIZE_SPECIAL_CHARS); ?>" /><br /></label>
<label>Почта:<br /><input type="text" name="email" value="<?php echo filter_var($_SESSION['email'], FILTER_SANITIZE_EMAIL); ?>" /><br /></label>
<label><button type="submit">Войти</button></label>
</form>
</body>
</html>
<?php
session_start();
$whitelist = array(
'login' => 'login',
'pass' => 'pass',
'email' => 'email'
);
$incoming = array_keys(array_intersect_key($_POST, $whitelist));
foreach($whitelist as $key){
$_SESSION[$key] = $_POST[$key];
}
header('Location: show.htm');
<?php
$SelectFromDb = "SELECT * FROM people WHERE name = '$login'";
$res = mysqli_query($mysqli,$SelectFromDb);
$num = mysqli_num_rows($res);
if($num == 0){
$querry = "INSERT INTO people(name, pass, email) VALUES('$login', '$pass', '$email')";
$result = mysqli_query($mysqli, $querry) or die("Ошибка записи!");
}
else {
echo "Ошибка записи в БД! Данный логин уже существует!";
}
<?php
session_start();
$login = filter_var($_SESSION['login'], FILTER_SANITIZE_SPECIAL_CHARS);
$pass = filter_var($_SESSION['pass'], FILTER_SANITIZE_SPECIAL_CHARS);
$email = filter_var($_SESSION['email'], FILTER_SANITIZE_EMAIL);
require_once ('db.php');
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="admin" />
<title>Результат</title>
</head>
<body>
<table>
<tr>
<?php echo $login; ?>
</tr>
<tr>
<?php echo $pass; ?>
</tr>
<tr>
<?php echo $email; ?>
</tr>
</table>
<form action="../index.htm" method="post">
<input type="hidden" name="login" value="<?php echo $login; ?>" />
<input type="hidden" name="pass" value="<?php echo $pass; ?>" />
<input type="hidden" name="email" value="<?php echo $email; ?>" />
<button type="submit">Редактровать</button>
</form>
<?php require_once('users.php'); ?>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
the username/password pair is entered into the fields, the presence of this pair in the database is checked, if not, then it is written
foreach($whitelist as $key){
$_SESSION[$key] = $_POST[$key];
}
The session does not start in index.htm.
FILTER_SANITIZE_SPECIAL_CHARS is almost FILTER_SANITIZE_FULL_SPECIAL_CHARS which is equivalent to htmlspecialchars(). htmlspecialchars should be shorter.
htmlspecialchars($text, ENT_COMPAT, 'UTF-8', true);
For now fix these 2 points
I do not recommend storing passwords in their original form. When adding a password to the database, use the md5($pass) function
2nd line in users.php corrected to
$SelectFromDb = "SELECT * FROM people WHERE name = '". $login . "'";
session_start(); //добавил в index.htm
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question