This is just a simple site to log someone in and out of a secured page and im having troble making it work. I have put comments in the php code what should happen but what I cant make work. Can someone please help me with it.
If you need to test it I included the SQL. Username: test Password: test
index.php
[php]
<?php
//If you are logged in then echo 'Super secret stuff!' else echo 'You are not logged in!' and show a button called 'Login' that on submit goes to the 'login.php' file.
//And when you are logged in then show a button called 'Logout' that on submit runs the 'logout.php' file.
?>
[/php]
login.php
[php]
<?php
include_once('auth.php');
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = hash('sha512', $_POST['password']);
$object = new auth();
$object->verify($username, $password);
}
?>
<!doctype html>
Login
Login
Username:
Password:
[/php]
auth.php
[php]
<?php
include_once('config.php');
class auth{
private $db;
public function __construct(){
$this->db = new connection();
$this->db = $this->db->dbConnect();
}
public function verify($username, $password){
if(!empty($username) && !empty($password)){
$st = $this->db->prepare('SELECT * FROM `accounts` WHERE username=? AND password=?');
$st->bindParam(1, $username);
$st->bindParam(2, $password);
$st->execute();
if($st->rowCount() == 1){
echo 'Found record!';
//Creates a session then echo 'Logged in!' redirects to 'index.php' after 3 seconds.
}else{
echo 'Incorret username or password!';
}
}else{
echo 'Please enter username and password!';
}
}
}
?>
[/php]
config.php
[php]
<?php
class connection{
public function dbConnect(){
return new PDO("mysql:host=localhost; dbname=login", "root", "*****");
}
}
?>
[/php]
logout.php
[php]
<?php
//Destorys the session and echos 'Logged out!' and redirects to the 'index.php' after 3 seconds.
?>
[/php]
accounts.sql
CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`username` varchar(32) NOT NULL COMMENT 'Username',
`password` char(128) NOT NULL COMMENT 'Password',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
INSERT INTO `accounts` (`id`, `username`, `password`) VALUES
(1, 'test', 'ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff');