Advertisement
lowheartrate

My login and registration

Jun 4th, 2015
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.67 KB | None | 0 0
  1. // index.php
  2.  
  3. <?php
  4. // Stops errors from showing
  5. error_reporting(0);
  6.  
  7. session_start();
  8.     if(isset($_POST['username'], $_POST['password'])){
  9.      require 'core/connect.php';
  10.  
  11.     $query = dbConnect()->prepare("SELECT username, password FROM Users WHERE username=:username AND password=:password");
  12.     $query->bindParam(':username', $_POST['username']);
  13.     $query->bindParam(':password', md5($_POST['password']));
  14.     $query->execute();
  15.  
  16.     if($row = $query->fetch()){
  17.     $_SESSION['username'] = $row['username'];
  18.     $_SESSION['password'] = $row['password'];
  19.     header("Location: index.php");
  20.     } else {
  21.         echo '<p style="text-align: center; color: red;">Invalid username/password</p>';
  22.     }
  23.  
  24.     if($row = $query->fetch()){
  25.         $_SESSION['password'] = $row['password'];
  26.         header("Location: index.php");
  27.         }
  28.     }
  29.  
  30.     if(isset($_SESSION['username'])) {
  31.                 echo '
  32.                 <div style="max-width: 90%; text-align: center; margin: 0 auto;">
  33.                 <img src="http://i.imgur.com/MBuIZ3h.png" width="75%" style="text-align: center; margin: 10px 0;">
  34.                 <p style="margin-left: 8px; font-size: 24px;">Welcome, <strong>'.$_SESSION['username'].'</strong><br></p><p style="margin-top: -20px; margin-left: 10px;"><a href="logout.php">Logout</a></p>
  35.                 </div>';
  36.             } else {
  37.                 echo '
  38.                 <div style="max-width: 90%; text-align: center; margin: 0 auto;">
  39.                     <img src="http://i.imgur.com/MBuIZ3h.png" width="75%" style="text-align: center; margin: 10px 0;">
  40.                     <form method="POST" style="text-align: center; margin: 0 auto;">
  41.                         <input style="width: 100%; margin: 5px 0;" type="text" name="username" placeholder="Username"><br />
  42.                         <input style="width: 100%;" type="password" name="password" placeholder="Password"><br />
  43.                         <input style="margin-top: px; width: 100%;" type="Submit" value="Login"><br>
  44.                         <br>
  45.                         <a href="http://www.heartfx.org/registeraccount.php"><p style="margin-top: -10px;">Register an account</p></a>
  46.                     </form>
  47.                 </div>';
  48.             }
  49.         ?>
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. // core/connect.php
  64.  
  65.  
  66. <?php
  67.     function dbConnect(){
  68.         try{
  69.             $dbuser = '';
  70.             $dbpw = '';
  71.             $conn = new pdo("mysql:host=198.71.225.55:3306;dbname=heartfx_users;", $dbuser, $dbpw);
  72.             $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  73.             return $conn;
  74.  
  75.         }   catch(PDOException $e){
  76.             echo 'ERROR', $e->getMessage();
  77.         }
  78.     }
  79. ?>
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. // register.php
  92.  
  93.  
  94. <form method="POST">
  95.     <?php
  96.     if (strlen(':username')<6) {
  97.         echo "Username must be atleast 6 characters";
  98.     } else {
  99.         echo '<button type="submit" style="width: 100%; padding: 10px 5px;">Register account</button><br><br>';
  100.     }
  101.     ?>
  102. </form>
  103.         </div>
  104.  
  105.         <br><br>
  106.  
  107.         <?php
  108.         session_start();
  109.  
  110.         if(isset($_POST['firstname'],$_POST['lastname'], $_POST['username'], $_POST['password'], $_POST['email'])){
  111.             require 'core/connect.php';
  112.  
  113.             $query = dbConnect()->prepare("INSERT INTO Users (firstname, lastname, username, password, email) VALUES (:firstname, :lastname, :username, :password, :email)");
  114.             $query->bindParam(':lastname', $_POST['lastname']);
  115.             $query->bindParam(':firstname', $_POST['firstname']);
  116.             $query->bindParam(':username', $_POST['username']);
  117.             $query->bindParam(':password', md5($_POST['password']));
  118.             $query->bindParam(':email', $_POST['email']);
  119.  
  120.             if (strlen($_POST['username'])<6) {
  121.                 echo 'Username must be at least 6 characters';
  122.             } else {
  123.                 header("Location: index.php");
  124.             }
  125.  
  126.             if($query->execute()){
  127.                 header("Location: registeredaccount.php");
  128.             } else {
  129.                 echo 'ERROR';
  130.             }
  131.         }
  132.         ?>
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. // logout.php
  140.  
  141.  
  142. <?php
  143.     session_start();
  144.     session_destroy();
  145. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement