I wanted to do this: go to the Page (it is necessary to password it), you are redirected to the page with the login and password form; if you enter correctly, then go to the page Page . But here is the problem, after entering the correct password, go to the Page Page and it again throws you to the password. Vicious circle. Does anyone have something similar, more working? Throw off.

    7 answers 7

    I think so completely simple:

     <?php if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authenticate: Basic realm="My Realm"'); header('HTTP/1.0 401 Unauthorized'); echo 'Текст, отправляемый в том случае, если пользователь нажал кнопку Cancel'; exit; } else { echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>"; echo "<p>Вы ввели пароль {$_SERVER['PHP_AUTH_PW']}.</p>"; } ?> 

    Taken here

      The bottom line is that the server must remember that you have passed authorization and authentication. This information will help you save data about successful authorization:

      Sessions

      Cookies

        There is a good tutorial on youtube , for one source you can download, called Login System !

          Isn't it easier to do everything through htaccess ???

          Suppose you have a website www.site.ru, and at www.site.ru/webadmin/ you have a site administration system, access to which must be limited by a password. To do this, you need to create two files in the / webadmin / directory: .htaccess and .htpasswd.

          Contents of the .htaccess file:

          AuthType basic

          AuthName 'Authorization ...'

          AuthUserFile '/.../www/webadmin/.htpasswd'

          Require valid-user

          DirectoryIndex admin.php

          The third and fifth lines are important to you, the rest do not need to be changed. The third line between single quotes is the path to the .htpasswd file from the root directory of the server. As a rule, this information (the path from the server root) is provided by the host, among other information about the leased FTP site. The fifth line contains the file that will be loaded by default (in case you did not specify a specific file in the address bar).

          The .htpasswd file stores the login and password for the folder. The contents of this file looks like this:

          login: $ apr1 $ fH4 ..... $ hMovWyy.EMp5FAW4TEUDH /

          • login and colon passwords encrypted with MD5.

          Taken from here tyk

            This is the simplest option, try it:

             <?php // Инициализация переменных $user_login = !empty($_POST['user_login'])?$_POST['user_login']:NULL; $user_password = !empty($_POST['user_password'])?$_POST['user_password']:NULL; // Устанавливаем логин - пароль $login = 'Иванов'; $password = 'Ракета'; // Проверка if($login === $user_login && $password === $user_password) echo 'Проходи!'; else echo 'Стой, стрелять буду!'; ?> <form action="" method="post"> Логин <input name="user_login" type="text"><br> Пароль <input name="user_password" type="password"><br> <input name="ok" type="submit" value="Тук-тук"> </form> 

              Let me offer you even easier

               <? function input() { //принимаем по посту ник и пароль $userlogin = $_POST['userlogin']; $userpassword = $_POST['userpassword']; //проверяем сходятся введенные данные с имеющимися if($userlogin != 'login' && $userpassword != 'pass') { echo "Катись отсюда"; } else { echo "добро пожаловать"; } } ?> #форма <form action="input()" method="post"> Логин: <input name="userlogin" type="text"><br> Пароль: <input name="userpassword" type="password"><br> <input name="send" type="submit" value="Отправить"> </form> 
              • correct code> Password: <input name = "userpassword" type = "password"> <br> - Artem
              • > action = "input ()" How long have web development been doing? - ling
              • 3 years engaged, right now engaged in the development of games. I don’t remember the last time I wrote the code in php - dajver

              Better this way:

               //Страничка login.php <?php session_start(); $password = "thispass"; if (!empty($_POST['password'])) $trim_password = trim($_POST['password']); { if ($_trim_password != $password) die ("Неверный пароль!"); else { $_SESSION['auth'] = "success"; header('Location: private.php'); } } //Страничка private.php <?php session_start(); if (empty($_SESSION['auth'])) header('Location:login.php'); else echo "Hello!"; ?> 

              I think you will write the form yourself. Not yet for you to do.