Register and Login (Email Activation)
Posted: Tue Jul 05, 2011 10:01 pm
Hi guys I'm getting the following error from this tutorial:
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\advertise\core\inc\user.inc.php on line 41
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\advertise\core\inc\user.inc.php:41) in C:\xampp\htdocs\advertise\system\user\register.php on line 32
Now I guess the first part is caused by my localhost not sending e-mails correctly but what about the other one?
user.inc.php
[syntax=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 `user_name` = '{$user}'");
return (mysql_result($total, 0) == '1') ? true : false;
}
// Checks is 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; echo mysql_error();
}
// 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,
Thank you for registering, before you log-in you need to activate your account.
To do this simply click (or copy and paste) the link below:
http://localhost/advertise/system/user/ ... e.php?aid={aid}
EMAIL;
mail($email, 'Your New Account At My Site', $body, 'From:do_not_reply@betterphp.co.uk');
mysql_query("INSERT INTO `users` (`user_name`, `user_password`, `user_email`) VALUES ('{$user}', '{$pass}', '{$email}') "); echo mysql_error();
$user_id = mysql_insert_id();
mysql_query("INSERT INTO 'user_activations'('user_id', 'activation_code') VALUES ({$user_id}, '{$aid}' )");
}
?>[/syntax]
register.php
[syntax=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 (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false)
{
$errors[] = 'The e-mail address 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[] = 'Password verfication failed. Your two passwords must match.';
}
if (user_exists($_POST['username']))
{
$errors[] = 'The username you entered is already taken.';
}
if (empty($errors))
{
add_user($_POST['username'], $_POST['email'], $_POST['password']);
header('Location: user_cp.php');
die();
}
}
?>
<!DOCTYPE htlm PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-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><?php echo $page_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"email">E-Mail:</label>
<input type="test" name="email" id="email">
</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" />
</form>
</body>
</html>[/syntax]
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\advertise\core\inc\user.inc.php on line 41
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\advertise\core\inc\user.inc.php:41) in C:\xampp\htdocs\advertise\system\user\register.php on line 32
Now I guess the first part is caused by my localhost not sending e-mails correctly but what about the other one?
user.inc.php
[syntax=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 `user_name` = '{$user}'");
return (mysql_result($total, 0) == '1') ? true : false;
}
// Checks is 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; echo mysql_error();
}
// 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,
Thank you for registering, before you log-in you need to activate your account.
To do this simply click (or copy and paste) the link below:
http://localhost/advertise/system/user/ ... e.php?aid={aid}
EMAIL;
mail($email, 'Your New Account At My Site', $body, 'From:do_not_reply@betterphp.co.uk');
mysql_query("INSERT INTO `users` (`user_name`, `user_password`, `user_email`) VALUES ('{$user}', '{$pass}', '{$email}') "); echo mysql_error();
$user_id = mysql_insert_id();
mysql_query("INSERT INTO 'user_activations'('user_id', 'activation_code') VALUES ({$user_id}, '{$aid}' )");
}
?>[/syntax]
register.php
[syntax=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 (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false)
{
$errors[] = 'The e-mail address 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[] = 'Password verfication failed. Your two passwords must match.';
}
if (user_exists($_POST['username']))
{
$errors[] = 'The username you entered is already taken.';
}
if (empty($errors))
{
add_user($_POST['username'], $_POST['email'], $_POST['password']);
header('Location: user_cp.php');
die();
}
}
?>
<!DOCTYPE htlm PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-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><?php echo $page_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"email">E-Mail:</label>
<input type="test" name="email" id="email">
</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" />
</form>
</body>
</html>[/syntax]