Hello, dear! There was an unpleasant problem with the database, or with the session itself. The fact is that I am writing a module that checks whether a person is authorized, and if not, the output of the login form.
The code here is the explanation below:
<?php unset($_SESSION['logged_user']); session_start(); require_once('db.php'); $sql_connection = dbConnect(); function LoginForm(){ if(!empty($_POST['user'])){ $sql_get_info = "SELECT `name`, `password`, `group` FROM `users` WHERE `name` = '".$_POST['user']."' "; $data = mysql_query($sql_get_info, $sql_connection) or die; if($data['password'] == $_POST['password']){ $_SESSION['logged_user'] = $_POST['login']; switch($date['group']) { case 10: { $_SESSION['group'] = 10; break; } case 20: { $_SESSION['group'] = 20; break; } default: {echo "Ваши права неопределены. ";} } } else{ echo "Неверный пароль". LoginForm(); } } echo " Авторизуйтесь для продолжения. <br> <form method = 'POST'> <input type = 'text' name = 'user'><br> <input type = 'password' name = 'password'><br> <input type = 'submit'><br> </form>"; die(); } if(!isset($_SESSION['logged_user'])){ LoginForm(); } ?> Успешная авторизация.
Copy to gist: https://gist.github.com/anonymous/0ddda3ff6893f3dd944e
The fact is that in this situation, after calling the function, the 4-5 line with the database connection is "forgotten", and because of this, null was passed to one of the parameters in line 13, because of this there was an error:
Warning: mysql_query () expects parameter 2 to be resource, null given in C: \ Winginx \ home \ manager.loc \ public_html \ protect.php on line 13
As I understand it, it was necessary to add the require_once of the file with the function of connecting to the database + to announce the start of the session in the body of the function so that it sees them:
unset($_SESSION['logged_user']); session_start(); require_once('db.php'); $sql_connection = dbConnect();
I did it, i.e .:
function LoginForm(){ unset($_SESSION['logged_user']); session_start(); require_once('db.php'); $sql_connection = dbConnect(); if(!empty($_POST['user'])){...
As a result, after entering the data and sending it - the server began ... to lay down . The page simply fell with a Time-out error, and moreover - then even another page on the server did not start at all, that is, it had to be reloaded. There is nothing in the error logs of the server or PHP.
I use winginx.
Looks like it’s about session and so on.
Regarding the database: With require_once, the function of connecting to the database is fine, I use it on other pages of the service, and here such problems ...
The current code with which the server crashes is:
<?php unset($_SESSION['logged_user']); session_start(); function LoginForm(){ session_start(); require_once('db.php'); $sql_connection = dbConnect(); if(!empty($_POST['user'])){ $sql_get_info = "SELECT `name`, `password`, `group` FROM `users` WHERE `name` = '".$_POST['user']."' "; $data = mysql_query($sql_get_info, $sql_connection) or die; if($data['password'] == $_POST['password']){ $_SESSION['logged_user'] = $_POST['login']; switch($date['group']) { case 10: { $_SESSION['group'] = 10; break; } case 20: { $_SESSION['group'] = 20; break; } default: {echo "Ваши права неопределены. ";} } } else{ echo "Неверный пароль". LoginForm(); } } echo " Авторизуйтесь для продолжения. <br> <form method = 'POST'> <input type = 'text' name = 'user'><br> <input type = 'password' name = 'password'><br> <input type = 'submit'><br> </form>"; die(); } if(!isset($_SESSION['logged_user'])){ LoginForm(); } ?> Успешная авторизация.
Copy to gist: https://gist.github.com/anonymous/e3bf63abb5fdd06068c8
mysql_*
outdated extension. Typeerror_reporting(E_ALL)
andini_set('display_errors', true)
at the beginning of the script and restart the script. To check the session, makevar_dump($_SESSION)
aftersession_start()
. You can still write something before that there for clarity. - Naumovmysql
showphpinfo();
outputphpinfo();
- Naumovsession_start()
specified at the very beginning of the file, it’s better that there is nothing at all, delete everything above, secondly, there can only be onesession_start()
. From under the function it just needs to be removed. And why are you doing unset, you will never be authenticated by the user, it will immediately be reset, and the code will be performed on a new one - Vasily Barbashev