Using AJAX to secure sessions and save messages I have been working with PHP sessions and login protected pages for some time, but I want to start using some AJAX for managing some actions inside login protected pages (easy way with jQuery).

Here is what I want to do:

The user logs in (classic no-AJAX method), and wants to submit a message. I make a request with AJAX to another page, where the session is checked and the message is saved.

Here is how the start page looks:

&lt;?php 
    session_name('NEWSESSION');
    session_start();
    session_regenerate_id(true);

    if(!$_SESSION['user_logged']){
            header("location: login.php");
    }

    $sessionTTL = time() - $_SESSION["timeout"];
    if ($sessionTTL &gt; SES_LENGHT) {
            session_destroy();
            header("location: login.php");
    }
?&gt;

&lt;!-- page header and all other html things here --&gt;

&lt;div id="returned_message"&gt;&lt;/div&gt;
&lt;textarea id="message"&gt;TEST&lt;/textarea&gt;
&lt;input type="" id="submit" value="Submit message"&gt;
&lt;script&gt;
$("#submit").click(function(){
    $.ajax({ 
        type: 'POST',
        data: {
            action: 'submit_message',
            message: $("#message").val();
        },
        url: '/save.php',
        success: function(data) {
            $('#returned_message').html(data);
        }
    });
});
&lt;/script&gt;


And this is save.php where I send the request (and sends a "msg saved" alert):

&lt;?php 
session_name('NEWSESSION');
session_start();
session_regenerate_id(true);

if(!$_SESSION['user_logged']){
    header("location: login.php");
}

$sessionTTL = time() - $_SESSION["timeout"];
if ($sessionTTL &gt; SES_LENGHT) {
    session_destroy();
    header("location: login.php");
}

$username = $_SESSION['user_username']; 

if($_POST["action"] == "save_message"){
    /* PHP code for saving message */
    echo "Message saved!";
}
?&gt;


Is this the right way to do this? And how vulnerable is this (is it more vulnerable than to do it with classic PHP methods, with no fancy AJAX things)? All suggestions are welcome.
Consider adding User Agent check

Adding a User Agent check will add another layer of security. This will slow down Session Hijacking a bit.

AJAX alone is not a security issue

AJAX is no more vulnerable then regular access when it comes to sessions. 

SES_LENGHT is not defined

The constant SES_LENGHT is not defined anywhere. Is your code snippet complete?