Php 通过$\u会话将变量传递到下一页
我需要将一个变量从login.php传递到index.php,以便在用户未输入用户名或密码时获得错误消息。我有一个模式表单,它将Php 通过$\u会话将变量传递到下一页,php,variables,session,Php,Variables,Session,我需要将一个变量从login.php传递到index.php,以便在用户未输入用户名或密码时获得错误消息。我有一个模式表单,它将$\u POST['usename']和$\u POST['password']传递到login.php login.php require_once(realpath(__DIR__ . "/resources/session.php")); if(!$logged) { if(isset($_POST['username']) && iss
$\u POST['usename']
和$\u 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 ...
}
$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");
$\u SESSION['noty']
是我应该传递给index.php的变量
index.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 ...
}
$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");
在index.php中包含的header.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 noty($text, $layout, $type) {
echo "<script type=\"text/javascript\">
noty({text: '$text', layout: '$layout', type: '$type'});
</script>";
}
函数noty($text,$layout,$type){
回声“
noty({text:'$text',布局:'$layout',类型:'$type'});
";
}
这不管用$\u会话['noty']
在我从login.php进入index.php时设置为未设置
编辑1:
session.php顶部包含session\u start()
,在index.php和login.php上都是必需的
编辑2:
退出代码>在标题()之后(
重定向对我不起作用
已解决:
我将$\u SESSION
设置为array()
,只要用户没有登录SESSION.php,那么它也会删除$\u SESSION['noty']。那个错误太愚蠢了。在使用header()函数之后,您应该使用退出代码>函数。更多详细信息:使用header()
函数后,应使用退出代码>函数。更多详细信息:您是否在两页的顶部调用了session\u start()
?session\u start
,是否使用它?使用session\u start()
访问$\u session
变量。我使用。session.php是这两个文件所必需的,它在顶部包含session\u start()。您是否在两个页面的顶部调用了session\u start()
,是否使用它?使用session\u start()
访问$\u session
变量。我使用。这两个文件都需要session.php,它的顶部包含session_start()。谢谢你的回答,但对我来说不起作用$\u会话['noty']
在index.php上仍处于未设置状态。感谢您的回答,但它对我不起作用$\u会话['noty']
在index.php上仍处于未设置状态。