Advertisement
Guest User

index.php

a guest
Apr 19th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.39 KB | None | 0 0
  1. <?php
  2.  
  3.     $user = 'root';
  4.     $password = 'root';
  5.     $db = 'udemy';
  6.     $host = 'localhost';
  7.     $port = 8889;
  8.  
  9.     session_start();
  10.  
  11.     $error = "";
  12.  
  13.     if (array_key_exists("logout", $_GET)) {
  14.         unset($_SESSION);
  15.         setcookie("id", "", time() - 60 * 60);
  16.         $_COOKIE["id"] = "";
  17.     } else if ((array_key_exists("id", $_SESSION) AND $_SESSION['id']) OR (array_key_exists("id", $_COOKIE) AND $_COOKIE['id'])) {
  18.         header("Location: loggedinpage.php");
  19.     }
  20.  
  21.  
  22.     if (array_key_exists("submit", $_POST)) {
  23.  
  24.  
  25.         $link = mysqli_connect("$host:$port", $user, $password, $db);
  26.  
  27.         if (mysqli_connect_error()) {
  28.             die ("Database Connection Error");
  29.         }
  30.  
  31.  
  32.         if (!$_POST['email']) {
  33.             $error .= "An email address is required<br>";
  34.         }
  35.  
  36.         if (!$_POST['password']) {
  37.             $error .= "A password is required<br>";
  38.         }
  39.  
  40.         if ($error != "") {
  41.             $error = "<p>There were error(s) in your form:</p>".$error;
  42.         } else {
  43.  
  44.             if ($_POST['signUp'] == '1') {
  45.  
  46.  
  47.                 $query = "SELECT id FROM users WHERE email = '".mysqli_real_escape_string($link, $_POST['email'])."' LIMIT 1";
  48.  
  49.                 $result = mysqli_query($link, $query);
  50.  
  51.                 if (mysqli_num_rows($result) > 0) {
  52.                     $error = "That email address is taken.";
  53.                 } else {
  54.                     $query = "INSERT INTO users (email, password) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."', '".mysqli_real_escape_string($link, $_POST['password'])."')";
  55.  
  56.                     if (!mysqli_query($link, $query)) {
  57.                         $error = "<p>Could not sign you up - please try again later.</p>";
  58.                     } else {
  59.  
  60.                         $query = "UPDATE users SET password = '".md5(md5(mysqli_insert_id($link)).$_POST['password'])."' WHERE id = ".mysqli_insert_id($link)." LIMIT 1";
  61.  
  62.                         mysqli_query($link, $query);
  63.  
  64.                         $_SESSION['id'] = mysqli_insert_id($link);
  65.  
  66.                         if ($_POST['stayLoggedIn'] == '1') {
  67.                             setcookie("id", mysqli_insert_id($link), time() + 60 * 60 * 24 * 365);
  68.                            
  69.                         }
  70.  
  71.  
  72.                         header("Location: loggedinpage.php");
  73.                     }
  74.                 }
  75.             } else {
  76.                 $query = "SELECT * FROM users WHERE email ='".mysqli_real_escape_string($link, $_POST['email'])."'";
  77.  
  78.                 $result = mysqli_query($link, $query);
  79.  
  80.                 $row = mysqli_fetch_array($result);
  81.  
  82.                 if (isset($row)) {
  83.                     $hashedPassword = md5(md5($row['id']).$_POST['password']);
  84.  
  85.                     if ($hashedPassword == $row['password']) {
  86.                         $_SESSION['id'] = $row['id'];
  87.  
  88.                     if ($_POST['stayLoggedIn'] == '1') {
  89.                             setcookie("id", $row['id'], time() + 60 * 60 * 24 * 365);
  90.                            
  91.                         }
  92.                         header("Location: loggedinpage.php");
  93.                     } else {
  94.                         $error = "That email/password combination could not be found.";
  95.                     }
  96.                 } else {
  97.                     $error = "That email/password combination could not be found.";
  98.                 }
  99.             }
  100.         }
  101.     }
  102.  
  103.  
  104.  
  105.  
  106. ?>
  107.  
  108.  
  109. <div id="error">
  110.     <?php echo $error; ?>
  111. </div>
  112.  
  113.  
  114.  
  115.  
  116. <form method="post">
  117.     <input type="email" name="email" placeholder="Your Email">
  118.  
  119.     <input type="password" name="password" placeholder="Password">
  120.  
  121.     <input type="checkbox" name="stayLoggedIn" value="1">
  122.  
  123.     <input type="hidden" name="signUp" value="1">
  124.  
  125.     <input type="submit" name="submit" value="Sign Up!">
  126. </form>
  127.  
  128. <form method="post">
  129.     <input type="email" name="email" placeholder="Your Email">
  130.  
  131.     <input type="password" name="password" placeholder="Password">
  132.  
  133.     <input type="checkbox" name="stayLoggedIn" value=1>
  134.  
  135.     <input type="hidden" name="signUp" value="0">
  136.  
  137.     <input type="submit" name="submit" value="Log In!">
  138. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement