Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.50 KB | None | 0 0
  1. <?php
  2.     session_start(); //Starts A Session Unless There Already Is One.
  3.     include 'dbconnectfilehere.php'; // Connects to the file which accesses the database.
  4.    
  5.     //Finds if there is a session called 'username', which starts once there logged in!
  6.     if($_SESSION['username']) {
  7.         // If there is a session, it takes them to home.php.
  8.         header('Location: home.php');
  9.     } else {
  10.         //Else if there isn't a session, it see's if there $_POST values exist.
  11.         if(isset($_POST['username']) && isset($_POST['password'])) {
  12.             //If they do, it gets the username and password value.
  13.             $username = mysql_real_escape_string($_POST['username']);
  14.             $password = mysql_real_escape_string(md5($_POST['password']));
  15.             //Now it does a mysql query to try and find the users.
  16.             $query = mysql_query("SELECT * FROM `users` WHERE `username` = '{$username}' && `password` = '{$password}'");
  17.             $num = mysql_num_rows($query);
  18.             if($num == 0) { //If there isn't any rows, it means there information is wrong.
  19.                 echo "Wrong information!";
  20.             } else { //If there is a row, it starts the session!
  21.                 $_SESSION['username'] = $username;
  22.                 $_SESSION['password'] = $password;
  23.                 echo "You are now logged in! Please click <a href='home.php'>here!</a>";
  24.             }
  25.         }
  26.         ?>
  27.         <form action="login.php" method="post">
  28.         Username:<br />
  29.         <input type="text" name="username" id="username" /><br />
  30.         Password:<br />
  31.         <input type="password" name="password" id="password" /><br />
  32.         <input type="submit" value="Login" />
  33.         </form>
  34.         <?php
  35.     }
  36. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement