SHARE
TWEET

Done :D

a guest Feb 4th, 2016 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class Connect
  3. {
  4.  
  5. public function __construct()
  6.     {
  7.         try
  8.         {
  9.             $this->db = new PDO("mysql:host=localhost;dbname=aaron_test",'aaron_test','d6G4X5S54');
  10.         }
  11.             catch(PDOException $e)
  12.         {
  13.             echo $e->getMessage();
  14.         }
  15.     }
  16.  
  17. public function login($name, $pass)
  18.     {
  19.         if(!empty($name) && !empty($pass))
  20.         {
  21.             $st = $this->db->prepare("SELECT * FROM users WHERE username= ? and password= ?");
  22.             $st->bindParam(1, $name);
  23.             $st->bindParam(2, $pass);
  24.             $st->execute();
  25.  
  26.             if($st->rowCount() == 1)
  27.             {
  28.                 echo "Access Granted.";
  29.             }
  30.             else
  31.             {
  32.                 echo "incorrect username or password.";
  33.             }
  34.         }
  35.         else
  36.         {
  37.             echo "Please supply a username and password.";
  38.         }
  39.     }
  40. public function register($name, $pass)
  41.     {
  42.         if(!empty($name) && !empty($pass))
  43.         {
  44.             $get = $this->db->prepare("SELECT * FROM users WHERE username = ?");
  45.             $get->bindParam(1, $name);
  46.             $get->execute();
  47.  
  48.             if($get->rowCount() == 0)
  49.             {
  50.                 $set = $this->db->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
  51.                 $set->bindParam(1, $name);
  52.                 $set->bindParam(2, $pass);
  53.                 $set->execute();
  54.  
  55.                 echo "Your details have now been registered.";
  56.             }
  57.             else
  58.             {
  59.                 echo "An account with this username already exists.";
  60.             }
  61.         }
  62.         else
  63.         {
  64.             echo "Please fill in all of the fields.";
  65.         }
  66.     }
  67. }
  68.  
  69. ?>
  70.  
  71. ///// INDEX.PHP BELOW /////
  72.  
  73. <?php
  74. require 'user.php';
  75.  
  76. if(isset($_POST['submit']))
  77. {
  78.     $name = $_POST['user'];
  79.     $pass = $_POST['password'];
  80.  
  81.     $object = new Connect();
  82.     $object->login($name, $pass);
  83. }
  84.  
  85. if(isset($_POST['register']))
  86. {
  87.     $name = $_POST['reg_user'];
  88.     $pass = $_POST['reg_pass'];
  89.  
  90.     $reg = new Connect();
  91.     $reg->register($name, $pass);
  92. }
  93. ?>
  94.  
  95. <!-- LOGIN STARTS -->
  96. <h2>Login</h2>
  97.  <form action="" method="POST">
  98.     Username: <input type="text" name="user">
  99.     Password: <input type="text" name="password">
  100.     <input type="submit" name="submit" value="Login">
  101.  </form>
  102.  
  103. <!-- REGISTER STARTS -->
  104. <h2>Register</h2>
  105.  <form action="" method="POST">
  106.     Username: <input type="text" name="reg_user">
  107.     Password: <input type="text" name="reg_pass">
  108.     <input type="submit" name="register" value="Register">
  109.  </form>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top