Solved

$_SESSION lost?

Posted on 2012-03-19
3
270 Views
Last Modified: 2012-03-25
Is the session value being lost in the following:

parent:
<?php
if(!empty($_SESSION['name'])) echo ($_SESSION['name']);
?>

<html>
<form>
<iframe src="child.php"/>
</iframe>

</form>

</html>

Open in new window


child:
<?php
 if(!empty($_FILES)) {$_SESSION['name'] =$_FILES['file']['name']  ;
   }
?>
<html>
<body>

<form action="child.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

Open in new window



Can the $_SESSION value be retrieved by the parent?
0
Comment
Question by:pillmill
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 37740926
Without session_start() at the top of each and every page, you effectively do not have session variables because session_start() sets up and refetches the session id which is used to identify the variables for your session.  Without session_start(), you do not have a proper session id.

If both files are on the same server and sessions are setup properly, then parent and child are in the same session because they are in the same browser at the same time and can see the same session variables.

http://us.php.net/manual/en/function.session-start.php
0
 

Author Comment

by:pillmill
ID: 37741073
Thanks. I added session_start() to the parent:
<?php
session_start(); 
if(!empty($_SESSION['name'])) ($_SESSION['name']);
?>
<html>
<form>
<iframe src="child.php" />
</iframe>
</form>
</html>

Open in new window


Session variables still seem to be unavailable.
How can I access $_SESSION set by the child?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37743672
You have to add it to ALL pages, not just the parent.  It has to be identified on each page that is loaded.  That includes the child page.  

The PHP page on the server only 'sees' the session during the time it is processing the PHP code.  When it gets to the browser, the server is already done with the session for that page.  It's not a 'sticky' kind of thing.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question