thank you so much for the tutorial! It is exactly what I have been looking for and it was a breeze following you through the 5 parts. Unfortunately it seems as if my php/connection to database isn't really working.
At first it was displaying the html just plain - meaning if I typed in something and hit enter it didn't do anything but loading a white page.
Then I must have changed something in the init.inc.php because now it doesn't do anything except displaying a plain white page when I open one of the documents (in firefox and safari). In Chrome I get the following:
![Image](http://www.upload-pictures.de/bild.php/27916,bildschirmfoto20130326um110534NLJCR.png)
It would be great if you guys could help me!
Than you so much again,
Jasper
//edited: replaced ' with `
//edited: Includes error reporting and few less minor code improvements
Login.php
[syntax=php]<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
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 (valid_credentials($_POST['username'], $_POST['password']) === false){
$errors[] = 'Username / Password incorrect.';
}
if (empty($errors)){
$_SESSION['username'] = htmlentities($_POST['username']);
header('Location: protected.php');
die();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content=text/html; charset=utf-8" /">
<link rel="stylesheet" type"text/css" href="ext/css/style.css" />
<title></title>
</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="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>
</body>
</html>[/syntax]
Logout.php
[syntax=php]<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
$_SESSION = array();
session_destroy();
header('Location: protected.php');
?>[/syntax]
Register.php
[syntax=php]<?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[] = 'The password verification failed.';
}
if (user_exists($_POST['username'])){
$errors[] = 'The username you entered is already taken.';
}
if (empty($errors)){
add_user($_POST['username'], $_POST['password']);
$_SESSION['username'] = htmlentities($_POST['username']);
header('Location: protected.php');
die();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content=text/html; charset=utf-8" /">
<link rel="stylesheet" type"text/css" href="ext/css/style.css" />
<title></title>
</head>
<body>
<div>
<?php
if (empty($errors) === false){
?>
<ul>
<?php
foreach ($errors as $error){
echo "<li>{$error}</li>";
}
?>
</ul>
<?php
}
?>
</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>
<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>
</body>
</html>[/syntax]
init.inc.php
[syntax=php]<?php
ob_start();
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
$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', 'root', 'root');
mysql_select_db('user_system');
$path = dirname(__FILE__);
include("{$path}/inc/user.inc.php");
?>[/syntax]
user.inc.php
[syntax=php]<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
//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 `user_name` = '{$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 = shal($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 = shal($pass);
mysql_query("INSERT INTO `users` (`user_name`, `user_password`) VALUES ('{$user}', '{$pass}')");
}
?>[/syntax]
protected.php
[syntax=php]<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include('core/init.inc.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<p>
You are logged in as <?php echo $_SESSION['username']; ?>
</p>
<p>
<a href="logout.php">Logout?</a>
</p>
</body>
</html>
[/syntax]