How can I save data from one site to another sites database? I need to make a form which saves data to an external database. I tried using fopen but I don’t think this is the correct way to do it:
$externalsave = fopen('http://onewebsite.com/folder/this_page_INSERTS_INTO_DB.php?datavalue1='.$data1.'&datavalue2='.$data2, 'r');
$data = '';
while(!feof($externalsave))
$data .= fread($externalsave, 4092);
fclose($externalsave);
echo $data;
Does someone know how I can save values inserted in a form on one website, in an external database?
I’m using an Apache server, and the code above works fine, I just don’t trust it. Or can I?
There is 3 main solutions :
1. Remote action
The easiest one is to simply point your form on the remote server. For exemple :
And in
myphpscript.phpsimply handle the form like you will in localhost.2. Proxy action (require the allow_url_fopen directive to be enabled)
You could also point your form on a php script on the same server which gonna send your data on the other server.
This solution can be usefull to bypass security like csrf token :
proxy.php :
No need of curl here 😉
3. Remote database
Finally you can use a database with remote access and simply connect to the database with its remote host like :
Unfortunately this solution is rarely allowed because it could represent a big security risk (anyone in the world can try to connect to the database).