Results 1 to 5 of 5

Thread: Propagate session state in IFrame

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2004
    Posts
    50

    Propagate session state in IFrame

    Hi,

    I am using a page with an iframe within it.
    When I am loading the first time the page, I change the session name. The problem is that I do not know how to pass the new session name also to the IFrame.

    The beginning of the code for the page is:

    Code:
    	global $session_name;
    	
    	if (isset($_REQUEST['session_name'])) {
    		// use session name passed via $_GET or $_POST
    		$session_name = $_REQUEST['session_name'];
    	} // if		
    	
    	// get details from any previous session
    	if (isset($session_name)) {
    		// use existing session name
    	} else {
    		// assign new session name
    		$session_name = getNewSession('session');
    	} // if
    	
    	session_name($session_name);
    	session_start();
    	
    	
    
    	echo ">" . $session_name;
    	
    	function getNewSession ($prefix = 'session')
    	// create a new session name using $prefix + a number.
    	{
        // step through numbers 0-99
        for ($i = 0; $i <= 1000; $i++) {
            $session_name = $prefix .$i;
            if (!array_key_exists($session_name, $_COOKIE)) {
                break;
            } // if
        } // if
        
        return $session_name;
    
    	} // getNewSession
    The beginning of the code for the IFrame is:

    Code:
    	session_start();	
    	
    	echo ">>" . session_name();
    For the page, the output is : >session0
    For the IFrame the output is : >>PHPSESSID

    The conclusion is that, in this case, the IFrame doesn’t know about the session name of the parent page.
    Could you suggest me an idea on how to make them work together, by using the same session name?

    Thanks

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Propagate session state in IFrame

    It is quite probable that the iFrame page is being requested at the same time as the parent page. The session state is not saved until the script has finished, hence you are not seen the update. Its a classic race condition.

    I suggest that you load the page with the iframe location set to "about:blank" and change the location when the page has loaded fully. This will however require Javascript.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2004
    Posts
    50

    Re: Propagate session state in IFrame

    I have followed your advice, by loading a blanc page in the iFrame, and then changing the location of the iFrame in the onLoad event of the body tag, after the page has fully loaded.

    Unfortunately the result remain the same, with a different session_name() on the two pages.

    Anyway, thanks for the advice. By reading it, I also thought that could be a solution.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Propagate session state in IFrame

    You also need to set session_name() before you call session_start() in the iFrame too. Otherwise it uses the default. I suggest that you check the session name contains no invalid characters too and limit the number of sessions that can exist per user as it would be very easy to flood the server with files if you do not.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2004
    Posts
    50

    Re: Propagate session state in IFrame

    A solution I found is to pass the session name in the address of the iframe, and it works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width