Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HOMEPAGE ONCE LOGGED IN -->
- <?php
- require 'user.php';
- $fetch = new Connect();
- session_start();
- Session::get('user');
- if (!Session::get('user')) {
- echo "Session start didn't work.";
- }else {
- echo 'worked fine.';
- }
- ?>
- < -- ENDS --
- INDEX PAGE -->
- <?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="password" name="password">
- <input type="submit" name="submit" value="Login">
- </form>
- <!-- LOGIN ENDS -->
- <!-- 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>
- <-- INDEX PAGE ENDS
- USER CLASSES FILE STARTS -->
- <?php
- class Session {
- public static function set($key, $name)
- {
- $this->session = Session::start('user');
- $_SESSION[$key] = $name;
- }
- public static function get($key)
- {
- if (isset($_SESSION[$key])){
- return $_SESSION[$key];
- }
- else{
- return false;
- }
- }
- }
- class Connect
- {
- public function __construct()
- {
- try
- {
- $this->db = new PDO("mysql:host=127.0.0.1;dbname=testing",'root','root');
- }
- catch(PDOException $e)
- {
- echo $e->getMessage();
- }
- }
- public function login($name, $pass)
- {
- if(!empty($name) && !empty($pass))
- {
- $st = $this->db->prepare("SELECT password FROM users WHERE username= ?");
- $st->bindParam(1, $name);
- $st->execute();
- if($st->rowCount() == 1)
- {
- $db_pass = $st->fetch();
- if(password_verify($pass, $db_pass['password']))
- {
- $this->session;
- header ('Location: homepage.php');
- }
- else
- {
- echo "incorrect username or password.";
- }
- }
- 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)
- {
- $pass = password_hash($pass, PASSWORD_BCRYPT);
- echo $pass;
- $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.";
- }
- }
- }
- ?>
- <-- USER PAGE ENDS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement