Creating a Login File

Most websites developed with PHP contain multiple program files that will require access to MySQL and will therefore need the login and password details. Therefore, it’s sensible to create a single file to store these and then include that file wherever it’s needed. Following example shows such a file, which I’ve called login.php. Type it in, replacing values (such as username) with the actual values you use for your MySQL database, and save it to the web development directory you set . We’ll be making use of the file shortly. The hostname localhost should work as long as you’re using a MySQL database on your local system, and the database publications should work if you’re typing in the examples I’ve used so far.

The login.php file

<?php // login.php
$db_hostname = 'localhost';
$db_database = 'publications';
$db_username = 'username';
$db_password = 'password';
?>

The enclosing tags are especially important for the login.php file in above Example, because they mean that the lines between can be interpreted only as PHP code. If you were to leave them out and someone were to call up the file directly from your website, it would display as text and reveal your secrets. But, with the tags in place, all they will see is a blank page. The file will correctly include in your other PHP files.
 
The $db_hostname variable will tell PHP which computer to use when connecting to a database. This is required, because you can access MySQL databases on any computer connected to your PHP installation, and that potentially includes any host anywhere on the Web. However, the examples here will be working on the local server. So in place of specifying a domain such as mysql.myserver.com, the word localhost (or the IP address 127.0.0.1) will correctly refer to it.
 
The database we’ll be using, $db_database, is the one called publications, which you probably created previously, or the one you were provided with by your server administrator (in which case you have to modify login.php accordingly). The variables $db_username and $db_password should be set to the username and password that you have been using with MySQL.
 
The variables $db_username and $db_password should be set to the username and password that you have been using with MySQL.

 
Another benefit of keeping these login details in a single place is that you can change your password as frequently as you like and there will
be only one file to update when you do, no matter how many PHP files access MySQL.