简体   繁体   English

如何使用 PHP 从多个页面访问 session 变量

[英]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.

相关问题 如何在不断更改其值的同时访问多个PHP页面上的变量或变量值或变量 - How to access variable or variable value or variable on multiple PHP pages while continuously changing its value 如何在外部javascript中从php访问会话变量 - How do I access session variable from php in external javascript 如何从 PHP 访问 ASP 经典会话变量? - How to access ASP classic session variable from PHP? PHP检查会话,检查多个变量以允许访问特定页面 - PHP check session, checking multiple variables to allow access to specific pages 如何使用$ _SESSION变量在php页面之间传递随机变量? - How to pass an random variable between php pages using $_SESSION variable? 通过不同的操作和另一个PHP页面访问zend框架会话 - access zend framework session from different actions and another PHP pages 如何在会话变量中保存从数据库中提取的数组以将其发送到 php 中的其他网页 - how to save in a session variable an array extracted from a database to send it to other web pages in php 如何在另一个页面中访问PHP会话变量? - How to access php session variable in another page? PHP在多个页面中使用SESSION - PHP use SESSION in the multiple pages 多页中的PHP Session变量 - PHP Session variables in multiple pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM