Solved

PHP session variables in iframe

Posted on 2008-10-06
5
2,054 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

895 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