Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>PHP Project: Login</title>
- </head>
- <body>
- <?php
- //Error reporting
- ini_set('display_errors', '1');
- error_reporting(E_ALL);
- //Start the session
- session_start();
- //Session Variables
- $_SESSION['username'] = $_POST["username"];
- $_SESSION['password'] = $_POST["password"];
- //Boolean Variables
- $blnUserExists = false;
- $blnPassMatch = false;
- //showError - Used to display error messages
- function showError($strMessage)
- {
- return "Error: ". $strMessage;
- }
- //If a username and password were entered
- if ($_SESSION['username'] && $_SESSION['password'] != NULL)
- {
- //Define the location of the text database and open it
- $dbFile = "database.txt";
- $file = fopen($dbFile, 'X') or die(showError("The file cannot be open"));
- //While not the end of file OR the user does not exist
- while(!(feof($dbFile)) || $blnPassMatch == false)
- {
- //The line currently being read
- $strLine = fgets($dbFile);
- //Split the line into 3 strings - user:pass:access
- $arrDB = explode(":", $strLine);
- $dbUsername = $arrDB[0];
- $dbPassword = $arrDB[1];
- $dbAccess = $arrDB[2];
- //Check to see if the values match
- if ($_SESSION['username'] == $dbUsername)
- {
- //The username exists
- $blnUserExists = true;
- //Does the password match?
- if ($_SESSION['password'] == $dbPassword)
- {
- //Username exists && passwords match
- echo "Hello ". $_SESSION['username'] .". You are now logged in";
- echo "<br />";
- echo "You are an ". $dbAccess;.
- echo "<br />";
- echo "<a href='logout.php'>Logout</a>";
- }
- else
- {
- //The password doesn't match
- $blnPassMatch == false;
- }
- }
- else
- {
- //The username does not exist
- blnUserExists = false;
- }
- }
- //If the user didn't exist
- if (blnUserExists == false)
- {
- die(showError("The username \'". $_SESSION['username'] ."\' does not exist."));
- }
- //If the password didn't match
- if ($blnPassMatch == false)
- {
- die(showError("Incorrect password"));
- }
- }
- else
- {
- //A username and passowrd were not entered
- die(showError("Please enter a username AND password"));
- }
- //Close the file
- fclose($file);
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement