Here I have a problem with curl request.
I want to block it in few files.
Here is my code on server
MyFile.php
Class MyFile {
public function getName() {
return "hi";
}
public function getBday() {
return '01-01-1970';
}
}
$my = New MyFile();
return json_encode($my->$_GET['functionName']());
localhost/test/MyFile.php?functionName=getName
Output: hi
localhost/test/MyFile.php?functionName=getBday
Output: 01-01-1970
I'm running that file in AJAX and for security, I'm using JWT tokens as Authorization header in AJAX request.
That thing is working fine.
But while I try to access that file via curl from another website. then it is also responding a data. but I have to prevent it.
AnotherServerFile.php
<?php
$header = array();
$header[] = 'Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE1MTk2NTA4NjMsImp0aSI6InQ4OTU3RjBIbE1yQ21UREdUdTNCK0VZdUFsTVZRZUZhNHhnckRyMzRnNXc9IiwiaXNzIjoibmlldHdhY2h0ZW4ubmwiLCJuYmYiOjE1MTk2NTA4NjMsImV4cCI6MTUxOTY1MjY2M30.wN7MA782ix-ln8uKlGNsc-oxMxJNa3hfmY9GjzhVeFhEZm5IJOMd0PU8ppUxDU9_yDGcbKsuPzNzCh6CmPQ1qA';
$header[] = "X-Requested-With: XMLHttpRequest";
$service_url = 'localhost/test/MyFile.php?functionName=getName';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //IMP if the url has https and you don't want to verify source certificate
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($response);
this file is on another server. like
example.com/AnotherServerFile.php
And AnotherServerFile.php file can access my MyFile.php's code via cURL.
So, I want to prevent the use of AnotherServerFile.php accessing MyFile.php
Any help will be appreciated.