【问题标题】:Troubles moving errors from one page to another PHP将错误从一个页面移动到另一个 PHP 的麻烦
【发布时间】:2013-06-22 04:49:44
【问题描述】:

我正在关注 PhpAcademy 的关于制作登录系统的教程,我对这一切都很陌生。我没有像他那样制作小部件,而是为登录表单制作了一个完整的页面,称为signin.php,以及一个称为login.php 的验证页面。表单操作是 login.php,这是文件,我有一个数组 errors[] 存储在所有页面的全局 php 文件中。每次出现错误时,我都会向其附加一个新错误,这一切都很好,但是现在我希望我的错误出现在登录页面的表单下,我该怎么做?现在,错误只出现在 login.php 中。我尝试使用标头发送错误,但这似乎不起作用,我尝试通过简单地输出错误数组在那里打印错误(因为它是全局的),但这似乎也没有打印任何东西.我能做什么?

【问题讨论】:

  • 使用 $_SESSION 存储您的错误并从其他页面访问
  • $_SESSION 是所述的最佳方式,但您也可以使用 $_GET 发送错误状态。

标签: php login system transfer


【解决方案1】:

全局并不意味着它对所有页面都是全局的。全局仅对当前页面是全局的。如果您希望数据在页面/重定向之间保留,您需要在 url 中传递数据(即signin.php?error=Invalid username and password。我不喜欢这种方法,因为您需要清理错误消息并且它使 url 丑陋。)或您必须存储它,以便您可以在下一页检索它。最简单的选择是使用 $_SESSION。这是一个例子:

login.php

<?php
    session_start();

    // ...

    // You'll have to change my custom functions like form_has_errors to whatever
    // you're using in your code
    if (form_has_errors()) {
        // Store the error in the session
        $_SESSION['errors'] = form_get_errors();

        // redirect
        header('Location signup.php');
    }

    // ...

signup.php

<?php 
    session_start();

    // ...

    if (isset($_SESSION['errors'])) {
        // change this to something that displays the errors better
        print_r($_SESSION['errors']);

        // remove the error from the session so we don't display it twice
        unset($_SESSION['errors']); 
    }

【讨论】:

  • 正是我需要的!谢谢!
猜你喜欢
  • 2018-10-29
  • 2019-12-03
  • 1970-01-01
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多