0

我很难找出我的 PHP 用户/注册站点的错误。注册后会发送一封带有激活链接的电子邮件,单击该链接时会成功激活帐户,但未显示成功消息。

如果我在浏览器中复制/粘贴激活链接并更改链接中的电子邮件或电子邮件代码中的字母,它将成功地给我所有错误消息。

激活链接如下所示:

http://website.com/activate.php?email=useremail@website.com&email_code=2631df446b129480abdbf54f08e8c494

这是我的 activate.php 的代码

<?php 
include 'core/init.php';
logged_in_redirect();
include 'includes/overall/header.php';
?>

<?php 
if (isset($_GET['success']) == true && empty($_GET['success']) == true) {
    ?>
<h3>Thanks, your account has been activated..</h3>
<p>You're free to log in!</p>
<?php
    header('Location: activate.php?success');
    exit();
} 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[] = 'Ooops something went wrong, and we couldn\'t find that email adress!';
    } else if (activate($email, $email_code) === false) {
    $errors[] = 'We had problems activating your account';
    } if (empty($errors) === false) {
?>
    <h2>Ooops...</h2>
<?php
    echo output_errors($errors);
} else {
    header('Location: activate.php?success');
    exit();
}
} else {
header('Location: index.php');
exit();
}
?>

<?php include 'includes/overall/footer.php'; ?>

为什么它不给我成功消息并重定向?

URL 不变:http ://website.com/activate.php?email=useremail@website.com&email_code=2631df446b129480abdbf54f08e8c494

至:website.com/activate.php?success

4

4 回答 4

0

永远,永远不要在命令中使用HTMLcode <?php PHPcode语法。 基本上:您首先输出一个字符串(您的 HTML 代码),然后尝试输出标题。这是不正确的。标头需要在其他任何事情之前发送。您想要做的是将整个文档转换为 PHP(文档的第一个字符应该是),然后使用heredoc语法实际插入 HTML 代码块,并回显它们(所有内容都收集到一个变量中并在最后一行回显你的代码)。避免像您在这里所做的那样不惜一切代价将 PHP 与 HTML 混合。header

<?php

于 2013-01-31T15:04:36.590 回答
0

您将其设置为 ifactivate == true并将空重定向到该确切页面,因此您将获得重定向循环。

if (isset($_GET['success']) == true && empty($_GET['success']) == true) {
        header('Location: activate.php?success');
        exit();
}

尝试

if (isset($_GET['success']) == true && empty($_GET['success']) == true) {
       echo "It worked";
       exit();
}
于 2013-01-31T15:05:34.387 回答
0

这条线有点可疑:

if (isset($_GET['success']) == true && empty($_GET['success']) == true) {

你确定不是&& empty($_GET['success']) == false)吗?

于 2013-01-31T14:58:25.587 回答
0

您应该测试您的哪个标准不合格,见下文。作为旁注:exit如果您没有传递状态,则不需要括号,也不需要打开和关闭 php,这不是必需的:

<?php 
include 'core/init.php';
logged_in_redirect();
include 'includes/overall/header.php';

if (isset($_GET['success']) && !empty($_GET['success'])) {
    //in activate.php isset($_GET['success']) add this ouput, 
    //it works / not recommended and never seen.
    //echo "<h3>Thanks, your account has been activated..</h3>";
    //echo "<p>You're free to log in!</p>";
    header('Location: activate.php?success');
    exit;
} else if (isset($_GET['email'], $_GET['email_code'])) {
    $email = trim($_GET['email']);
    $email_code = trim($_GET['email_code']);
    if (email_exists($email) === false) {
        $errors[] = 'Ooops something went wrong, and we couldn\'t find that email adress!';
    } else if (activate($email, $email_code) === false) {
        $errors[] = 'We had problems activating your account';
    } if (empty($errors) === false) {
        echo "<h2>Ooops...</h2>";
        echo output_errors($errors);
    } else {
        header('Location: activate.php?success');
        exit;
    }
} else {
    // The following 2 lines: You should see 1, 0 or nothing
    // From there you should be able to troubleshoot.        
    echo "isset: ".isset($_GET['success'])."<br>";
    echo "Empty: ".empty($_GET['success']);
    //header('Location: index.php');
    exit;
}
include 'includes/overall/footer.php';

当我用它测试上面的代码时,?success它重定向对我来说很好。

编辑:

empty($_GET['success'] == true)如果我理解正确并且您在其中使用了上述代码,activate.php那么如果它有效,您将收到一个无限的标头重定向错误。你的逻辑需要重新审视。

于 2013-01-31T15:10:44.610 回答