Solved

PHP session variables in iframe

Posted on 2008-10-06
5
2,104 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

624 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