AndrewM16921 Posted October 19, 2012 Share Posted October 19, 2012 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(); } ... Quote Link to comment https://forums.phpfreaks.com/topic/269693-resuming-sessions/ Share on other sites More sharing options...
Christian F. Posted October 19, 2012 Share Posted October 19, 2012 cURL has an option to use a cookie jar, which your friend needs to use. More information in the PHP manual. Quote Link to comment https://forums.phpfreaks.com/topic/269693-resuming-sessions/#findComment-1386484 Share on other sites More sharing options...
shaddowman Posted October 19, 2012 Share Posted October 19, 2012 I think you need these to change palces. You need to start the session first. session_id($_POST['sid']); session_start(); Quote Link to comment https://forums.phpfreaks.com/topic/269693-resuming-sessions/#findComment-1386486 Share on other sites More sharing options...
AndrewM16921 Posted October 19, 2012 Author Share Posted October 19, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/269693-resuming-sessions/#findComment-1386494 Share on other sites More sharing options...
Christian F. Posted October 20, 2012 Share Posted October 20, 2012 The Cookie Jar file should be available independent of the programing language used, or if he just used the CLI version. I just assumed the use of PHP since we're in a PHP forum. Quote Link to comment https://forums.phpfreaks.com/topic/269693-resuming-sessions/#findComment-1386546 Share on other sites More sharing options...
AndrewM16921 Posted October 20, 2012 Author Share Posted October 20, 2012 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). Quote Link to comment https://forums.phpfreaks.com/topic/269693-resuming-sessions/#findComment-1386598 Share on other sites More sharing options...
AndrewM16921 Posted October 20, 2012 Author Share Posted October 20, 2012 Actually, I guess I was doing it correctly afterall. Turns out you just can't hold objects in the session array I guess? I converted it to a string, then reconstructed it after, and it works fine. Weird. Quote Link to comment https://forums.phpfreaks.com/topic/269693-resuming-sessions/#findComment-1386608 Share on other sites More sharing options...
jcbones Posted October 20, 2012 Share Posted October 20, 2012 You can store objects in sessions, have you tried serialize() and unserialize()? Quote Link to comment https://forums.phpfreaks.com/topic/269693-resuming-sessions/#findComment-1386609 Share on other sites More sharing options...
PFMaBiSmAd Posted October 20, 2012 Share Posted October 20, 2012 (edited) 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 October 20, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/269693-resuming-sessions/#findComment-1386614 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.