In this article, we will look at the authorization of visitors. There are several ways to restrict access to site resources, for example, using the Apache Web server by creating a .htpassw file. This path is not always convenient, since transferring files to another server requires recreating .htpassw over a new one, moreover, it is rather tedious to change the password with this method. In this regard, developers often resort to PHP authorization, despite the fact that such protection is more susceptible to hacking.
This article will discuss the principle of such authorization, and it will allow you to organize something similar on your site. We will start from the Guestbook Web application (on MySQL), which can be downloaded here.
Usually, access to the administration panel (admin / index.php) is limited by Apache tools, but we will consider a script that allows you to restrict access using PHP.
The login and password will be stored in a file, since this does not require access to the database and can be used in other Web applications.
All further code is based on the code of the Guestbook, so for convenience it should be downloaded from the site.
$access = array();
$access = file("access.php");
$login = trim($access);
$passw = trim($access);
$_SESSION['login'] = $_POST['login'];
$_SESSION['passw'] = $_POST['passw'];
$login != $_SESSION['login'] or
$passw != $_SESSION['passw'] )
<a href="index.php">Return to guestbook administration</a>
<form action=index.php method=post>
Логин <input class=input name=login value="">
Пароль <input class=input name=passw value="">
<input type=hidden name=enter value=yes>
<input class=button type=submit value="Enter">
The file with login and password access.php has the following structure:
<?php die; ?>
For more reliable protection, the password and login can be subjected to irreversible encryption using the md5 () function
Please note that when accessing the file from the browser window, the script will be stopped in the first line by the die () function, not allowing the username (admin) and password (passw) to be displayed in the browser window.
Now we should create a control file through which we will gain access to all the other files of the administration system. Rename the index.php file to main.php in the admin directory of the guestbook and create a new index.php file with the following content.