Solved

$_SESSION lost?

Posted on 2012-03-19
3
266 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 82

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 82

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Several part series to implement Internet Explorer 11 Enterprise Mode
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now