Jump to content

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

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. :/

 

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.

 

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.

Edited by PFMaBiSmAd
Link to comment
https://forums.phpfreaks.com/topic/269693-resuming-sessions/#findComment-1386614
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.