Here is the authorization code. Yes, without mysql. Yes, terrible, but still.

<? $filename = ("base.txt"); function AuthForm () { echo ('<form method=\'post\'> <label>Логин: </label> <input type = \'text\' name = \'login\'><br /> <label>Пароль: </label> <input type = \'password\' name = \'password\'><br /> <input type = \'submit\' name = \'log\' value = \'Войти\'> </form>' ); } if(empty($_POST)) { }else { $arr = file($filename); $i = 0; $temp = array(); foreach($arr as $line) { $data = explode("|:|",$line); $temp['login1'][$i] = $data[0]; $temp['password1'][$i] = $data[1]; $i++; } $login = $_POST['login']; $password = /*md5*/($_POST['password']); if(!in_array($login,$temp['login1'])) { echo("Пользователь с таким именем не зарегистрирован. <a href='#'>Вернуться</a>"); exit(); } $index = array_search($login,$temp['login1']); if($password != $temp['password1'][$index]) { echo("Неправильный пароль. <a href='#'>Вернуться</a>"); exit(); } } ?> 

Here is the authorization on the site:

 <? require ("auth.php"); AuthForm(); ?> 

Who has already connected this nonsense, please help. Already tried everything.

  • Most likely, but still they are different, there is already a code with an error, and here, a clean code, where I asked for help to suggest how it would be better to arrange it. - MoloF

1 answer 1

This code should be at the top of every page.

 <?php session_start(); ?> 

In the array of sessions you can add data by assigning a value to a new array element.

 $_SESSION['newEl'] = 'value'; 

In the code block where the submit form gets you, after validating the login, write to the session, say the same login.

 if(!empty($_POST)){ //валидация //если проверки пройдены - заносите логин в сессию $_SESSION['login'] = $_POST['login']; } 

On the remaining pages, provided that the session start code is set, you should have this variable, according to this condition you can consider the user to be authorized and not display the authorization form.

More information about the sessions can be found in the documentation . There are also described the frequent reasons for the “flashing” of the session with redirects through the pages.

  • is it possible, <? session_start (); ?> add to auth.php and connect accordingly? - MoloF
  • Can I have another correction? it really interferes when I personally set myself if (empty ($ _ POST)), he swears that something is already filled. and does not do anything. I get a permanent error saying that the login is incorrect, although I only updated the page. What can you do about it? - MoloF
  • Theoretically possible. But it’s almost “not cool” to connect a php file to all html files (pages). And by and large you need a session on this issue only to record the fact of authorization - Konstantin Soroka
  • Konstantin, I just registered here, can I contact you somehow? in a nutshell I would describe the problem, and you are a very good programmer. - MoloF
  • On account of if (empty ($ _ POST)), not a good check, because you have this array always empty, except when clicking the Submit button. Better to redo that option that I suggested in the message if (! Empty ($ _ POST)) After clicking the Submit button, you will go inside this if and follow the authorization process in theory. - Konstantin Soroka