Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protect call to remote php script

Tags:

security

php

  1. I have on a server a PHP scrip that updates a DB.

  2. I want to be able to call this script from remote, either from another server or my localhost PC using a GET, or from the browser using AJAX,

  3. But I don't want anyone to be able to call this script unless allowed.

So far I simply added into the script a piece of code to verify a certain pin in the GET, i.e.

//myscript.php

if( isset($_GET['pin']) && $_GET['pin'] === '1234' )
{
   //update the DB...

In this way remote caller must know the pin, i.e.

file_get_contents(http://remoteserver.com/myscrip.php?pin=1234); //will work
file_get_contents(http://remoteserver.com/myscrip.php?pin=5678); //will NOT work

This seems so simple that I'm wondering if it's secure.

What are other possible more secure alternatives (maybe not too more complicated)?

For instance, I read about using an hash that changes over time, but is it worth it, how could it be done?

like image 694
Marco Demaio Avatar asked Feb 28 '26 12:02

Marco Demaio


1 Answers

you could password protect the folder (can be done easy if you are using cpanel or plesk) and use curl to access that url.

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
like image 119
Mircea Soaica Avatar answered Mar 03 '26 01:03

Mircea Soaica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!