Before the login and register pages wouldn't show, users were not added to the database, and you were unable to log in, is that because I am using localhost instead of 127.0.0.1?
init.inc.php
<?php
session_start();
$exceptions = array('register', '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', 'coolrob1_grange', '*hidden*');
mysql_select_db('coolrob1_grange');
$path = dirname(__FILE__);
include("{$path}/inc/user.inc.php");
?>
user.inc.php
<?php
//checks if the given username exists in the database
function user_exists($user){
$user = mysql_real_escape_string($user);
$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '{$user}'");
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 `username` = '{$user}' AND '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` (`username`, `password`) VALUES ('{$user}', '{$pass}')");
}
?>
login.php
<?php include('core/init.inc.php');
$errors = array();
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 / Password incorrect';
}
if (empty($errors)){
$_SESSION['username'] = htmlentities($_POST['username']);
header('Location: user.php');
die();
}
}
?>
<?php include 'core/head.php'; include 'core/nav.php'; ?>
<div class="container">
<h1>Login</h1>
<br/>
<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="register.php">Register here</a>';
}
?>
</div>
<form action="" method="post">
<p> <label for="username">Username:</label>
<input type="text" name="username" id="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="Login" /></p>
</form>
</div>
<?php include 'core/footer.php'; ?>
logout.php
<?php
session_start();
$_SESSION = array();
session_destroy();
header('Location: user.php');
?>
register.php
<?php 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 must match';
}
if (user_exists($_POST['username'])){
$errors[] = 'The username is already taken';
}
if (empty($errors)){
add_user($_POST['username'], $_POST['password']);
$_SESSION['username'] = htmlentities($_POST['username']);
header('Location: user.php');
die();
}
}
?>
<?php include 'core/head.php'; include 'core/nav.php'; ?>
<div>
<?php
if (empty(errors) === false){
?>
<ul>
<?php
foreach ($errors as $error){
echo "<li>{$error}</li>"
}
?>
</ul>
<?php
}
?>
</div>
<div class="container">
<h1>Register</h1>
<br/>
<form action="" method="post">
<p> <label for="username">Username:</label>
<input type="text" name="username" id="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> <label for="repeat_password">Repeat Password:</label>
<input type="password" name="repeat_password" id="repeat_password" />
</p>
<p> <input type="submit" value="Register" /></p>
</form>
</div>
<?php include 'core/footer.php'; ?>
The live site is at http://grange.robbrazier.com (just lorem ipsum so far I put all of the pages in the nav bar for ease of access temporarily
