PHP session problem while using iframe.

Hi guys, recently i am so busy and not able to update my website often.

But today, I face a problem on the PHP session while using iframe to include a custom php file.
The custom php file just doing some select/update command using session data.
I try my script in Google Chrome & Mozilla Firefox, everything is working fine but not Internet Explorer.
The data inside the session is not retrievable and causing the entitle script not working.

Below is my sample script

<?php
  session_start();
  //echo session_id(); //PRINT OUT THE SESSION ID
  if(!isset($_SESSION['mydata']) && $_POST['action'] == 'update')
    die("unable to get mydata");
 
  $_SESSION['mydata'] = 'ANYTHING';
 //PERFORM SELECT/UPDATE
?>

Above just a simple sample, if you using wordpress/joomla (php applications) and iframe the custom page and using Internet Explorer to retrieve and update it. The page should display “unable to get mydata” but not perform the update statement.
After debug for sometime, I try to print out the session_id() and found that everything the iframe page refresh, it will generate another new session_id but not use the previous id (This act differently with Firefox and Chrome) and this is the main issue causing my script is not working.

Then I google a bit and find the solution below.

What you need to do it just add in a single line command into your custom php page.

<?php
  header('P3P: CP="CAO PSA OUR"'); //ADD IN THIS LINE IN ORDER TO SOLVE THE INTERNET EXPLORER ALWAYS GET NEW SESSION ISSUE
  session_start();
  //echo session_id(); //PRINT OUT THE SESSION ID
  if(!isset($_SESSION['mydata']) && $_POST['action'] == 'update')
    die("unable to get mydata");
 
  $_SESSION['mydata'] = 'ANYTHING';
 //PERFORM SELECT/UPDATE
?>

21 Comments to “PHP session problem while using iframe.”

  1. Trond 30 September 2009 at 8:55 pm #

    You should use sessions and database instead.

  2. Eduardo HRB 9 October 2009 at 3:03 am #

    This header saved my life, rarely had this problem, but in the last two times I could not solve it.

    Now, finally resolved.

    Excellent post.

    • h2Guru 9 October 2009 at 9:55 am #

      Thanks…
      The header save my life as well 😀
      Cheer~~!

  3. Manfredi 24 January 2010 at 10:37 pm #

    Спасибо, проблема решилась =)

  4. Tomek 4 March 2010 at 5:39 pm #

    Good solution, thanks 🙂

    Unfortunately it doesn’t work in FF (3.5.8) under Windows 2003 Server, but under Win XP or Vista it does.

  5. h2Guru 22 March 2010 at 7:38 pm #

    Hi Tomek,

    I found no problem by using FF 3.0.X….
    You face the problem on FF 3.5.8?

  6. nishit 7 May 2010 at 3:38 pm #

    Thanks a lot………..

    This is a really useful solution.saved me a lot of work …

  7. Pete 31 August 2010 at 10:47 pm #

    I was trying to solve the same problem for a while.
    Thanks a lot.

  8. Chitra 6 October 2010 at 4:07 am #

    Great Boss 🙂

    Your code helped me on my Project Release 🙂

  9. Chitra 6 October 2010 at 4:08 am #

    Thanks a lot !!!!!!!!!!

  10. Paul 7 December 2010 at 3:28 am #

    I am having a problem loading an iframe on this page in IE. It works fine on all other browsers. Any tips?

    http://untaxableinc.com/our-services/manage-your-tax-documents/

  11. h2Guru 8 December 2010 at 12:23 pm #

    Hi Paul,

    Checked your site, the iframe seem working fine but found a js problem.
    Are you try to do some cross domain trigger??

    I don’t think it will allow you to do it since it is a security issue.

  12. Karthik 22 February 2011 at 1:48 am #

    You saved my life. Thanks a lot.. I tried 100 of different things and finally this single line of code fixed it.

    Thanks! 🙂

  13. Konrad 23 May 2011 at 8:11 pm #

    hi,
    You saved my live too 🙂

  14. Latonya Weckwerth 24 August 2011 at 1:19 pm #

    I do consider all the ideas you’ve presented to your post. They are very convincing and can certainly work. Nonetheless, the posts are too brief for newbies. May just you please lengthen them a bit from next time? Thank you for the post.

  15. Chris 14 November 2011 at 7:30 pm #

    Thanks for this – saved us a lot of work!

  16. Martin 1 May 2012 at 1:15 pm #

    3 years later…still a great hint. 😉

    Thanks a lot!

  17. Hi 20 February 2013 at 10:05 pm #

    not working fror me

  18. RoryGren 7 May 2013 at 5:56 pm #

    Thank you so much!
    Still saving people after so many years!
    Saved my bacon today!

  19. […] I believe this article’ll be useful: http://www.how2guru.com/archives/php-session-problem-while-using-iframe/ […]

  20. Somebody 29 May 2014 at 5:37 pm #

    great !!


Leave a Reply