I have a very basic idea of PHP session management. From what I know, in order to maintain variables across multiple scripts in an app. we can store them in a session.
I have a main page - index.php in which I have an iframe referencing another php script - bar.php. All I am trying to do is access the session variables from the index page in the iframe, which, so far I haven't been able to accomplish.
My code is as follows:
index.php
Expand|Select|Wrap|Line Numbers
- <?php
- session_start();
- $_SESSION['myVar']="hello" ;
- $sessId=session_id();
- echo "<br> sessId: " . $sessId . "<br>" ;
- print('<iframe name="test" id="test" src="foo/bar.php?sessId='.$sessId.'" marginwidth=0 marginheight=0 onload="this.height=this.contentDocument.height" width="100%" frameborder="0" scrolling="yes"></iframe>');
- ?>
bar.php
Expand|Select|Wrap|Line Numbers
- <?php
- $sessId = $_REQUEST['sessId'] ;
- if(session_id() == "")
- {
- session_id($sessId);
- session_start();
- echo "<br> sessId: " . $sessId . "<br>" ;//shows the same session-id as index.php
- echo "<pre>" ;
- print_r($_SESSION); //shows an empty array
- }
- else
- {
- //something
- }
- ?>
Where might I be going wrong? Any suggestions?