简体   繁体   中英

Including a PHP from another server using JS

I am attempting to build a very simple advertising system which is included on all of my websites and is called using a variable above the script to determine what kind of advert is displayed.

For this system, I have been using basic include functions. For example, on a clients website, near the footer, I would have:

$ad_type = 'banner';
include = '../../adsystem/adsystem.php';

The code for this adsystem.php is:

///// BANNER AD //////
if($ad_type == 'banner'){
        $today = date("Y-m-d");
        $sql = "SELECT * FROM `ad_adverts` WHERE `ad_start_date` <= '$today' AND `ad_end_date` >= '$today' AND `ad_type` = 'banner' ORDER BY RAND() LIMIT 1";
        $result = $ad_conn->query($sql);

                  if ($result->num_rows > 0) {
                     // output data of each row
                     while($row = $result->fetch_assoc()) {

                     $ad_id = $row["ad_id"];                    
                     $ad_link = $row["ad_link"];
                     $ad_direc = $row["ad_direc"];

                     ?>
                     <div align="center">
                     <a target="_blank" href="http://mysite.co.uk/adsystem/adsystem.php?redirect=<? echo $ad_link; ?>&adid=<? echo $ad_id; ?>"><img src="<? echo $ad_direc; ?>" alt="<? echo $ad_link; ?>" style="width:70%; height:70px;"></a>
                     </div>
                     <?


                              }
                         }
$ad_type = '';                  
}
/////////////////////

VERY BASIC AT THE MOMENT - IS NOT YET COMPLETE. Simply using this as a test to get it working, then I will deal with fixing security problems and SQL injection, etc...

This will then display the adsystem.php code at the bottom of the site, or wherever I decide to add it. And this works perfectly for local websites - although I am working with websites which are not on the same server, and as you can imagine, that's where I run in to problems due to security issues.

Google Adsense and other advertising agencies combat this issue by using JS code to call the adverts, although I'm not skilled enough with JS to do this. On that direction though, I am wondering if it might be possible to use JS to just call the PHP script or if that would even work?

If anybody could point me in the right place here that would be great?

Without JS you are not able to do it. Please check document.write method. Here you have an example how to add dynamically your JS file with ads into html document:

<script>
  var url = 'http://ads.com/buyme?rand='+Math.random()
  document.write('<script src="'+url+'"></scr'+'ipt>')
</script>

Under this url, you should generate piece of JavaScript code dynamically in PHP, which should contains content of advertising and write in into html document using document.write method again.

You could use AJAX to call asynchronously to that script. In your php return a JSON with available advertisements.

Also you should consider using template engine and/or PHP framework. Mixing views with business logic isn't great idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM