Advertisement
Guest User

Untitled

a guest
May 1st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. <?php
  2.  
  3.     $name = $_POST['name'];
  4.     $password = $_POST['password'];
  5.    
  6.     if ((!isset($name)) || (!isset($password))) {
  7.     // visitor needs to enter a name and password
  8. ?>
  9.  
  10. <h1>Please Log In</h1>
  11. <p>This page is secret.</p>
  12. <form method="post" action="test.php">
  13. <p>Username: <input type="text" name="name"></p>
  14. <p>Password: <input type="password" name="password"></p>
  15. <p><input type="submit" name="submit" value="Log In"></p>
  16. </form>
  17.  
  18. <?php
  19.     } else {
  20.  
  21.         //connect to mysql
  22.         $mysql = mysqli_connect("localhost", "webauth", "webauth");
  23.         if(!$mysql) {
  24.             echo "Cannot connect to db.";
  25.             exit;
  26.         } else
  27.             echo "connected to db.";
  28.        
  29.         // select the appropriate database
  30.         $selected = mysqli_select_db($mysql, "auth");
  31.         if (!$selected) {
  32.             echo "Cannot select database.";
  33.             exit;
  34.         } else echo "selected a database.";
  35.        
  36.         // query the database to see if there is a record which matches
  37.         $qurty = "select count(*) from authorized_users where name = '".$name."' and password = '".$password."'";
  38.                    
  39.         $result = mysqli_query($mysql, $query);
  40.  
  41.         if(!$result) {
  42.             echo "Cannot run query.";
  43.             exit;
  44.         }
  45.         $row = mysqli_fetch_row($result);
  46.         $count = $row[0];
  47.        
  48.         if ($count > 0) {
  49.             // visitor's name and password combination are correct
  50.             echo "<h1>Here it is!</h1>
  51.                 <p>I bet you are glad you can see this secret page.</p>";
  52.         } else {
  53.             // visitor's username and password are not correct
  54.             echo "<h1>Go Away!</h1>
  55.                 <p>You are not authorized to use this resource.</p>";
  56.         }
  57.     }
  58.    
  59. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement