Solved

PHP session variables in iframe

Posted on 2008-10-06
5
2,047 Views
Last Modified: 2013-12-13
Hi

I have a script that run smoothly without the iframe, but when i use the iframe in IE and safari it doesn't work properly.

I am searching some information from a database and display them, when I log in it create a instance of a class with all the login information encapsulate it. I save that instance variable in a session ($_SESSION['login']=&$Login) and pass it to other pages. This works great with me. But when I jump one page to the other page within iframe at the first time my php script say the session variable $_SESSION['login'] is not set, but I can go back using the browser back button and come back again, then  the php script say the session variable is set and works fine.

I know this is little bit confusing but any help is appreciate.

Thank you
~P
0
Comment
Question by:ppeiris
  • 3
5 Comments
 
LVL 6

Expert Comment

by:deresh
ID: 22653534
it seems that this is a problem with cookies that isn't set for iframe becaouse of privacy policies. try using different storage for sessions ( database, for example)
0
 
LVL 1

Author Comment

by:ppeiris
ID: 22654647
Thank you for your reply, But how can I save a class instance in to a database ? I using mysql database in the back-end but still don't understand how to store a instance in to a database. My understanding is that when I create the instance from a class template it will allocate chunk of memory for that instance and maintain all the variables in that so if I try to save that in to a database do i have to save the memory location of the instance perhaps!

Thank you
~P
0
 
LVL 2

Expert Comment

by:AdiF
ID: 22656355
Yes, it definitely sounds as though the PHP session cookies aren't being forwarded in the iframe.  Are you linking to URL's of the same domain?  (i.e. mydomain.com) and not different domains?

Anyhow; a workaround would be to append the PHP session in your link URL's. You can use the session_id() function to add an HTTP GET like myscript.php?PHPSESSID=<id here> and manually load it when you call session_start() with each script.  This method is not so secure.
0
 
LVL 1

Author Comment

by:ppeiris
ID: 22656661
Hi

Yes your correct I am using this iframe in different domain. in other words i have to display my inventory in other websites. my site is working fine even in the same script (with iframe in different domain) in firefox and safari. I have tried passing session id via weblink but still i have the same problem. I would like to try to save the session variable (in my case its a class intense) in my mysql database, but can you give me some hint how to start!

Thank you
~P
0
 
LVL 1

Accepted Solution

by:
ppeiris earned 0 total points
ID: 22663723
Hi

PROBLEM SOLVED !

I create a temporary file in /tmp dir to store my class object and reload those information when it jump to the next page.

steps:
1) create a tmp file session id as a name of the file [  $fp = fopen("/tmp/".$session_id().".txt", "w")  ]
2) serialize the class object [ $a=serialize($classobj)  ]
3) write to the file [  fwrite($fp, $a) ]
4) close the file [ fclose($fp) ]

** go to the next page **

5) get the information from the file [  $s=implode("", @file("/tmp/".$_GET['id'].".txt"))  ]
6) unserialize the class object [ $classobj=unserialize($s); ]

** now we all done... you can call a class function in the object [ $classobj->YourFunction() ]

**note: you have to delete the tmp file that you created within your script after you done with what you want.


please refer to these for more information
http://us.php.net/serialize
http://us3.php.net/unserialize


0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

18 Experts available now in Live!

Get 1:1 Help Now