【问题标题】:Success message is not showing and url is not redirected to activate.php?success未显示成功消息且 url 未重定向到 activate.php?success
【发布时间】:2013-01-15 17:21:07
【问题描述】:

我很难找出我的 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'; ?>

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

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

到: website.com/activate.php?成功

【问题讨论】:

  • empty($_GET['success']) == true 抱歉,为什么要添加额外的代码,而不能只检查 empty($_GET['success']) ?并且 isset 本身返回 true 或 false 无需使用 == true 进行检查
  • 您可能应该使用 POST,即使您重定向也是在使用 GET 共享个人信息。
  • 已发送 HTML 后无法发送标头。你必须先做标题。此外,您应该编写一个 5 秒的重定向,而不是立即重定向,以便用户可以阅读您的消息,告诉他们他们已激活。 &lt;head&gt;&lt;meta http-equiv="refresh" content="5; url=index.php"&gt;&lt;/head&gt;
  • 如果我理解正确并且上面的代码在empty($_GET['success'] == true) 中包含在activate.php 中,那么如果它有效,你会得到一个无限的标头重定向错误。你的逻辑需要重新审视。

标签: php


【解决方案1】:

这句话有点可疑:

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

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

【讨论】:

  • 或者更好:(isset($_GET['success']) &amp;&amp; !empty($_GET['success'])
  • 是的,我使用相同的方法,我只是添加了 FALSE 以使更改引人注目
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    您将其设置为 if 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();
    }
    

    【讨论】:

      【解决方案4】:

      您应该测试您的哪个标准不合格,见下文。附带说明:如果您没有传递状态,则不需要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 中的上述代码,那么如果它有效,您将收到无限标头重定向错误。你的逻辑需要重新审视。

      【讨论】:

      • 输出 1、0 或什么都没有是什么意思?
      • 我的意思是boolean 值。 1 是true,0 是false 或者什么都没有,因为它没有到达那里。我的代码与返回 boolean 值的 isset 相呼应
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 2016-11-03
      • 1970-01-01
      • 2016-10-09
      相关资源
      最近更新 更多