单击电子邮件激活链接后,php和mysql无法激活新用户

时间:2013-11-12 16:45:18

标签: php mysql

我正在用php创建一个注册/登录系统。我的代码能够通过电子邮件向新用户发送激活链接,但是当用户点击链接时,用户未被激活..请任何人都可以帮忙,代码如下...

users.php

function activate($email, $email_code) {
    $email          = mysql_real_escape_string($email);
    $email_code     = mysql_real_escape_string($email_code);

    if (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `user` WHERE `email` = '$email' AND `email_code` = '$emal_code' AND `active` = 0"), 0) == 1) {
        if (mysql_query("UPDATE `user` SET `active` = 1 WHERE `email` = '$email'")){
         return true; 
    } else {
         return false;  
    }

}
}

function register_user ($register_data) {
    array_walk($register_data, 'array_sanitize');
    $register_data['password'] = md5($register_data['password']);

    $fields = '`' . implode('`, `', array_keys ($register_data)) . '`';
    $data   = '\'' . implode('\', \'', $register_data) . '\'';

    mysql_query("INSERT INTO `user` ($fields) VALUES ($data)");
    email($register_data['email'], 'Activate your account', "Hello " .      $register_data['username'] . ",\n\nYou need to activate your account by clicking the link below:\n\nhttp://fredhosting.com/real/activate.php?email=" . $register_data['email'] . "&email_code=" . $register_data['email_code'] . "\n\n - Fredhosting.com");
}

Activate.php

if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
?>
    <h2>Thanks, we have activated your account...</h2>
    <p>You can now Log in!</p>
<?php
} else if (isset($_GET['email'], $_GET['email_code']) === true) {

    $email       = trim($_GET['email']);
    $email_code  = trim($_GET['email_code']);

    if (email_exists($email) === false) {
        $errors[] = 'Oops, something went wrong and we could not find that email address.';
    } else if (activate($email, $email_code) === false) {
        $errors[] = 'We had some issues activating your account.';
    }

    if (empty($errors) === false) {
    ?>
        <h2>Oops...</h2>
    <?php
        echo output_errors($errors);
    } else {
        header('Location: activate.php?success');
        exit(); 
    }

}

1 个答案:

答案 0 :(得分:1)

错误:

if (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM 
`user` WHERE `email` = '$email' 
AND `email_code` = '$emal_code' 
AND `active` = 0"), 0) == 1)
  

不正确的变量$ emal_code

然而,在您多次重新发布代码之前,通常会出现拼写错误!

您似乎正在关闭PHP错误,请激活推荐它们。

ini_set('display_errors', '1');
error_reporting(E_ALL);