2

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 from test.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.

PenguinCoder
  • 4,335
  • 1
  • 26
  • 37
user123_456
  • 5,635
  • 26
  • 84
  • 140

1 Answers1

6

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

Patrick Evans
  • 41,991
  • 6
  • 74
  • 87
  • 1
    Pretty sure the *'Here's the code'* in the code block, is just improper SO formatting, and not the actual file contents. – PenguinCoder Jun 27 '12 at 18:44
  • True, 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 – Patrick Evans Jun 27 '12 at 18:49