How can I find which domain is requesting the iframe from my server and store the domain in the database

I have a bunch of different websites requesting an iframe

html file from my server for different reasons.

Is it possible to track onload

which domain is querying iframe

and then store it in the database.

If the domain already exists, do not add it to the database.

+3


source to share


2 answers


It is not the domain that is requesting content for the iframe, it is the user's browser.

There is a small chance of seeing which page this iframe is enabled on: the referrer (erroneously spelled as "referent" in the standard document, so every browser had to copy this typo), available in PHP via $ _SERVER ['HTTP_REFERER "].



You can try and parse the domain from there, best using parse_url () .

+3


source


You can use a combination of jQuery post()

and PHP $_SERVER

:

$.post("collect.php", { source: "<?=$_SERVER['HTTP_REFERER']?>" } );

      



Your script team can now check the database to see existing source links and inject a new one if required. An example of using PDO:

if (isset($_POST['source']) && $source = $_POST['source']) {

   $db = new PDO('mysql:host=localhost;dbname=testdb;charset=UTF-8', 'username', 'password');

   // Be sure to sanitize $source

   if ($db->query("SELECT * FROM table WHERE source = $source")->rowCount() == 0) {
      $stmt = $db->prepare("INSERT INTO table(`time`, `source`) VALUES(?, ?)");
      $stmt->execute(array(time(), $source));
   }

}

      

+2


source







All Articles