doubei2231 2010-05-27 08:21
浏览 76
已采纳

使用PHP session_id()来确保我们的服务器动态生成iframe

We use iframes to show ads on our site.

Iframes are used to allow us to keep the ad generation code and other site modules separate.

As we track ad views on our site, and need to be able to keep an accurate count of which pagetype gets what views, I must ensure that users can't simply copy-paste the iframe in which the ad is loaded onto another site. This would cause ad count to become inflated for this page, and the count would not match the view count of the page the iframe "should" be displayed in.

Before anyone says so: no I can't simply compare the page view count with the ad view count, or use the page view count * number of ads per page, as # of ads per page will not necessarily be static.

I need to come up with a solution that will allow ads to be shown only for iframes that are generated dynamically and are shown on our pages.


I am not familiar with PHP sessions, but from what little reading I have had time to do, the following seems to be to be an acceptable solution:

Add "s = session_id()" to the src of the ad's iframe.

In the code that receives and processes ad requests, only return (and count) and ad if s == session_id().

Please correct me if I'm wrong, but this would ensure:

Ads would only be returned to iframes whose src was generated alongside the rest of the page's content, as is the case during normal use.

We can return our logo to ad calls with an invalid session_id.

So a simple example would be:

One of our pages:

<?php session_start(); ?>
<div id="someElement">
    <!-- EVERYONE LOVES ADS -->            
    <iframe src="http//awesomesite.com/ad/can_has_ad.php?s=<?php echo session_id(); ?>></iframe>
</div>

ad/can_has_ad.php:

<?php session_start(); ?>
if($_GET['s'] == session_id()){
    echo 'can has ad';
}
else{
    echo '<img src="http://awesomesite.com/images/canhaslogo.jpg"/>';
}

And finally, copied code with static 's' parameter:

<!-- HAHA LULZ I WILL SCREW WITH YOUR AD VIEW COUNTS LULZ HAHA -->            
<iframe src="http//awesomesite.com/ad/can_has_ad.php?s=77f2b5fcdab52f52607888746969b0ad></iframe>

Which would give them an iframe showing our awesome site's logo, and not screw with our view counts.

I made some basic test cases: two files, one that generates the iframe and echos it, and one that the iframe's src is pointed to, that checks the 's' parameter and shows an appropriate message depending on the result. I copied the iframe into a file and hosted it on a different server, and the correct message was displayed (cannot has ad).


So, my question is:

Would this work or am I being a PHP session noob, with the above test being a total fluke?

Thanks for your time!

Edit:

I'm trying to solve this without touching the SQL server, as the rest of the site is very SQL intensive and I don't want to add load to the already creaky SQL server...

  • 写回答

1条回答 默认 最新

  • douyoupingji7238 2010-05-27 10:11
    关注

    You shouldn't really put the session id in the url because it makes it vulnerable to referrer snooping. (If the user follows a link from a page with the session id in the url, they will be able to see the session id in the referrer header)

    Rather than using an iFrame, you might consider making the request for the iFrame contents on the server (eg using curl) and printing it onto your page. That way there will be no way for anyone else to directly embed it.

    You could also consider checking the referrer header before loading the iFrame (which should be the containing page's url) but it is very easy to fake referrer headers so this shouldn't be considered a 'good' solution.

    In summary, I'd strongly suggest finding another way of putting the ads onto your page!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么