Jump to content

Cross-Page Session Access


reapfyre

Recommended Posts

Hi Guys,

 

I'm trying to do something a little tricky and trying to find the quickest code to do it.

I have 2 PHP pages con-currently running on the one Web Page...

 

Both these pages share the same sessionID as they are from the same Browser page.

What I basically want is to beable to change the value of a session variable in one page, and beable to pick up that change in the other page.

 

I have used MySQL / Flat files to pass information between these two pages and have even thought of using Sockets, but scaleability is an issue with that and MySQL is slower than a straight pass to a Session variable.

 

Basically one PHP Script is constantly running in a While loop and never ends, it checks the session of any updated variables which in my code, is changed from the other page.

 

I do have this working right now, but its a bit messy.

 

In order for the never-ending script to see any changes made to the Session, you basically have to Refresh the Session to receive the changes.

 

So basically my code now is:

 


while(true)
{
     session_start();

     if(isset($_SESSION['MYVAR']))
     {     
             //Do Something
             break;
     }
    
     session_destroy();
     sleep(1);     
}

 

This works fine but Im wondering whether there is a better way?    Session_Start will only allow you to call it once, unless its destroyed first, so I really need a session_refresh(); if it existed...anyone have any ideas?

 

 

Thanks heaps.

 

Mark

Link to comment
Share on other sites

I think you should make an ajax script that check the value of the variable on certain period of time (couple of seconds or something).

 

And one more thing, if you access the variable from both pages keep in mind that the variable may be overwritten, you got to think this really nice ;)

Link to comment
Share on other sites

Im already using AJAX and Reverse AJAX...

I didn't want to constantly call a PHP page every second as all the headers, etc get sent everytime...

 

Through a script that never ends I have a permenant connection to the client, so I can send the client any update whenever the server decides to...but the script doesn't pick up the session change without destroying and then reloading the session (thus retrieving the stored variables).

 

Unless I start using shared memory functions in the Unix kernal.... but memory management across hundreds of users may have scaleability issues aswell.

 

Any other ways anyone can think of?

Link to comment
Share on other sites

OK this is just a thought...

 

How about making a table for this temporary data u need to collect from the page. So make a row in the table for the log in user and update it whenever the user makes a change. And check this row information on your other site ... and at the end when the user logs off , delete this row.

 

Hope I gave you some idea :)

Link to comment
Share on other sites

Yeah I have used MySQL to do the transfer, which also works, but aren't as fast as a straight pass of a variable...even saving to a file will have I/O operations which are slower aswell, especially when it could be doing hundreds of operations a second..

 

What would be great is if PHP had developed the ability for seperate PHP threads to communicate to each other, without using sockets and databases and flat files.

 

At the moment there only seems to be Sessions, which do not update asynchronously through the threads.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.