我需要将变量从 login.php 传递到 index.php ,以便在用户输入用户名或密码时收到错误消息。我有一个模式表单,将$_POST['usename']
和$_POST['password']
传递给 login.php 。
的login.php
require_once(realpath(__DIR__ . "/resources/session.php"));
if(!$logged) {
if(isset($_POST['username']) && isset($_POST['password'])) {
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
if(strlen($_POST['username']) == 0 || strlen($_POST['password']) == 0) {
$_SESSION['noty'] = "error||topCenter||(here the message)";
header("Location: index.php");
} else {
// Code...
}
}
} else {
// Code ...
}
$_SESSION['noty']
是我应传递给 index.php 的变量。
的index.php
$page_id = 1;
require_once(realpath(__DIR__ . "/resources/session.php"));
require_once(TEMPLATE_PATH . "/header.php");
require_once(TEMPLATE_PATH . "/sidebar.php");
require_once(TEMPLATE_PATH . "/breadcrumb.php");
renderLayout(VIEWS_PATH . "/index.view.php");
require_once(TEMPLATE_PATH . "/footer.php");
位于 header.php 的底部, index.php :
if(isset($_SESSION['noty']) AND strlen($_SESSION['noty']) > 0) {
$noty = explode("||", $_SESSION['noty']);
noty($noty[2], $noty[1], $noty[0]);
$_SESSION['noty'] = "";
} else {
$_SESSION['noty'] = "";
}
noty function
也是:
function noty($text, $layout, $type) {
echo "<script type=\"text/javascript\">
noty({text: '$text', layout: '$layout', type: '$type'});
</script>";
}
这不起作用。当我从 login.php 到 index.php 时设置 <{1}}。
编辑1:
session.php 在顶部包含$_SESSION['noty']
, index.php 和 login.php 都需要。< / p>
编辑2:
session_start()
重定向后, exit;
对我无效。
解决:
每当用户没有登录 session.php 时,我就会将header()
设置为$_SESSION
,因此它也会删除$ _SESSION [&#39; noty&#39]。那个错误是如此愚蠢。