Accessing SESSION variables from another php file
you need
session_start()
at the top of each of your php scripts otherwise the session variable is meaningless across php files.
Also you are getting the Cannot Modify header error because you have
Here's the code:
being sent to the browser before the headers, move all header functions before any content
user123_456
Updated on June 04, 2022Comments
-
user123_456 almost 2 years
I want to access the session variable which I declared in the another php file.
How can I do that?
Here is what I did.
test.php
$_SESSION['SESS_VERSION'] = $member['Version']; session_write_close(); header('location: '.$_SESSION['SESS_VERSION']);
This session variable is working and I am correctly redirected to another page.
On that page for example:
test2.php
I am calling a php script from javascript to return me a JSON formated data.
What I was trying to do in that
test3.php
script is to access a session variables fromtest.php
Here's the code:
<?php header("Content-type: application/json; charset=UTF-8"); echo '{ "results" : [ '; $result = dbMySql::Exec("SELECT m.data1 v.data2, k.data3 FROM {$_SESSION['SESS_MAIN_BASE']} m, {$_SESSION['SESS_SECOND_BASE']} v, {$_SESSION['SESS_THIRD_BASE']} k"); $result_array = array(); ?>
Why I can't access any of the session variables on this php page? Maybe my syntax is not correct. But this is the error that I am getting:
Warning: Cannot modify header information - headers already sent
And of course error that variables are empty.
-
Funk Forty Niner almost 12 yearscan't
echo
andheader
at the same time, far as I know. -
PenguinCoder almost 12 years
-
-
PenguinCoder almost 12 yearsPretty sure the 'Here's the code' in the code block, is just improper SO formatting, and not the actual file contents.
-
Patrick Evans almost 12 yearsTrue, in that case he probably has white spaces before the php tags, only way he would be getting the header error with that code. But if he doesn't know to use session_start before using $_SESSION you never know lol