Jump to content

Some php session vars not available to ajax script


braunshedd

Recommended Posts

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);
    }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();... :shrug:

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!  :D

Link to comment
Share on other sites

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.)

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

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.