2

I have a simple post request that comes from one specific page of my site:

reqdata = 'text=' + mytext;
$.ajax({
  type: "POST",
  url: "/request.php",
  data: reqdata,
  cache: false,
  success: function(html) {
    alert(html)
  }
});

It goes to another page of the same site .. So the first page calls page mydomain.com/test.php calls http request to mydomain.com/request.php

How do I recognise on page mydomain.com/request.php that the original page the request come from was mydomain.com/test.php?

I wish to ensure that the request can be done only from this exact page mydomain.com/test.php and not from other domains nor page.

I do the request using ajax and javascript and therefore I think that I cannot add a hidden authentication that would ensure the whole thing is secure. Because each value is seen on the original page source code.

fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44
Brana
  • 1,197
  • 3
  • 17
  • 38

1 Answers1

1

It seems you are searching for a way to protect yourself against Cross Site Request Forgery (XSRF).

The common way to protect against XSRF is to render some time limited key on your page (just some <script> var mySecret = <?php (someSecret) ?>) and keep track of these. You could use the first characters of the session id for example and check if the request data contains this field. You can just add this to your data with something like ...&secret=mySecret When there is a session id with the first characters of this data attribute, you accept the request, otherwise you reject it on your server (possibly with error code 403).

So this question would be

best practices to avoid XSRF on my site

and should be asked on the information security stack exchange.

nasskalte.juni
  • 433
  • 3
  • 14