Register and Login (Email Activation)

Post here is you are having problems with any of the tutorials.
Post Reply
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

Register and Login (Email Activation)

Post by twiggy »

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]
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

Re: Register and Login (Email Activation)

Post by twiggy »

I have no idea what just happened but its now working perfect :\ hmmm anyway onwards I go
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

Re: Register and Login (Email Activation)

Post by twiggy »

ok spoke too soon, the user details are being placed in the database and I can log-in with them but nothing is being added to the user_activations table, any ideas?
conradk
Posts: 117
Joined: Tue Jul 05, 2011 10:41 pm

Re: Register and Login (Email Activation)

Post by conradk »

twiggy wrote:ok spoke too soon, the user details are being placed in the database and I can log-in with them but nothing is being added to the user_activations table, any ideas?


I don't understand what you mean. I'd gladly help you but I'm not sure to get what your problem is. Could you detail this last post ? I would have known how to fix the second error message from your first post, but this last post I don't get :/

Best regards,
CK
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Register and Login (Email Activation)

Post by jacek »

The header error was just caused by the error above it so don't worry about that.

As for the new problem. If a query is failing a good step to try is adding

[syntax=php]echo mysql_error();[/syntax]
after it. This will give you the reason it fails.
Image
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

Re: Register and Login (Email Activation)

Post by twiggy »

Already tried the echo mysql_error(); I learnt that from you last time I had a problem ;) it wasn't showing anything just looping back.

Got it anyway I had named my table id and not user_id doh! :D
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Register and Login (Email Activation)

Post by jacek »

twiggy wrote:Got it anyway I had named my table id and not user_id doh! :D

Ah, easy to do. At least you spotted it :)
Image
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

Re: Register and Login (Email Activation)

Post by twiggy »

Ok before I stab someone, I have not changed a thing in the script, come back to it and get this:

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'm guessing my xampp install has gone wrong again? Is the top error causing the header problem?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Register and Login (Email Activation)

Post by jacek »

twiggy wrote: Is the top error causing the header problem?

jacek wrote:The header error was just caused by the error above it so don't worry about that.


Are you sure your SMTP server is running ?
Image
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

Re: Register and Login (Email Activation)

Post by twiggy »

I have Mercury running from the xampp control panel. This is one of two solutions to the problem I can find on the internet, the other is to check the ini file to ensure its pointing to local host and mine is.

SMTP localhost localhost
smtp_port 25 25

Any ideas will be welcome
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

Re: Register and Login (Email Activation)

Post by twiggy »

Got it! What a mess!

For anyone reading this thread later on the problem is with Mercury apparently there is a bug in a module or something that causes the SMTP to go offline. Simply go to file and click enter offline mode, cycle this option until the SMTP come back online.

Now I have no hair left, doe's anyone know where I can get a good wig :D
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Register and Login (Email Activation)

Post by jacek »

twiggy wrote:there is a bug in a module or something that causes the SMTP to go offline. Simply go to file and click enter offline mode, cycle this option until the SMTP come back online.

Well that isn't very good !

Good that you found a solution though :D
Image
Post Reply