i followed the register and login tutorials(user account system, cookie extension and email activation) three times but it is not working at some points.
The remember me box is not working, that is not the most important issue but after register there is no user_id and activation_code added in the user_activations table. I can't find out why, everything looks oke by me but somewhere it isn't right.
I also checked off course the database but i can't find anything wrong there as well.
init.inc.php
<?php
session_start();
$exceptions = array('register', 'login', 'activate');
$page = substr(end(explode('/', $_SERVER['SCRIPT_NAME'])), 0, -4);
$connection = mysql_connect("localhost","username","password");
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db("database",$connection);
$path = dirname(__FILE__);
include("{$path}/inc/user.inc.php");
if (!$db_select) {
die("database selection failed: " . mysql_error());
}
if (isset($_COOKIE['username'], $_COOKIE['password']) && isset($_SESSION['username']) === false){
if (valid_credentials($_COOKIE['username'], $_COOKIE['password'])){
$_SESSION['username'] = htmlentities($_COOKIE['username']);
setcookie('username', $_COOKIE['username'], time() + 684800);
setcookie('password', $_COOKIE['password'], time() + 684800);
}
}
if (in_array($page, $exceptions) === false){
if (isset($_SESSION['username']) === false){
header('location: login.php');
die();
}
}
?> register.php
<?php
include('init.inc.php');
$errors = array();
if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){
if (empty($_POST['username'])){
$errors[] = 'De gebruikersnaam mag niet leeg zijn.';
}
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
$errors[] = 'De ingevulde emailadres is niet goed.';
}
if (empty($_POST['password']) || empty($_POST['repeat_password'])){
$errors[] = 'Het paswoord is niet ingevuld.';
}
if ($_POST['password'] !== $_POST['repeat_password']){
$errors[] = 'De paswoord vereficatie was niet correct';
}
if (user_exists($_POST['username'])){
$errors[] = 'De gebruikersnaam is al in gebruik';
}
if (empty($errors)){
add_user($_POST['username'], $_POST['email'], $_POST['password']);
header('location: beschermd.php');
die();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</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"> Gebruikersnaam;</label>
<input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
</p>
<p>
<label for="email"> Email;</label>
<input type="text" name="email" id="email" value="<?php if (isset($_POST['email'])) echo htmlentities($_POST['email']); ?>" />
</p>
<p>
<label for="password"> Paswoord;</label>
<input type="password" name="password" id="password" />
</p>
<p>
<label for="password">Herhaal paswoord;</label>
<input type="password" name="repeat_password" id="repeat_password" />
</p>
<p>
<input type="submit" value="Registreer" />
</p>
</form>
</body>
</html>
user.inc.php
<?php
//bestaat de gebruikersnaam in de database
function user_exists($user){
$user = mysql_real_escape_string($user);
$total = mysql_query("SELECT COUNT(`user_id`) FROM `user_system` WHERE `user_name` = '{$user}'");
return (mysql_result($total, 0) == '1') ? true : false;
}
//is de gebruikersnaam en paswoord combinatie correct
function valid_credentials($user, $pass){
$user = mysql_real_escape_string($user);
$pass = mysql_real_escape_string($pass);
$total = mysql_query("SELECT COUNT(`user_id`) FROM `user_system` WHERE `user_name` = '{$user}' AND `user_password` = '{$pass}'");
return (mysql_result($total, 0) == '1') ? true : false;
}
function is_active($user){
$user = mysql_real_escape_string($user);
$sql = "SELECT
COUNT(`user_activations`.`user_id`)
FROM `user_system`
INNER JOIN `user_activations`
ON `user_system`.`user_id` = `user_activations`.`user_id`
WHERE `user_system`.`user_name` = '{$user}'";
$result = mysql_query($sql);
return (mysql_result($result, 0) == '0') ? true : false;
}
function activate_account($aid){
$aid = mysql_real_escape_string($aid);
mysql_query("DELETE FROM `user_activations` WHERE `activation_code` = '{$aid}'");
}
//paswoord vergeten
function random_string($length){
$charset = array_flip(array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9)));
shuffle($charset);
$password = array_slice($charset, 0, $length);
return implode('', $password);
}
//voegt een gebruiker toe aan de database
function add_user($user, $email, $pass){
$user = mysql_real_escape_string(htmlentities($user));
$email = mysql_real_escape_string($email);
$pass = sha1($pass);
$charset = array_flip(array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9)));
$aid = implode('', array_rand($charset, 10));
$body = <<<EMAIL
Hallo,
dank voor het registreren, voordat je gaat inloggen is het nodig dat je je account gaat activeren.
omdat de doen kan je gewoon op deze link klikken, http://www.robcnossen.nl/activate.php?aid={$aid}
EMAIL;
mail($email, 'je nieuwe account in robbcnossen.nl', $body, 'From: emailadres@email.nl');
mysql_query("INSERT INTO `user_system` (`user_name`, `user_password`, `user_email`) VALUES ('{$user}', '{$pass}', '{$email}')");
$user_id = mysql_insert_id();
mysql_query("INSERT INTO `user_activations` (`user_id`, `activation_code`) VALUES ('{$user_id}', '{$aid}')");
}
?>
login.php
<?php
include('init.inc.php');
$errors = array();
if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){
if (empty($_POST['username'])){
$errors[] = 'De gebruikersnaam mag niet leeg zijn.';
}
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
$errors[] = 'De ingevulde emailadres is niet goed.';
}
if (empty($_POST['password']) || empty($_POST['repeat_password'])){
$errors[] = 'Het paswoord is niet ingevuld.';
}
if ($_POST['password'] !== $_POST['repeat_password']){
$errors[] = 'De paswoord vereficatie was niet correct';
}
if (user_exists($_POST['username'])){
$errors[] = 'De gebruikersnaam is al in gebruik';
}
if (empty($errors)){
add_user($_POST['username'], $_POST['email'], $_POST['password']);
header('location: beschermd.php');
die();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</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"> Gebruikersnaam;</label>
<input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
</p>
<p>
<label for="email"> Email;</label>
<input type="text" name="email" id="email" value="<?php if (isset($_POST['email'])) echo htmlentities($_POST['email']); ?>" />
</p>
<p>
<label for="password"> Paswoord;</label>
<input type="password" name="password" id="password" />
</p>
<p>
<label for="password">Herhaal paswoord;</label>
<input type="password" name="repeat_password" id="repeat_password" />
</p>
<p>
<input type="submit" value="Registreer" />
</p>
</form>
</body>
</html>
activate.php
<?php
include('init.inc.php');
if (isset($_GET['aid'])){
activate_account($_GET['aid']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p>
Je account is geactiveerd, je kan nu <a href="login.php">log in</a>
</p>
</body>
</html>
I hope somebody can see what is wrong here.Thanks
