After setting up this admin login system I found that I can't access to it with my Sql user/pass, the login page keeps rejecting my details. I've been trying to figure what's the problem here for 2 hours, I'll appreciate your assistance in this one!
function page (admin.php):
<?php
global $pdo;
function dbconnect()
{
global $pdo;
try {
$pdo = new PDO('mysql:host=localhost;dbname=pong','root','');
} catch (PDOException $e) {
die('connection failure! ' . $e->getMessage());
}
}
function attempt($username, $password)
{
global $pdo;
$stmt = $pdo->prepare('
SELECT id, username
FROM admin
WHERE username = :username AND password = :password
LIMIT 1');
$stmt->execute(array(':username' => $username, 'password' => md5($password)));
if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
$_SESSION['username'] = $data->username;
return true;
} else {
return false;
}
}
function is_user()
{
if (isset($_SESSION['username']))
return true;
}
function redirect($url)
{
header('Location: ' .$url);
exit;
}
Sign-in page (signin.php) :
<?php
require('admin.php');
session_start();
if (is_user()) {
redirect('../tv/game.php');
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sign In</title>
<link rel="stylesheet" type="text/css" href="../css/pure-min.css">
<link rel="stylesheet" type="text/css" href="../css/style.css">
</head>
<body>
<div class="container">
<h1>Sign In</h1>
<?php if (!empty($_GET['error'])): ?>
<p class="status status-error pure-input-1"><?php echo $_GET['error'] ?></p>
<?php endif ?>
<form action="signin_post.php" class="pure-form pure-form-stacked" method="post">
<fieldset class="pure-group">
<input type="text" class="pure-input-1" name="username" placeholder="username">
<input type="password" class="pure-input-1" name="password" placeholder="password">
<input class="pure-button pure-button-primary pure-input-1" type="submit" value="Sign In">
</fieldset>
</form>
</div>
</body>
</html>
And signin_post.php :
<?php
require('admin.php');
dbconnect();
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (attempt($_POST['username'], $_POST['password'])) {
header('Location:../tv/game.php');
}
else {
header('Location: signin.php?error=' . urlencode('invalid login details'));
}
}
Thanks in advance!