Jump to content

Recommended Posts

Ok, so I have a PHP file, called API.php which contains a series of functions that get a result from a SOAP call using a UserId and Password. The thing is, I intend to call apon API.php using AJAX, so the API.php has to output the information. I was wondering if there was anyway I can make it so that it will only give access to requests sent by that one page?

Link to comment
https://forums.phpfreaks.com/topic/189514-how-can-i-secure-this/
Share on other sites

If your ajax call is only in the page where you want to allow the calls from, then I suggest you add a parameter to the information passed to API.php, and if that parameter is not present, then API.php will do nothing or return an error message.

 

That way, it will only function when called by ajax, from the correct page

OK, point taken, but  the code used could change each time the page is loaded.

 

Obviously the called page would need to know the current value, but that could be handled, with a database value or a text file on the server, or even a session value.

 

It is very difficult to be absolutely secure in what you want to do, but you can make it harder for someone to access in ways that you do not want them to.

I personally don't have much experience with AJAX, but could you use $_SERVER['HTTP_REFERER'] on the called page and make sure that it comes from your site?  I can't imagine that it's 100% secure but at least it isn't visible in the source code.

Had my morning cup of brew and woken up, and obviously you're right :P  I always forget that it's passed via the browser :)

 

I'm sure there must be an elegant solution to this, will an AJAX called page be able to detect a define() on the originating page or is it totally independent?

The ajax called page is independant of the calling page. It can only know what is passed to it.

 

It can be called with GET or POST parameters, or of course can share a session.

 

The choice of GET or POST with an ajax call is purely down to the amount of data you need to send, as the url on such a call is not seen in the browser, so visibility of GET params is not a problem. Some people would say that POST with ajax is harder to set up, but you just need to know the differences in setting up the POST data, from just adding the params to the url you are calling.

OK, point taken, but  the code used could change each time the page is loaded.

 

Obviously the called page would need to know the current value, but that could be handled, with a database value or a text file on the server, or even a session value.

 

It is very difficult to be absolutely secure in what you want to do, but you can make it harder for someone to access in ways that you do not want them to.

 

Ah, A single use token. Yeah this could be done,

OP: Single use tokens are a key string usually generated by a hash function, like md5. this key is than imbedded in your javascript, and also added to a database.

When the javascript retrieves a page, it also sends this key. the key is verified against the db and removed if found.

and you can regenerate a new token, and send it back with your api function

 

I think that is pretty secure method, when not using username/password.

very well done jl :)

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.