确认电子邮件验证

时间:2013-07-28 20:57:27

标签: php database email

我正在尝试让我的电子邮件验证工作。一切正常,只要发送带有哈希链接的电子邮件进行确认,但一旦进入下面的verify.php链接,它就不会将我的数据库活动行从0更新为1.有任何建议吗?

if(isset($_GET['email']) && !empty($_GET['email']) AND isset($_GET['email_hash']) && !empty($_GET['email_hash'])){  
    // Verify data  
    $search = "SELECT email, email_hash, active FROM users WHERE email='".$email."' AND hash='".$email_hash."' AND active='0'";
    $match = $database->num_rows( $query );

    if($match > 0){  

        //Fields and values to update
        $update = array(
            'active' => 1 
        );
        //Add the WHERE clauses
        $where_clause = array(
            'email' => '$email', 
            'email_hash' => '$email_hash',
            'active' => '1'
        );
        $updated = $database->update( 'users', $update, $where_clause, 1 );
        if( $updated )
        {
            echo '<p>Your account has been activated, you can now login</p>';
        }
    }

}else{  
    echo '<p>Your account is already activated</p>';
}  

4 个答案:

答案 0 :(得分:1)

您的代码不正确(使用$ email / $ email_hash但未声明)这就是它的工作原理:

if(isset($_GET['email']) && !empty($_GET['email']) AND isset($_GET['email_hash']) && !empty($_GET['email_hash'])){  
    // Verify data  
    $email = $_GET['email'];
    $email_hash= $_GET['email_hash'];

    $search = "SELECT email, email_hash, active FROM users WHERE email='".$email."' AND hash='".$email_hash."' AND active='0'";
    $match = $database->num_rows( $query );

    if($match > 0){  

        //Fields and values to update
        $update = array(
            'active' => 1 
        );
        //Add the WHERE clauses
        $where_clause = array(
            'email' => '$email', 
            'email_hash' => '$email_hash',
            'active' => '1'
        );
        $updated = $database->update( 'users', $update, $where_clause, 1 );
        if( $updated )
        {
            echo '<p>Your account has been activated, you can now login</p>';
        }
    }

}else{  
    echo '<p>Your account is already activated</p>';
}

我想补充一点,在生产阶段,你必须转义并验证所有传入的数据(POST,GET等)。

答案 1 :(得分:1)

您应该在if子句中定义$ email和$ email_hash。

$email = $_GET['email'];
$email_hash = $_GET['email_hash'];

目前,您依赖于一个名为register_globals的弃用指令。

答案 2 :(得分:1)

你正在做一些非常明显的错误。首先,你应该打开错误。 您必须使用error_reporting(-1)ini_set('display_errors', true)来查看和查找错误。这是必不可少的,否则你将很难找到错误。 确保在应用程序处于生产环境后将其关闭。

在您的具体情况下,if条件不起作用。永远不会使用变量$search。您在$query中引用了未定义的$database->num_rows($query)变量。 $email$email_hash未定义。

不要使用 $email = $_GET['email'];。您必须清理所有用户输入,否则您将获得sql注入!

而是使用特定于数据库的转义函数或预处理语句。 Mysql-&GT;

$email = mysql_real_escape_string($_GET['email']);

答案 3 :(得分:0)

你的where子句数组不好,应该是

$where_clause = array(
    'email' => $_GET['email'], 
    'email_hash' => $_GET['email_hash'],
    'active' => 0 // not 1
);

顺便说一句,您似乎正在使用一些抽象库进行数据库查询 - 尝试更改您的选择查询以使用占位符并让库为您转义变量。现在您的代码看起来容易受到SQL Incjections的攻击。<​​/ p>

相关问题