braunshedd Posted July 26, 2011 Share Posted July 26, 2011 Ok, i've written a series of php pages which store user data entered from previous pages. The last page calls an ajax php file, but this ajax file finds almost all of the session vars set previously to be null. All of the other php pages, however, are able to access these vars. Any idea what i'm doing wrong? index2.php => takes file uploaded from previous page and puts name in session <?php session_start(); session_unset(); session_destroy(); header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past ?> <?php session_start(); $_SESSION['file'] = $_FILES['file']['name']; mkdir("/blend/". $_FILES['file']['name']); mkdir("/blend/" . $_FILES['file']['name'] . "/frames"); move_uploaded_file($_FILES["file"]["tmp_name"], "/blend/". $_SESSION['file'] . "/scene.blend"); ?> control.php => takes other user input from input2.php and puts into session <?php header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past ?> <?php session_start(); $_SESSION['endFrame'] = $_POST['frameEnd']; $_SESSION['format'] = $_POST['format']; $_SESSION['currFrame'] = $_POST['frameStart']; var_dump($_SESSION); ?> updateAjax => is the ajax script called by control.php, only $_SESSION['file'] appears to be set (this is only a snippit) function sendInfo() { $output = array(); $output['status'] = "Currently Rendering"; if (!renderRunning()) { $output['status'] = "Starting Render"; } if ( !renderRunning() && renderFinished()) { $output['status'] = "Zipping all files"; } $output['currFrame'] = $_SESSION['currFrame']; $output['lastFrame'] = $_SESSION['endFrame']; if (renderFinished()) { $output['downloadButton'] = true; $output['downloadLink'] = "/var/www/" . $_SESSION['fileDir'] . "/frames.zip"; } else { $output['downloadButton'] = false; } $output['imgSrc'] = $_SESSION['imgSrc']; $output['status'] = var_dump($_SESSION); echo json_encode($output); } Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 all files need to have session_start(); at the beginning to be able to access the session variables. maybe it's missing in the last one? Quote Link to comment Share on other sites More sharing options...
braunshedd Posted July 26, 2011 Author Share Posted July 26, 2011 higher up in the script i do include session_start(); Quote Link to comment Share on other sites More sharing options...
xyph Posted July 26, 2011 Share Posted July 26, 2011 Perhaps the updateAjax is executed by the client before the other code? Debugging state issues in a by-design stateless medium can be troublesome. Try simply returning the raw output of $_SESSION in the ajax call. Compare it to where in your script you expect that to be your session dump. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 try putting session_start(); before you output the headers in control.php Quote Link to comment Share on other sites More sharing options...
braunshedd Posted July 26, 2011 Author Share Posted July 26, 2011 I've tried changing the positioning of the headers vs the session_start();, but no difference Someone on another fourm got on to me for putting headers after the session_start();... Anyways, in response to the previous post, the control.php should be executed before the ajax call (or so i understand) and the raw output of $_SESSION still does not include the other missing vars Thank you all for helping me out so quickly though! Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 In that case, I suggest you put this: echo '<pre>'; print_r($_SESSION); echo '</pre>'; at the end of each page, just to track the session variables and figure out where it's going wrong. Quote Link to comment Share on other sites More sharing options...
braunshedd Posted July 26, 2011 Author Share Posted July 26, 2011 I have been doing that with var_dump($_SESSION), and i've discovered that the vars dissappear only on the updateAjax.php Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 try this, just for my own peace of mind: remove session_unset(); and session_destroy(); from index2.php and try again. (If your variables exist at the end of control.php and not in updateAjax, they are being destroyed somewhere.) Quote Link to comment Share on other sites More sharing options...
braunshedd Posted July 26, 2011 Author Share Posted July 26, 2011 When I do that nothing different happens Funny thing is, the session isn't destroyed entirely when it reaches updateAjax.php, as the var file in the session is still readable (set in input2.php) What on earth is the problem :-\ Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 that's why I thougt the problem would be in index2.php, because it actually destroys the session, but then creates a new one and sets the var file again. that would explain why you only have that variable. with he info we have seen, we're not going to reach any conclusion. wanna post the complete code? Quote Link to comment Share on other sites More sharing options...
braunshedd Posted July 26, 2011 Author Share Posted July 26, 2011 Will do Had to remove all images to fit file size though [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 27, 2011 Share Posted July 27, 2011 man, that took me some time.... this is a quick fix, and not the root of the problem, but it may help you to figure out the rest. (by the way, nice code!) in control.php, where you have session_start(); $_SESSION['endFrame'] = $_POST['frameEnd']; $_SESSION['format'] = $_POST['format']; $_SESSION['currFrame'] = $_POST['frameStart']; put: session_start(); if(isset($_POST) && !empty($_POST)){ $_SESSION['endFrame'] = $_POST['frameEnd']; $_SESSION['format'] = $_POST['format']; $_SESSION['currFrame'] = $_POST['frameStart']; } now it works. I didn't go searching for the problem after this, but for some reason control.php must be loading twice and the second time $_POST is empty so the variables get reset. hope this helps Quote Link to comment Share on other sites More sharing options...
braunshedd Posted July 27, 2011 Author Share Posted July 27, 2011 I'm trying to run this code online using coderun, but it's pulling 500 errors on me :'(, although it's not your code that causes it Well, when I finally get access back to my php server, i'll be sure to test your fix. Considering it worked for you, my hopes rest high Thank you soooooo much for all your help, nobody has been able to find out the problem thus far. You have made a valuable contribution to the OpenRender project Quote Link to comment Share on other sites More sharing options...
xyph Posted July 27, 2011 Share Posted July 27, 2011 Just an FYI WebStyles if(isset($_POST) && !empty($_POST)) is redundant. empty() will not throw a notice if you put a variable that doesn't exist into it. It's even more redundant because $_POST should always exist as an array. You can simply change it to if( !empty($_POST) ) And just to be the devil's advocate, you really should use the following if( !empty($_POST['frameEnd']) ) $_SESSION['endFrame'] = $_POST['frameEnd']; if( !empty($_POST['format']) ) $_SESSION['format'] = $_POST['format']; if( !empty($_POST['currFrame']) ) $_SESSION['currFrame'] = $_POST['frameStart']; That way, in case some rouge form happens to populate the $_POST value somehow, you shouldn't get nullified results. If you hate repeating code like that, use $fields = array( 'frameEnd', 'format', 'currFrame' ); foreach( $fields as $field ) if( !empty($_POST[$field]) ) $_SESSION[$field] = $_POST[$field]; Both use 3 lines, but if you have 10 fields you want to populate, the second chunk will save you some lines without losing much efficiency. Quote Link to comment Share on other sites More sharing options...
braunshedd Posted July 27, 2011 Author Share Posted July 27, 2011 IT WORKS!!!!!! Quote Link to comment 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.