Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1. <?php
  2.  
  3. session_start();
  4.  
  5. /* Log the user out if requested */
  6. if(isset($_GET['act']) && $_GET['act'] == 'logout')
  7. {
  8.     /* Check for CSRF */
  9.     if(isset($_GET['token']) && $_GET['token'] == $_SESSION['token'])
  10.     {
  11.         session_destroy();
  12.         header("Location: " . $_SERVER['PHP_SELF']);
  13.     }
  14.     else
  15.     {
  16.         exit("Token key incorrect. Possible hacking attempt.");
  17.     }
  18. }
  19.  
  20. /* Check if user is already logged in */
  21. if(!isset($_SESSION['id']))
  22. {
  23.     /* Connect to the database */
  24.     $conn = mysql_connect("localhost", "root", "removedforpastebin");
  25.     mysql_select_db("one_page_login", $conn);
  26.  
  27.     /* Check if they attempted to login */
  28.     if(isset($_POST['submit']))
  29.     {
  30.         /* Prepare data for query */
  31.         $user = mysql_real_escape_string($_POST['user'], $conn);
  32.         $pass = sha1($_POST['pass']);
  33.        
  34.         /* Select user id and password from database */
  35.         $result = mysql_query("SELECT `id`, `password` FROM `users` WHERE `username`='" . $user . "'", $conn);
  36.        
  37.         /* Check that a user actually exists by that name */
  38.         if(mysql_num_rows($result) != 0)
  39.         {
  40.             /* Check if password is correct */
  41.             $array = mysql_fetch_assoc($result);
  42.             if($array['password'] == $pass)
  43.             {
  44.                 $_SESSION['id'] = $array['id'];
  45.                 $_SESSION['token'] = md5(microtime());
  46.                
  47.                 header("Location: " . $_SERVER['PHP_SELF']);
  48.             }
  49.             else
  50.             {
  51.                 echo "The password you entered was invalid!";
  52.             }
  53.         }
  54.         else
  55.         {
  56.             echo "No user exists by that username!";
  57.         }
  58.     }
  59.     else
  60.     {
  61.         /* Build the form */
  62.         ?>
  63.         <form name='login' method='post' action=''>
  64.             <table>
  65.             <tr>
  66.                 <td>Username</td>
  67.                 <td><input type='text' name='user' value='' /></td>
  68.             </tr>
  69.             <tr>
  70.                 <td>Password</td>
  71.                 <td><input type='password' name='pass' value='' /></td>
  72.             </tr>
  73.             <tr>
  74.                 <td colspan='2'><input type='submit' name='submit' value='Login!' /></td>
  75.             </tr>
  76.         </form>
  77.         <?php
  78.     }
  79. }
  80. else
  81. {
  82.     echo "You are already logged in! <a href='?act=logout&token=" . $_SESSION['token'] . "'>Logout?</a>";
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement