[英]How to access the session variable from multiple pages with PHP
I want to access $_SESSION['roleid']
in master.php .我想在master.php中访问
$_SESSION['roleid']
。 master.php is included in every page. master.php包含在每一页中。 I'm only able to access
$_SESSION['roleid']
in dashboard.php after user login.用户登录后,我只能在dashboard.php中访问
$_SESSION['roleid']
。 How to access $_SESSION['roleid']
in every page.如何在每个页面中访问
$_SESSION['roleid']
。
<?php
session_start();
if($_SESSION['login']==1) {
$_SESSION['loggedIn'] = true;
$role_id1 = $_GET['role_id'];
// store here in session
$name=$_GET['name'];
$_SESSION['roleid'] = $role_id1;
// $role_id=$_SESSION['roleid'];
$a=$_SESSION['roleid'];
// echo $a;die;
if(isset($_SESSION["roleid"])){
header("location:api/dashboard.php?role_id=$a?name=$name");
}
} else {
header("location:index.php");
echo "login unsuccessful.";
}
?>
session_start()
function, which page we want acess session variables we have to first call session start function on that page then we can access session function, which page we want acess session variables we have to first call session start function on that page then we can access session
Page 1:第 1 页:
session_start();
Set session variables:设置 session 变量:
$_SESSION["color"] ="green";
echo "Session variables are set.";
Page2:第2页:
session_start();
echo “my favcolor is“.$_SESSION["color"]; /*here it will display my fav color is green*/
To be able to access the session variables you need to call session_start();
为了能够访问 session 变量,您需要调用
session_start();
on top of every page that will use the session variable.在将使用 session 变量的每个页面的顶部。 After the start call has been made you can use session variables like this
echo $_SESSION["my_var"];
开始调用后,您可以使用 session 变量,例如
echo $_SESSION["my_var"];
and this to set the content $_SESSION["my_var"] = "Var content";
并设置内容
$_SESSION["my_var"] = "Var content";
, if you are unsure what the session actually belongs it is possible to check the content of the session by doing var_dump($_SESSION);
,如果您不确定 session 实际属于什么,可以通过执行
var_dump($_SESSION);
. . This will show all the data the session contains since it is passed as an array.
这将显示 session 包含的所有数据,因为它作为数组传递。
Please do remember that a session is not recursive through subdomains because of the cookie that is being used to track which session belongs to who.请记住,session 不会通过子域递归,因为用于跟踪 session 属于谁的 cookie。 A session is also dependent on that headers are not sent yet since it needs to interact with the cookies.
session 还取决于尚未发送标头,因为它需要与 cookies 交互。
To delay sending of headers do this: 1. Call ob_start();
要延迟发送标头,请执行以下操作: 1. 调用
ob_start();
at the completely top of the scripts that needs to set multiple headers 2. Do the things you need to do like set headers and so on 3. Call ob_end_flush();
在需要设置多个标题的脚本的完全顶部 2. 做你需要做的事情,比如设置标题等等 3. 调用
ob_end_flush();
to send the headers.发送标题。
Here is the offical PHP docs on this: https://www.php.net/manual/en/function.ob-start.php https://www.php.net/manual/en/function.ob-end-flush.php Here is the offical PHP docs on this: https://www.php.net/manual/en/function.ob-start.php https://www.php.net/manual/en/function.ob-end-冲洗。php
you should check $_SESSION['roleid']
: * if having $_SESSION['roleid']
, you will get it.你应该检查
$_SESSION['roleid']
: * 如果有$_SESSION['roleid']
,你会得到它。 On that code, you store $_GET['role_id']
to $_SESSION['roleid']
but $_GET['role_id']
have no in all page, it's only in dashboard.在该代码上,您将
$_GET['role_id']
存储到$_SESSION['roleid']
但$_GET['role_id']
在所有页面中都没有,它仅在仪表板中。 I think that.我觉得。 You should try.
你应该试试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.