在拉入AJAX数据时如何忽略Adsense?(How to ignore Adsense when pulling in AJAX data?)

编程入门 行业动态 更新时间:2024-10-26 21:31:01
拉入AJAX数据时如何忽略Adsense?(How to ignore Adsense when pulling in AJAX data?)

我使用谷歌的Adsense和无限负载。 每当我通过AJAX在页面上“加载更多”数据时,它也会自动从Google中提取另一个广告,由于Google不支持通过AJAX引入广告,因此显然不会显示。

我需要帮助的逻辑是如何只在页面加载时显示1个广告,每当按下加载更多按钮时,如何忽略@include ('components.responsive_ad_display_only')

我目前考虑过使用会话。 我正在使用session()->put('retrieve', '1'); 我加入广告后立即。 然后一旦按下“加载更多”,我检查会话是否已设置。 如果已设置,我在提取更多数据时不会包含广告。 这种情况的问题是我最终需要“忘记”会话,以便当访问者刷新主页时,会出现一则新广告。

这是我使用的代码:

@php if (! session()->has('retrieve')) { $i = 1; $rand = rand(1, 10); session()->put('retrieve', '1'); } else { $i = 1; $rand = 200; // any integer higher than media items displayed session()->forget('retrieve'); } @endphp @foreach ($media as $media_item) @if ($i == $rand) <div class="ad_item"> <!-- Ad Displayed here --> @include('components.responsive_ad_display_only') </div> @endif <div class="media_item"> <h1>Media Item Name</h1> </div> @endforeach

此代码段不适用于每隔一段时间显示广告,因为会话每次都会设置和取消设置。

我希望有任何帮助和方向来解决我的困境。

非常感谢!

I use Google Adsense and infinite load. Whenever I "load more" data on the page through AJAX, it automatically pulls in another ad from Google as well, which obviously is not displayed since Google does not support to bring in ads through AJAX.

I need help with the logic of how I can only show 1 ad on the load of the page and whenever the load more button is pressed, how to ignore the @include ('components.responsive_ad_display_only')

I currently thought about using sessions. I am using session()->put('retrieve', '1'); right after I include the ad. Then once "load more" is pressed, I check if the session is set. If it is set I am not including the ad while pulling in more data. The issue with this scenario is the fact that I need to "forget" the session eventually, so that when the visitor refreshes the homepage, a new ad would appear.

This is the code I use:

@php if (! session()->has('retrieve')) { $i = 1; $rand = rand(1, 10); session()->put('retrieve', '1'); } else { $i = 1; $rand = 200; // any integer higher than media items displayed session()->forget('retrieve'); } @endphp @foreach ($media as $media_item) @if ($i == $rand) <div class="ad_item"> <!-- Ad Displayed here --> @include('components.responsive_ad_display_only') </div> @endif <div class="media_item"> <h1>Media Item Name</h1> </div> @endforeach

This code snippet works for not displaying an ad every other time, because the session keeps getting set and unset each time.

I would appreciate any help and direction towards solving my dilemma.

Thanks a lot!

最满意答案

为防止广告出现在ajax请求中,有许多方法,包括但不限于

在ajax请求URL中添加参数

用它就好

//in JS ... url = "/somefile.php?page=1"; // update it to look like url = "/somefile.php?page=1&ajax=1"; ... //in PHP if(!isset($_GET["ajax"]) || $_GET["ajax"] != 1){ //check ajax param in URL /// Print your ad here 使用PHP检查标头(不确定的方式,但几乎无处不在)

喜欢这个

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { /// Print your ad here

还有其他方式涉及会话和其他类型的验证,但这些也是最简单的

To prevent the ad from showing up on ajax requests there are many ways including but not limited to

Adding a parameter in the ajax request url

Use it like

//in JS ... url = "/somefile.php?page=1"; // update it to look like url = "/somefile.php?page=1&ajax=1"; ... //in PHP if(!isset($_GET["ajax"]) || $_GET["ajax"] != 1){ //check ajax param in URL /// Print your ad here Checking the headers using PHP (not sure way, but it works almost everywhere)

Like this

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { /// Print your ad here

There are other ways involving sessions, and other types of verification but these too are the simplest

更多推荐

本文发布于:2023-07-29 14:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316805.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:拉入   数据   AJAX   Adsense   pulling

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!