Advertisement
Guest User

Untitled

a guest
Jun 6th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.08 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>PHP Project: Login</title>
  4. </head>
  5. <body>
  6.  
  7. <?php
  8.  
  9. //Error reporting
  10. ini_set('display_errors', '1');
  11. error_reporting(E_ALL);
  12.  
  13. //Start the session
  14. session_start();
  15.  
  16. //Session Variables
  17. $_SESSION['username'] = $_POST["username"];
  18. $_SESSION['password'] = $_POST["password"];
  19.  
  20. //Boolean Variables
  21. $blnUserExists = false;
  22. $blnPassMatch = false;
  23.  
  24. //showError - Used to display error messages
  25. function showError($strMessage)
  26. {
  27.     return "Error: ". $strMessage;
  28. }
  29.  
  30. //If a username and password were entered
  31. if ($_SESSION['username'] && $_SESSION['password'] != NULL)
  32. {
  33.     //Define the location of the text database and open it
  34.     $dbFile = "database.txt";
  35.     $file = fopen($dbFile, 'X') or die(showError("The file cannot be open"));
  36.    
  37.     //While not the end of file OR the user does not exist
  38.     while(!(feof($dbFile)) || $blnPassMatch == false)
  39.     {
  40.         //The line currently being read
  41.         $strLine = fgets($dbFile);
  42.        
  43.         //Split the line into 3 strings - user:pass:access
  44.         $arrDB = explode(":", $strLine);       
  45.         $dbUsername = $arrDB[0];
  46.         $dbPassword = $arrDB[1];
  47.         $dbAccess = $arrDB[2];
  48.        
  49.         //Check to see if the values match
  50.         if ($_SESSION['username'] == $dbUsername)
  51.         {
  52.             //The username exists
  53.             $blnUserExists = true;
  54.            
  55.             //Does the password match?
  56.             if ($_SESSION['password'] == $dbPassword)
  57.             {
  58.                 //Username exists && passwords match
  59.                 echo "Hello ". $_SESSION['username'] .". You are now logged in";
  60.                 echo "<br />";
  61.                 echo "You are an ". $dbAccess;.    
  62.                 echo "<br />";
  63.                 echo "<a href='logout.php'>Logout</a>";        
  64.             }
  65.             else
  66.             {
  67.                 //The password doesn't match
  68.                 $blnPassMatch == false;
  69.             }
  70.         }
  71.         else
  72.         {
  73.             //The username does not exist
  74.             blnUserExists = false;
  75.         }
  76.     }
  77.    
  78.     //If the user didn't exist
  79.     if (blnUserExists == false)
  80.     {
  81.         die(showError("The username \'". $_SESSION['username'] ."\' does not exist."));
  82.     }
  83.    
  84.     //If the password didn't match
  85.     if ($blnPassMatch == false)
  86.     {
  87.         die(showError("Incorrect password"));
  88.     }
  89. }
  90. else
  91. {
  92.     //A username and passowrd were not entered
  93.     die(showError("Please enter a username AND password"));
  94. }
  95.  
  96. //Close the file
  97. fclose($file);
  98.  
  99. ?>
  100.  
  101. </body>
  102. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement