未显示成功消息,并且未重定向 URL 以激活.php成功


Success message is not showing and url is not redirected to activate.php?success

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

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

激活链接如下所示:

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

这是我激活的代码.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'; ?>

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

网址不会从以下位置更改:http://website.com/activate.php?email=useremail@website.com&email_code=2631df446b129480abdbf54f08e8c494

自:website.com/activate.php?success

这句话有些可疑:

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

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

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

您将其设置为如果activate == 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();
}

您应该测试哪个标准失败,请参见下文。作为旁注:如果你不传递状态,你不需要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测试上面的代码时,它对我来说重定向得很好。

编辑:

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