I am having trouble with this code:
This is the init.inc.php file
<?php
//init.inc.php file
session_start();
$exceptions = array('signup', 'login', 'index');
$page = substr(end(explode( '/', $_SERVER['SCRIPT_NAME'])), 0, -4);
// SQL stuff.
mysql_connect('SQL host','username','password');
mysql_select_db('database name');
include('user.inc.php');
//$_SESSION['uid'] = 1;
if (isset($_COOKIE['username'], $_COOKIE['password'])){
if (valid_credentials($_COOKIE['username'], $_COOKIE['password'])){
$_SESSION['username'] = htmlentities($_COOKIE['username']);
setcookie('username', $_COOKIE['username'], time() + 3600);
setcookie('password', $_COOKIE['password'], time() + 3600);
}
}
if (in_array($page, $exceptions) === false){
if (isset($_SESSION['username']) === false){
header( 'Location: login.php' );
die();
}
}
This is the login.php file:
<?php
include('init.inc.php');
$errors = array();
if (isset($_POST['username'], $_POST['password'])){
if (empty($_POST['username'])){
$errors[] = 'The username field is empty.';
}
if (empty($_POST['password'])){
$errors[] = 'The password field is empty.';
}
if (valid_credentials($_POST['username'], sha1($_POST['password'])) === false){
$errors[] = 'Username or password is incorrect.';
}
if (empty($errors)){
setcookie('username', $_POST['username'], time() + 3600);
setcookie('password', sha1($_POST['password']), time() + 3600);
$username = $_POST['username'];
session_register("username");
header( 'Location: home.php' );
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<body>
<form action="" method="post">
<p>
<?php
if (empty($errors) === false){
?>
<ul>
<?php
foreach ($errors as $error){
echo "<center><li>[$error]</li></center>";
}
?>
</ul>
<?
}
?>
</p>
<input type="text" name="username" id="username" placeholder="Username" maxlength="40" value="<?php if (isset($_POST['username'])){ echo htmlentities($_POST['username']); } ?>"><br><br>
<input type="password" name="password" placeholder="Password"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
The problem with this code is that it says my username/password is incorrect even though it is.......IDK what to do....Be glad if someone helped..