Page 1 of 1

register and login system; no data in user_activation table

Posted: Mon Apr 02, 2012 12:38 pm
by Robbedoesie
Hello,
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

Re: register and login system; no data in user_activation ta

Posted: Tue Apr 03, 2012 1:40 am
by jacek
Looks okay to me :?

Try adding
echo mysql_error();
after the two insert queries in the add_user() function.

If the query is failing for some reason that will tell you why.

Re: register and login system; no data in user_activation ta

Posted: Tue Apr 03, 2012 9:13 pm
by Robbedoesie
And then off course all the data appears in the database. I don't know how but it is there. Maybe i saved it strangely but i think my computer is teasing me.

The next step is get the email in my mailbox. Somehow it is not getting there. I have to get there step by step, small steps.

I watched the email activation part again a few times but i can't see what is wrong and my emailadress is written right ;) .
Can you see what is wrong?

Well, maybe my computer gonna think one time that he teased me enough with this and show the mail in my mailbox.

Thanks,
Robbedoesie

Re: register and login system; no data in user_activation ta

Posted: Wed Apr 04, 2012 5:46 pm
by Robbedoesie
I tried this on localhost. I am gonna try to reach my mailbox with a webhost. If nothing chancing i am coming back to your forum.

Greetings, Robbedoesie

Re: register and login system; no data in user_activation ta

Posted: Thu Apr 05, 2012 1:04 pm
by jacek
Sometimes ISPs will block emails being sent using their network to prevent spammer using it. It should work when you upload the files.