Heres all my code: I've been looking and searching everywhere for a fix D:
Edit: username and password to login are: test
Url:
http://tumabackup.x10.mx/
login.php:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('core/init.inc.php');
$errors = array();
echo mysql_error();
if (isset($_POST['username'], $_POST['password'])){
if (empty($_POST['username'])){
$errors[] = 'The username cannot be empty.';
}
if (empty($_POST['password'])){
$errors[] = 'The password cannot be empty.';
}
if (empty($errors) && valid_credentials($_POST['username'], $_POST['password']) === false){
$errors[] = 'Username or password are incorrect.';
}
if (empty($errors)){
$_SESSION['username'] = htmlentities($_POST['username']);
header('Location: protected.php');
die();
}
echo mysql_error();
}
echo mysql_error();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>NewCo | Login</title>
<link rel="shortcut icon" href="favicon.ico">
<link href='http://fonts.googleapis.com/css?family= ... cento+Sans' rel='stylesheet' type='text/css'>
<link href="/ext/style/style-login.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div>
<?php
if (empty($errors) === false){
?>
<ul>
<?php
foreach ($errors as $error){
echo "<li>{$error}</li>";
}
?>
</ul>
<?php
}else{
echo 'Need an account ? <a href="index.php">Register here</a>';
}
?>
</div>
<form action="" method="post">
<p>
<label for="name">Username:</label>
<input type="text" id="name" placeholder="Username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
</p>
<p>
<label for="password">Password:</label>
<input type="password" name="password" id="password" />
</p>
<p>
<input type="submit" value="Submit" />
</p>
</form>
</body>
</html>
index.php (register page):
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('core/init.inc.php');
$errors = array();
if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){
if (empty($_POST['username'])){
$errors[] = 'The username cannot be empty.';
}
if (empty($_POST['password']) || empty($_POST['repeat_password'])){
$errors[] = 'The password cannot be empty.';
}
if ($_POST['password'] !== $_POST['repeat_password']){
$errors[] = 'Passwords dont match!';
}
if (user_exists($_POST['username'])){
$errors[] = 'Username already exists!';
}
if (empty($errors)){
add_user($_POST['username'], $_POST['password']);
$_SESSION['username'] = htmlentities($_POST['username']);
header('Location: protected.php');
die();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>NewCo | Sign Up</title>
<link rel="shortcut icon" href="favicon.ico">
<link href='http://fonts.googleapis.com/css?family= ... cento+Sans' rel='stylesheet' type='text/css'>
<link href="/ext/style/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="contact">
<h1>Under Construction.</h1>
<p><a href="login.php">Login</a></p>
<form action="/" method="post">
<input type="text" name="username" id="username" placeholder="Username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
<input type="password" name="password" id="password" placeholder="Password" />
<input type="password" name="repeat_password" id="repeat_password" placeholder="Re-Type Password">
<input type="submit" value="Submit" />
</form>
<?php
if (empty($errors) === false){
?>
<ul>
<?php
foreach ($errors as $error){
echo "<li>{$error}</li>";
}
?>
</ul>
<?php
}
?>
</div>
</body>
</html>
protected.php:
<?php include('core/init.inc.php'); ?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>NewCo | Welcome</title>
</head>
<body>
<p>
You are logged in as <?php echo $_SESSION['username']; ?>
</p>
<p>
<a href="logout.php">Logout?</a>
</p>
</body>
</html>
init.inc.php:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
$exceptions = array('index','login');
$page = substr(end(explode('/',$_SERVER['SCRIPT_NAME'])), 0, -4);
if (in_array($page, $exceptions) === false){
if (isset($_SESSION['username']) === false){
header('Location: login.php');
die();
}
}
mysql_connect('localhost', 'tumaback_user', 'root');
mysql_select_db("tumaback_system_user");
$path = dirname(__FILE__);
include("{$path}/inc/users.inc.php");
?>
users.inc.php:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Checks if the given user name exists in the database.
function user_exists($user){
$user = mysql_real_escape_string($user);
$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_name`= '{$user}'");
echo mysql_error();
return (mysql_result($total, 0) == '1') ? true : false;
}
// Checks if the given username and password combination is valid.
function valid_credentials($user, $pass){
$user = mysql_real_escape_string($user);
$pass = sha1($pass);
$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_name`= '{$user}' AND `User_password` = '{$pass}' ");
return (mysql_result($total, 0) == '1') ? true : false;
}
// Adds a user to the database.
function add_user($user, $pass){
$user = mysql_real_escape_string(htmlentities($user));
$pass = sha1($pass);
mysql_query("INSERT INTO `users` (`user_name`, `user_password`) VALUES ('{$user}', '{$pass}')");
}
?>