Page 1 of 1

Email Activation

Posted: Fri Feb 10, 2012 7:32 am
by JuiceYum
I'm back again :P

Anyway straight to the point I've got troubles with the email activation and they are that it's not going into the database, registering the user or redirecting to login.php. Heres my code:

Index.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 (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
		$errors[] = 'The email adress you entered does not appear to be valid.';
	}
	
	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['email'], $_POST['password']);
		
		
		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/style1.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="text" name="email" id="email" placeholder="Email" value="<?php if (isset($_POST['email'])) echo htmlentities($_POST['email']); ?>" />

			<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>
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);

mysql_connect('localhost', 'tumaback_user', 'root');
mysql_select_db("tumaback_system_user");

$path = dirname(__FILE__);

include("{$path}/inc/users.inc.php");

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() + 604800);
		setcookie('password', $_COOKIE['password'], time() + 604800);
	}
}

if (in_array($page, $exceptions) === false){
	if (isset($_SESSION['username']) === false){
		header('Location: login.php');
			die();
	}
}


?>
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 = mysql_real_escape_string($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, $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
	
	Hi,
	
	Thanks for registring, before you login you need to activate you account.
	
	To do that simply click the following link.
	
	http://tumabackup.x10.mx/activate.php?aid={$aid}
	
EMAIL;

	mail($email, 'Your new account at tumabackup.com', $body, 'From: admin@tumabackup.x10.m1');
	
	mysql_query("INSERT INTO `users` (`user_name`, `user_password`, `user_email`) VALUES ('{$user}', '{$pass}', '{$email}')");
	
	$user_id = mysql_insert_id();
	
	mysql_query("INSET INTO `user_activations` (`user_id`, `activation_code`) VALUES ({$user}, '')");
}

?>
Ive done the tests tremor said to do in one of his comments and still nothing. Also I get no errors.

Re: Email Activation

Posted: Fri Feb 10, 2012 3:28 pm
by Temor
Your last line in user.inc.php.
 mysql_query("INSET INTO `user_activations` (`user_id`, `activation_code`) VALUES ({$user}, '')");
You missed an R in INSERT.
And are you sure the rest of that line is correct? You're inserting a string ( the username ) into the user_id field and you're not inserting anything into the activation_code field.

Echoing mysql_error(); under the failing query should give you detailed error messages if your query is invalid.