Display Google AdSense ads to search engine traffic using html javascript - javascript

I am using below PHP code for Displaying AdSense Ads only for search engine visitor.
But now I need HTML/JavaScript Code for Displaying Ads on search engine visitor.
I'm trying to create HTML/JavaScript code, but failed.
Can I modify/create this HTML/JavaScript code?
Have any solution? or Have any HTML/JavaScript code?
<?php
$referrer = $_SERVER['HTTP_REFERER'];
$my_domain = "example.com";
$search_engines = "google|yahoo|bing|altavista|digg";
$pattern = "((http(s)?://)(\w+?\.)?(?!{$my_domain})({$search_engines}))";
if (preg_match("/{$search_engines}/i", $referrer) != false) {
echo <<<END
<script type="text/javascript"><!--
google_ad_client = "xx-xx-xxxxxxxxxxxxxxxxxx";
/* xxxxxxxx xxxxxx xxx xxx xxx xx xxxxxx */
google_ad_slot = "xxxxxxxxxxxxxx";
google_ad_width = xxx;
google_ad_height = xxx;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
END;
} else {
// Show something to visitors not referred by a search engine
}
?>
Please help me, Sir.

It's a little bit stupid, but you definetely can translate it to js:
<script type="text/javascript">
if (document.referrer.match(/google|yahoo|bing|altavista|digg/)) {
google_ad_client = "xx-xx-xxxxxxxxxxxxxxxxxx";
/* xxxxxxxx xxxxxx xxx xxx xxx xx xxxxxx */
google_ad_slot = "xxxxxxxxxxxxxx";
google_ad_width = xxx;
google_ad_height = xxx;
var script = document.createElement("script");
script.setAttribute("src", src);
document.getElementsByTagName("head")[0].appendChild(script);
} else {
// Show something to visitors not referred by a search engine
}
</script>

Related

Update page content from live PHP and Python output using Ajax

Long-time user, first-time asker. I've learned so much from the community and I love this site.
So here is what I'm shooting for. I want to have a web interface that runs ping commands on the backend. I ideally want a website that has a text input that allows you to enter an IP address or domain, a button that runs the command and a python script that runs from PHP to actually run the ping command. The tricky part for was to get the output to print to the website live as it is outputted on the command line. I want to do it this way as a way to future-proof the concept and eventually use different iperf parameters.
I built a little PHP page that "technically" gets the job done but I can't figure out how to only call the PHP script when the button is clicked. Since it's a PHP page, it runs whenever the page is loaded. So after some research, I figure ajax jquery is what I'm looking for. I've spent about 2 days trying different things that get me really close but it seems that I'm dancing around my solution.
From what I've learned about ajax, I essentially need a button that runs an ajax function that is linked to my working php script. I can get it to run the script but I can't get it to update the page content in a live/continuous manner. Only when the command is finished running.
Here is my php page that does what it needs to do but does it everytime the page is loaded/reloaded. Not ideal. I want the script to only run when the button is pressed.
liveping.php:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="liveping.php" id="ping" method="post" name="ping">
Domain/IP Address: <input name="domain" type="text"> <input name="ping" type="submit" value="Ping">
</form><?php
if (isset($_POST['ping'])) {
function liveExecuteCommand($cmd)
{
while (# ob_end_flush()); // end all output buffers if any
$proc = popen("$cmd 2>&1", 'r');
$live_output = "";
$complete_output = "";
while (!feof($proc))
{
$live_output = fread($proc, 4096);
$complete_output = $complete_output . $live_output;
echo "<pre>$live_output</pre>";
# flush();
}
pclose($proc);
}
}
$domain = $_POST['domain'];
$pingCmd = "python /var/www/html/ping.py ".$domain;
if (isset($_POST['ping'])) {
liveExecuteCommand($pingCmd);
}
?>
</body>
</html>
ping.py:
#!/usr/bin/python
import cgi
import os
import sys
ping = "ping -c 5 -W 2 "+sys.argv[1]
os.system(ping)
Some things I've tried:
<html>
<head>
<script>
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = setInterval(function() {
if (ajax.readyState == 4) {
document.getElementById('content').innerHTML = ajax.responseText;
}
},100);
function updateText() {
ajax.open('GET', 'ajax.php');
ajax.send();
}
</script>
</head>
<body>
<button onclick="updateText()">Click Me</button>
<div id="content">Nothing here yet.</div>
</body>
</html>
OR
<!DOCTYPE html>
<html>
<body>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load_tweets').load('ajax.php').fadeIn("slow");
}, 1000); // refresh every 10000 milliseconds
</script>
</head>
<div id="load_tweets"> </div>
</body>
</html>
WITH ajax.php
<?php
while (# ob_end_flush()); // end all output buffers if any
$proc = popen("ping -c 5 -W 2 google.com", 'r');
$live_output = "";
$complete_output = "";
while (!feof($proc))
{
$live_output = fread($proc, 4096);
$complete_output = $complete_output . $live_output;
echo "<pre>$live_output</pre>";
# flush();
}
pclose($proc);
?>
Thanks for any help!
You do not need python for showing ping results. Just two PHP files will be enough.
index.php will have the AJAX functionalities along with the form.
ajax.php will have the code to ping specified domain address.
I afraid that using jQuery you might not able to catch the live feed. Because it doesn't have any onreadystatechange. So, you might need to use vanilla JavaScript in this case. Here is a working demonstration:
index.php:
<!DOCTYPE html>
<html>
<head>
<title>Ping AJAX</title>
</head>
<body>
<div>
Domain/IP Address: <input id="domain" type="text">
<input id="ping" type="button" value="Ping">
</div>
<div id="result"></div>
<script>
function updateText(domain) {
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (this.readyState == 3) {
var old_value = document.getElementById("result").innerHTML;
document.getElementById("result").innerHTML = this.responseText;
}
};
var url = 'ajax.php?domain='+domain;
ajax.open('GET', url,true);
ajax.send();
}
document.getElementById("ping").onclick = function(){
domain = document.getElementById("domain").value;
updateText(domain);
}
</script>
</body>
</html>
ajax.php:
<?php
if (isset($_GET['domain'])) {
function liveExecuteCommand($cmd)
{
while (# ob_end_flush()); // end all output buffers if any
$proc = popen($cmd, 'r');
$live_output = "";
$complete_output = "";
while (!feof($proc))
{
$live_output = fread($proc, 4096);
$complete_output = $complete_output . $live_output;
echo "<pre>$live_output</pre>";
# flush();
}
pclose($proc);
}
$domain = $_GET['domain'];
$pingCmd = "ping ".$domain;
liveExecuteCommand($pingCmd);
}
else{
echo "No post request";
}
?>
Output:
Declaimer:
The ping command is changed as I am currently using Windows operating system. Update it according to your operating system.
As a first time questioner, you have described the problem neatly and also showed your efforts to solve the problem. I really appreciate it.
ajax.readyState == 4
essentially means, script on the other side has finished ... 3 is partial.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState
You just have to take all ajax script into the function
example:
function updateText() {
$.ajax({
type: 'GET', // can be POST, too
url: "ajax.php",
crossDomain: true,
data: {
firstvar: firstvar,
secondvar: secondvar
},
cache: false,
success: function(data) {
if($.trim(data) == "false") {
alert("Fail to recived data");
}
else {
// Success getting data
// Do some jobs
}
}
});
}
If you want to cancel submit to not refesh, U can use
return false; // At the end of the function above
Hope it helps.

How to send response with javascript to draw html elements

I'm creating a script like twitter in which user just provide an id and all his/her tweets get loaded on site where the script inserted.
What I've done is
User should copy this code to load my widget
<a class="getStarted" data-getStartedID="123456789">Get Started App ID</a>
<script>
!function(d,s,id){
var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';
if(!d.getElementById(id)){
js=d.createElement(s);
js.id=id;
js.src=p+"://localhost/practices/js_practice/siteOpen.js";
fjs.parentNode.insertBefore(js,fjs);
}}(document,"script","getStarted-C");
My siteOpen.js is as below :
!function(d){
var a = d.getElementsByClassName('getStarted');
var x = document.getElementsByClassName("getStarted")[0].getAttribute("data-getStartedID");
var r = new XMLHttpRequest();
var appID = x;
r.open("POST", "openwebIndex.php", true);
r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
r.setRequestHeader("Content-length", appID.length);
r.setRequestHeader("Connection", "close");
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return;
if(r.responseText.trim()==1){
return '<p>output to be draw on where script is pasted</p>';
if(console)console.info('Valid appID');
}
};
r.send('appID='+appID);
}(document);
i don't know what to do to send the response and load/draw my widget on user's website.
My response will be in html elements.
Please suggest me what should i do. I just stuck at this point.
EDIT
I'm getting object HTMLScriptElement when I alert js variable.
Just trying adding the html code in the body tag.
users html file
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
<script src="widget.js"></script>
Your widget.js
// var appId = d.getElementsByClassName('getStarted');
// process the app id and make the output here
var output = "<div>This is the content of the widget</div>";
document.body.innerHTML += output;
This will show the content in the users html file. If you have cross domain issue, use JSONP for resolving that.

PHP Displaying an embed before a certain date only

I am trying to display a Disqus commenting embed on a website, but I only want it to display the embed if it's before 2nd February 2014 UK time.
EDIT: For clarity, It should always display the embed before the 2nd Feb 2014 on any date but never after.
The PHP code I have is:
<?php if (time() <= strtotime('2014-02-02 00:00:00')) { ?>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'mysite'; // required: replace example with your forum shortname
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>
comments powered by <span class="logo-disqus">Disqus</span>
<?php } else {
//do something else
}?>
However, it doesn't seem to be displaying on posts. I have replaced 'my site'. This is just for an example.

Write JavaScript code inside JavaScript and all in PHP?

I have an ad that will appear when certain conditions, and when that ad appears, the ad will detect the width of the screen and use the ad size that I specify.
I did this using JavaScript, and inside JavaScript and all are in PHP. Problems arise when writing in document.write.
Could you please help me solve this problem.
This is my code:
<?php
if(!empty($variable1)) {
echo '
<script type="text/javascript">
var swidth = screen.width;
if (swidth >= 1218) {
document.write('
<script type="text/javascript">
document.write("
<script type='text/javascript'>
var adcode = {abcdefg};
</script>
<script type='text/javascript' src='http://domainname.com/ads.js'></script>
");
</script>
');
}
</script>
';
} else {
if(!empty($variable2)) {
echo $ads2;
} else {}
}
?>
from line 13 to 22 replace:
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://domainname.com/ads.js';
$("body").append(script);
var script2 = document.createElement('script');
script2.type = 'text/javascript';
script2.src = 'sc.js';// or url to the script file;
$("body").append(script2);
and sc.js:
var adcode = {abcdefg};
BUT
The best way to add JS in PHP:
<?php
// ...
?>
<script src="url_to_script"></script>
<?php
// ...
?>
Manipulations with DOM elements must be defined in JS and not affect PHP.

How load Adsense using Jquery

I'm trying to dynamically load Adsense at a specific point on my Wordpess posts - just before a div class that is manually inserted within the wysiwyg text area on each post. So far, I've used the following ...
<?php wp_enqueue_script( 'jquery' ); ?>
before wp_head, and after:
<script type="text/javascript">
jQuery(document).ready(function($){
$(".example-div").before("Here is the content");
});
</script>
In the above example plain text like "here is the content" loads in the desired position, but it doesn't work when I insert the Adsense code.
<script type="text/javascript">
jQuery(document).ready(function($){
$(".example-div").before("<script type="text/javascript"><!--
google_ad_client = "ca-pub-************";
/* Ad ID */
google_ad_slot = "********";
google_ad_width = 320;
google_ad_height = 50;
//-->
</script>
<script type="text/javascript"
src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script>");
});
</script>
Any suggestions on what modficiations are required - or best way to make the Adsense code appear - greatly appreciated.

Categories

Resources