We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28008
    • 23 Posts
    I have a website where I would like to use AJAX. I’m using mootools and as example use the following script:

    $('some_element').addEvent('click', function() {
        var myRequest = new Request.HTML({
            url: 'example.php',
            method: 'get'
        }).send('action=remove&id=100');
    });


    The request is send to example.php which in turn will remove something from the database. The goal is that only logged in webusers from certain usergroups are allowed to remove these particular records. At the moment I don’t know how I can secure this / verify if the request is allowed. I could secure the page containing the Javascript code, but if for some reason somebody with bad intentions learns out about the example.php file, he could remove records without being authorised by just loading "example.php?action=remove&id=100" directly into his browser. I’d like to know if it’s possible to validate a request in example.php even tho it’s an AJAX request.

    The only check I’m using now is:

    if (isAjax() !== true) {
        die();
    }
    
    function isAjax() {
        return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
    }


    But this can be bypassed if you know what you’re doing and still doesn’t tell me if the person initializing the XMLHttpRequest is in fact a valid webuser.

    I’ve been searching for a while now and don’t seem to be able to find an answer for the following. Hopefully somebody can tell me more about this subject.
      For those about to rock... I salute you
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      Remember that everything you can do with Ajax, you can do WITHOUT AJax.... Ajax only saves you a few clicks. So your question about this other page, example.php -- you should set it up first so you can make it work WITHOUT Ajax. On the example.php page, set up all of your user and role checking using the get parameters, e.g. example.php?action=remove&id=100-- it’s much easier to do error checking when YOU are initiating the request. With Ajax, some other page is initiating the request, so you can’t see if there are errors or what not. Savvy? Once you have it working without Ajax, then add the Ajax, and the same request should work the same way.
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        If you use a resource with a blank template and only a snippet as its content, and the snippet is your AJAX processor, then you’ll have access to everything MODx that any snippet would have.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 28008
          • 23 Posts
          Thanks Everett and Susan for the replies. The fact people like you do reply on this forum is one of the multiple reasons I like working with MODx. Thanks again and I’ll try to do the same myself more often.

          It turns out I wasn’t aware of the fact session data and such are also available in the AJAX processor. I just figured from the start they wouldn’t be and didn’t even bother to check it.
            For those about to rock... I salute you
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            Glad to help out. Session data should persist for a user’s session, regardless of which pages are requested, however the way that Evo and Revo handle sessions can be different depending on your server setup, but that probably doesn’t apply to you. Good luck!