PHP会话 - 随机不写入值

I am building a site that combines ajax and PHP to create dynamic forms that load/validate/submit in-page. I am using a combination of PHP session variables with custom handlers, $.post, and $().load to accomplish this. I build my forms in individual php pages (they are very large) and then load them into a section of my main page.

At the top of my main page I start a session like so:

<?php 
require("inc/SecureSession.php"); // Custom handlers
session_save_path($_SERVER['TEMP']); session_start();
?>

Then in each form page I start a session and generate a token variable, like so:

<?php
require("inc/SecureSession.php"); // Custom handlers
session_save_path($_SERVER['TEMP']); session_start();
$_SESSION['token'] = rand();
?>

This token variable gets inserted as a hidden field at the bottom of the form page, and should write to the session.

However, I've found that session variables set in dynamically loaded (or posted) pages do not seem to consistently stay set. They work within the scope of the form page but then do not always carry over back to the main page. I thought this might be because they were not actually accessing the right session, but it seems all session variables outside the form page can be accessed within the form page.

So I am stumped. The PHP handler I am using is the one here: http://www.zimuel.it/en/encrypt-php-session-data/

Edit: When I load one of the form-pages directly, it writes 100% of the time. It is only when I load a form via jQuery that it gets wonky.

Edit2: The issue wasn't that I was declaring the code in the wrong order. I did some research and found that I am supposed to declare the save path and handler functions before I start the session. The issue appears to have been either that I opened php with "