Reputation: 127
I want to access $_SESSION['roleid']
in master.php. master.php is included in every page. I'm only able to access $_SESSION['roleid']
in dashboard.php after user login. How to access $_SESSION['roleid']
in every page.
<?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.";
}
?>
Upvotes: 2
Views: 1099
Reputation: 448
To be able to access the session variables you need to call session_start();
on top of every page that will use the session variable. After the start call has been made you can use session variables like this echo $_SESSION["my_var"];
and this to set the 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);
. This will show all the data the session contains since it is passed as an array.
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. A session is also dependent on that headers are not sent yet since it needs to interact with the cookies.
To delay sending of headers do this:
1. Call 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();
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
Upvotes: 1
Reputation: 54
you should check $_SESSION['roleid']
:
* if having $_SESSION['roleid']
, you will get it. 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.
I think that. You should try.
Upvotes: 0