Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Connect
- {
- public function __construct()
- {
- try
- {
- $this->db = new PDO("mysql:host=localhost;dbname=aaron_test",'aaron_test','d6G4X5S54');
- }
- catch(PDOException $e)
- {
- echo $e->getMessage();
- }
- }
- public function login($name, $pass)
- {
- if(!empty($name) && !empty($pass))
- {
- $st = $this->db->prepare("SELECT * FROM users WHERE username= ? and password= ?");
- $st->bindParam(1, $name);
- $st->bindParam(2, $pass);
- $st->execute();
- if($st->rowCount() == 1)
- {
- echo "Access Granted.";
- }
- else
- {
- echo "incorrect username or password.";
- }
- }
- else
- {
- echo "Please supply a username and password.";
- }
- }
- public function register($name, $pass)
- {
- if(!empty($name) && !empty($pass))
- {
- $get = $this->db->prepare("SELECT * FROM users WHERE username = ?");
- $get->bindParam(1, $name);
- $get->execute();
- if($get->rowCount() == 0)
- {
- $set = $this->db->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
- $set->bindParam(1, $name);
- $set->bindParam(2, $pass);
- $set->execute();
- echo "Your details have now been registered.";
- }
- else
- {
- echo "An account with this username already exists.";
- }
- }
- else
- {
- echo "Please fill in all of the fields.";
- }
- }
- }
- ?>
- ///// INDEX.PHP BELOW /////
- <?php
- require 'user.php';
- if(isset($_POST['submit']))
- {
- $name = $_POST['user'];
- $pass = $_POST['password'];
- $object = new Connect();
- $object->login($name, $pass);
- }
- if(isset($_POST['register']))
- {
- $name = $_POST['reg_user'];
- $pass = $_POST['reg_pass'];
- $reg = new Connect();
- $reg->register($name, $pass);
- }
- ?>
- <!-- LOGIN STARTS -->
- <h2>Login</h2>
- <form action="" method="POST">
- Username: <input type="text" name="user">
- Password: <input type="text" name="password">
- <input type="submit" name="submit" value="Login">
- </form>
- <!-- REGISTER STARTS -->
- <h2>Register</h2>
- <form action="" method="POST">
- Username: <input type="text" name="reg_user">
- Password: <input type="text" name="reg_pass">
- <input type="submit" name="register" value="Register">
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement