Jump to content

Resuming Sessions


AndrewM16921

Recommended Posts

I have this project for school. There are three of us. I'm working on the "middle" (php/xml), while I have a partner working on the front end (ajax), and another partner on the back end (php/xml/mysql). I need to maintain a session for the front end accessing me, however it doesn't seem to work the same as when a user accesses the php file directly from the browser. Instead, he's doing something to make a request to my page with post variables, and I reply with xml. Similarly, I send a curl request to the back end and he replies in xml as well. So, I thought perhaps the browser somehow manages the session and sends a session id on every request. But since my page isn't accessed through a browser, but rather from the front end's page directly, perhaps this does not work. So, what I did was have him request a session id from me, and I'd start a session that way and reply with the id. Then, he'd send that sid for every request and I'd resume that specific session. Unfortunately, this does not seem to work. Here's the few lines of code that are relevant to this.

 

...
//start session if sid provided
if(isset($_POST['sid']))
{
   session_id($_POST['sid']);
   session_start();
}

if(isset($_POST['r']))
{
   $req = $_POST['r'];

   if($req == 'session')
   {
       //destroy old session if set
       if(isset($_POST['sid']))
       {
           session_destroy();
       }

       //Starts a new session and replies with a session id
       session_start();
       $_SESSION['schedule'] = new Schedule();
       $reply = session_id();
   }
...

Link to comment
https://forums.phpfreaks.com/topic/269693-resuming-sessions/
Share on other sites

  On 10/19/2012 at 9:58 PM, shaddowman said:

I think you need these to change palces. You need to start the session first.

 

session_id($_POST['sid']);
session_start();

 

That's actually the first thing that I tried. It didn't make any difference that I could tell, and didn't solve the problem. :/

 

  On 10/19/2012 at 9:49 PM, Christian F. said:

cURL has an option to use a cookie jar, which your friend needs to use. More information in the PHP manual.

 

I could have him change his requests around. But seeing as he used zero php, I was hoping to avoid having him change a large portion of his code (unless absolutely necessary). Isn't there a way to simply resume a session based on the session id, which could have been sent back and forth as I am doing?

Link to comment
https://forums.phpfreaks.com/topic/269693-resuming-sessions/#findComment-1386494
Share on other sites

I'll look into this cookie jar then, thanks. Hopefully that will solve my problem. Also, I said front end is using ajax. I probably should have specified only ajax.

 

  On 10/19/2012 at 6:13 PM, AndrewM16921 said:

I have this project for school. There are three of us. I'm working on the "middle" (php/xml), while I have a partner working on the front end (ajax), and another partner on the back end (php/xml/mysql).

Link to comment
https://forums.phpfreaks.com/topic/269693-resuming-sessions/#findComment-1386598
Share on other sites

As long as the class definition exists before the session_start() statement, you can create objects ($_SESSION['some_object'] = new some_class() ;) in session variables or you can assign an object to a session variable ($some_object = new some_class(); $_SESSION['some_object'] = $some_object.)

 

If your class makes or holds a resource (database connection, file handle,...) that resource will not persist between page requests since all resources used on any page are destroyed when the execution of that page ends.

 

All session data is automatically serialized and unserialized by php when the data is stored to the session data file and retrieved from the session data file. There's no need to specifically serialize any data put into a $_SESSION variable.

Link to comment
https://forums.phpfreaks.com/topic/269693-resuming-sessions/#findComment-1386614
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.