0

Can I get information from which web site is came request to read XML file on my web server, and insert into database this information from request?

For example, I want to develop a some script and when client install my script on his server, every time when he go to his website he call simplexml_load_file to get information if there new version of script and how to update it. I would like to know who has my script.


"blind" I try to make with .htaccess but it didn't work anything :( when I call text.php:

<?php
$myFile = "save.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_SERVER['REMOTE_ADDR']." : ".$_SERVER['REQUEST_TIME']."\n";

fwrite($fh, $stringData);
fclose($fh);

 $xml = file_get_contents('test.xml');
    header("Content-Type:text/xml");  
    echo $xml;
?>

he put me on save.txt file information from which IP address call is coming. Can I know from which web address is call is coming? And how to fix to call XML file instead php file?

hakre
  • 193,403
  • 52
  • 435
  • 836
mrakodol
  • 1,143
  • 3
  • 12
  • 41
  • Of course you can. I don't have your script though. I have my own and those belonging to the company I work for. – Ben Dec 11 '11 at 18:19
  • possible duplicate of [using simple xml to read rss feed](http://stackoverflow.com/questions/4887300/using-simple-xml-to-read-rss-feed) – GordonM Dec 11 '11 at 18:28
  • I only have a function who read xml file from server: public function getVersion() { $url = 'localhost/testserver/latest.xml'; $xml = simplexml_load_file($url); if ($xml->data->version > $this->version) { $this->msgInfo("Info!New Version Available: " . $xml->data->newver,false); } } But, I don't know how to follow requirements on server where this file is located. – mrakodol Dec 11 '11 at 18:41

1 Answers1

2

maybe you want to try using super global $_SERVER['HTTP_REFERER']. Try echoing it to see what it contains, and you will get how it works. Put a link to this script on a forum or a different domain and click to see the results.

And to know exactly who have your script, you can make them register (in order to get their name), or save the domains in a database to view later as logs (and maybe fill in manually the details about that specific domain), etc, etc.

EDIT:

I see that you have an XML file. Well you should work with htaccess and handle that request with a different script, and in that script use $_SERVER['HTTP_REFERER'].

OR: make it be a php file that will be requested. And do all the stuff to save what information you need, like the referer domain, and at the end of the file read with what ever you want (fread, file_get_contents, file, etc) the real xml file and output it... if you have problems with it, try setting the header first using header(): http://www.php.net/manual/en/function.header.php , to make it look (and feel) like a true xml file. I thing the correct header would be text/xml, or better: application/rss+xml.

<?php
    file_put_contents('save.txt', $_SERVER['HTTP_REFERER'].PHP_EOL);
    $xml = file_get_contents('test.xml');
    header('application/rss+xml');
    echo $xml;
?>

save the file as test.php

then make another file someonewhousethescript.html, put it in the same folder, put in it a link to test.php (after you do the thing with .htaccess you should link directly to test.xml).

and go to mrakodol.info/someonewhousethescript.html and click the link

and look into save.txt. There you will most probably see mrakodol.info/someonewhousethescript.html this is what I think you wanted.

And if you really want to keep the extension to .xml, you can always use apache mod-rewrite. If it is necessary I will provide a sample.

As for the extension, then make a new file in that folder (the root folder of the website as it seems) and name it .htaccess, and in it write the following lines:

RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^GET$ [NC]
RewriteRule test.xml test.php [NC]
khael
  • 2,600
  • 1
  • 15
  • 36
  • Where put $_SERVER['HTTP_REFERER']? link to XML file is: http://mrakodol.info/test.xml – mrakodol Dec 11 '11 at 19:05
  • I do what you tell me.Can anybody test it please? http://mrakodol.info/test.php Why when I using $_SERVER['HTTP_REFERER'] script don't put anything into save.txt file? – mrakodol Dec 11 '11 at 20:23
  • So you did test it. And what was the result? You have to make a simple hmtl file, and put a link to mrakodol.info/test.php in order to work. If you just type it in the browsers address bar it won't be any refer there. – khael Dec 11 '11 at 20:29
  • OK I figure it out.Thank you so much!Thanks for help! – mrakodol Dec 11 '11 at 20:41
  • I'm glad I helped you, see the .htaccess example to keep your extension, if you haven't so far. – khael Dec 11 '11 at 20:42
  • Thank you "blind", you are so nice. – mrakodol Dec 11 '11 at 21:21
  • I try to make with .htaccess but it didn't work anything :( when I call text.php $stringData = $_SERVER['REMOTE_ADDR']." : ".$_SERVER['REQUEST_TIME']."\n"; he put me on save.txt file information from which IP address call is coming. Can I know from which web address is call is coming? And how to fix to call XML file instead php file? – mrakodol Dec 12 '11 at 16:15