Jump to content

How to prevent direct access to the file called via Ajax?


eevan79

Recommended Posts

I use jQuery when adding messages. However, the file can be called directly. For example: includes/add_comment.php?id=2

 

So, I can make a form and call this file directly to add a message. ID is user id and form can be submited with HTML form wherever are located.

 

How to prevent direct access to the file when called through a Ajax?

Link to comment
Share on other sites

It's impossible to stop someone making a request directly to a file called through Ajax.  But what you can do is use some sort of authentication, the type depending on what you want to achieve.

 

If you want to stop spam, you would use a Captcha.  If you want to authenticate the user, you would store the user id in session data and use that in add_comment.php, instead of expecting it to be passed via Ajax.

Link to comment
Share on other sites

I already use authentication, but I don't want to registered user access the file directly.

 

 

Maybe I can use $_SERVER['HTTP_REFERER'] to check if file is called from website, instead directly. If user calls the file directly, $_SERVER['HTTP_REFERER'] will be empty.

 

 

But I think that this is not enough ...

Link to comment
Share on other sites

It all depends on what you are trying to prevent.  If you want to stop dedicated spammers, you need Captcha.  If you want to stop casual users playing around, checking the referrer is probably enough.  A posting timeout might also be appropriate, such as making people wait at least 60 seconds between posts, if spamming is what you are trying to prevent.

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.