AJAX request authentication to PHP script Recently I started looking at my AJAX call security and found some code that uses tokens, request and referer in an attempt to authenticate the call and stop XSS and CSRF.

The AJAX calls are for the article list / article table in the admin section.

Note: The PHP will not run unless you are logged in &mdash; it uses a cookie and sessions to authenticate user.

EDIT: Implemented schisms function name suggestion.

The following code works as it should but is it a good method?

Create our Token:

$token = md5(rand(1000,9999)); 
$_SESSION['token'] = $token; 


The button to deactivate / activate an article:

&lt;td&gt;
if($article_active === 1){ 
echo '&lt;input type="button" class="active_button" title="Deactivate" value="YES" name="'.$article_id.'"/&gt;'; 
} 
else { 
echo '&lt;input type="button" class="active_button" title="Activate" value="NO" name="'.$article_id.'" /&gt;'; 
}
echo '
&lt;/td&gt;


The AJAX call:

// ACTIVATE / DEACTIVATE WITH LINK
$(document).on("click",".active_button",function() {

    var form_data = {
        articleID: $(this).attr("name"),
        active: $(this).val(),
        selector: $("#selector").attr("name"),
        data: $("#data").val(),
        token:'&lt;?php echo $token; ?&gt;', 
        is_ajax: 1
    };

    $.ajax({
    type: "POST",
    url: "controllers/articlecontrol.php",
    data: form_data,
    success: function(data){
        document.location.reload(true);
    }
    });
});


The PHP that validates the AJAX request and sends the data to a PHP function:

// ACTIVATE / DEACTIVATE w/BUTTON
if(isset($_POST['active']) &amp;&amp; $selector === 'article') {

    if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' &amp;&amp; isset($_POST['token']) &amp;&amp; $_POST['token'] === $_SESSION['token']){

                $articleid = $_POST['articleID'];
                $active = ($_POST['active'] === 'YES') ? 0 : 1;
                $backend-&gt;setArticleStatus($articleid, $active);
            }
            else {
                $_SESSION['status'] = '&lt;div class="error"&gt;There was a Problem Activating / Deactivating that Article&lt;/div&gt;';
            }
}


The PHP activate / deactivate function (part of a class):

/**
 * Activate / Deactivate Article w/Button
 */

 public function setArticleStatus($articleid, $active) {
    $query = 'UPDATE wcx_articles SET article_active = :active WHERE article_id = :articleid';
    $stmt = $this-&gt;queryIt($query);
    $stmt = $this-&gt;bind(':active', $active);
    $stmt = $this-&gt;bind(':articleid', $articleid);
    if($this-&gt;execute()) {
        if ($active === 0) {
            $_SESSION['status'] = '&lt;div class="success"&gt;Article Deactivated Successfully&lt;/div&gt;';
        } 
        else {
            $_SESSION['status'] = '&lt;div class="success"&gt;Article Activated Successfully&lt;/div&gt;';
        }
    }
    else {
        $_SESSION['status'] = '&lt;div class="error"&gt;There was a Problem Activating / Deactivating that Article&lt;/div&gt;';
    }
 }
Prefer mt_rand() to rand().
Just a comment: your usage of tokens is similar to using nonces. There are established nonce generation libraries for PHP, and I suggest you take a look into them.
HTTP_X_REQUESTED_WITH is in general not a 100% reliable way to detect AJAX-ness. However, jQuery sends this header with its AJAX calls, so you should be okay. I'd suggest getting rid of the is_ajax in your request data though.
Your input element is at best confusing, and at worst messy. Why is Deactivate paired with YES, and Activate with NO? I'd prefer just using deactivate and activate as the button values, and templating inline:

&lt;td&gt;&lt;input type="button" class="active_button" name="&lt;?= $article_id ?&gt;" value="&lt;?= $article_active ? "de" : "" ?&gt;activate" /&gt;&lt;/td&gt;

activateDeactivate isn't a very good name. Maybe setActiveStatus or something?