I don't want to display ads to Facebook, Twitter Visitors - php

I am now using this code for facebook. "https" is active and wordpress site.
<?php
$ref = $_SERVER['HTTP_REFERER'];
if (strpos($ref, 'facebook.com') != false) {
?>
DON'T SHOW ADS
<?php } else { ?>
SHOW ADS
<?php } ?>
This code works for facebook. I wanted to add twitter, but when I add twitter it doesn't work at all. I tried this.
if (strpos($ref, 'facebook.com', 'twitter.com', 't.co') != false) {
It didn't work that way. If else query or "false" is correct? How can I do it in the simplest way? If the visitor comes from Facebook, Twitter, I don't want to show ads. thanks

strpos() does not check multiple "needles" to look for. You can store them in an array
and iterate over each one individually though:
<?php
$ref = $_SERVER['HTTP_REFERER'];
$sitesWithAdsHidden = [
'facebook.com',
'twitter.com',
't.co',
];
$isHiddenSite = false;
foreach ($sitesWithAdsHidden as $site) {
if (strpos($ref, $site) !== false) {
$isHiddenSite = true;
break;
}
}
if ($isHiddenSite) {
?>
DON'T SHOW ADS
<?php } else { ?>
SHOW ADS
<?php } ?>
Note that I also changed the strpos comparison to !== because a non-strict check could lead to evaluating to false if the position is actually 0 (the start of the string).

First and foremost, directly from Wikipedia:
"The referrer field is an optional part of the HTTP request sent by the web browser to the web server."
Therefore, you should always check that the Http Referer exists in the request. You can achieve this by using !empty() or isset(), however, for future maintainability, you can also use array_diff and array_keys.
You can then also achieve this without having to iterate over an array using preg_match.
if(!array_diff(['HTTP_REFERER'], array_keys($_SERVER)))
if(preg_match('/facebook|twitter/', $_SERVER['HTTP_REFERER']))
// todo: disable adverts
You could also use the null cascading operator to reduce this to one line. Do this if you have no further checks to make from the $_SERVER global variable.
if(preg_match('/facebook|twitter/', $_SERVER['HTTP_REFERER'] ?? ''))
// todo: disable adverts

Related

extract link from array php

I am trying to isolate link from array but in foreach loop it does not work for me.it cosider both elements as a link.
i just want to hyper link google.com and not bakery text but i am getting link on both so if part is not working and its considering bakery as a link.
$services=array('Bakery','www.google.com');
foreach($services as $service):
if (!filter_var($service, FILTER_VALIDATE_URL) === false) {
$service = $service;
} else {
$service = '<a href='.$service.'>'.$service.'</a>';
}
echo $service;
endforeach;
The problem here is with the following statement:
if (!filter_var($service, FILTER_VALIDATE_URL) === false)
^ ^^^^^
You're using a double negative here, being the ! operator which means "not" and you're using "false".
Either remove the ! or change the "false" to "true".
You're also going to need to add http:// to the url you wish to use in order to validate properly.
$services=array('Bakery','www.google.com');
will fail for the Google link. If you want it to validate, you will need to change it to:
$services=array('Bakery','http://www.google.com');

Display different content depending on Referrer

Hey I am trying to display a different phone number for visitors my website from my Google adwords campaign.
The code below works without the else statement (so if I click through to the page from Google it will display a message, and if I visit the site regularly it does not). When I added the else statement it outputs both numbers. Thank you
<?php
// The domain list.
$domains = Array('googleadservices.com', 'google.com');
$url_info = parse_url($_SERVER['HTTP_REFERER']);
if (isset($url_info['host'])) {
foreach($domains as $domain) {
if (substr($url_info['host'], -strlen($domain)) == $domain) {
// GOOGLE NUMBER HERE
echo ('1234');
}
// REGULAR NUMBER HERE
else {
echo ('12345');
}
}
}
?>
Your logic is slightly skewed; you're checking to see if the URL from parse_url matches the domains in your array; but you're running through the whole array each time. So you get both a match and a non-match, because google.com matches one entry but not the other.
I'd suggest making your domains array into an associative array:
$domains = Array('googleadservices.com' => '1234',
'google.com' => '12345' );
Then you just need to check once:
if (isset($url_info['host'])) {
if (isset($domains[$url_info['host']])) {
echo $domains[$url_info['host']];
}
}
I've not tested this, but it should be enough for you to see the logic.
(I've also removed the substr check - you may need to put that back in, to ensure that you're getting the exact string that you need to look for)

Extract location from URL using strpos

How do I extract a location from a url using strpos? If I can't use strpos, then what is a good alternative?
Here's the URL:
http://www.kijiji.ca/rss-srp-jobs/ontario/c45l9004
I've tried to use strpos:
if (strpos($url,'ontario') !== false){
echo "yes";
}
But it comes up with nothing. On top of this, I need to determine the source of the feed. The breakdown's like this:
Determine the source of the RSS Feed (Kijiji)
Check for the location in the $url variable (ontario) only if the source is Kijiji.
Here's my code so far:
if (strpos($url,'kijiji') !== false){
if (strpos($url,'ontario') !== false){
echo "ontario";
}
}
Ideally, Id like this script to check for a location in many different urls, so I can pinpoint the location when displaying a Job Ad.
Use parse_url function in order to get all the infgo you need.
See parse_url
Your script works for me. Are you sure you have defined $url? Like:
$url = 'http://www.kijiji.ca/rss-srp-jobs/ontario/c45l9004';
if (strpos($url,'ontario') !== false){
echo "yes";
}
And indeed have a look at url_parse function, which is for this kind of things
means domain related locality/local jobs
$listedDomains["http://www.kijiji.ca/rss-srp-jobs/%loc%/c45l9004"] = "ontario";
$listedDomains["http://www.example.com/rss-srp-jobs/%loc%/c45l9004"]= "bangalore";
foreach($listedDomains as $keyUrl=>$keyLocation)
{
echo str_replace("%loc%",$keyLocation,$keyUrl);
echo "<br />";
}

automatically detect iframe blocking

I use iframes in my news aggregation web app. While I am careful to always credit and link publishers, I also respect those who chose to block iframes (by not implementing sneaky workarounds). My question is how I can automatically detect whether iframes will be blocked, given the URL of the external page in question. So far i am using this code:
//get external html
$p = file_get_contents($this->scrape_ready_url);
//check for blocker
$pattern1 = "/window\.self.{1,10}window\.top/";
$s1 = preg_match($pattern1, $p);
//check for blocker2
$pattern2 = "/window\.top.{1,10}window\.self/";
$s2 = preg_match($pattern2, $p);
//condition response
if ($s1 === 1 || $s2 === 1) {
$this->frame = "blocked";
} else {
$this->frame = "not_blocked";
}
This works most of the time (so far), but many publishers such as yahoo use slight variations of the "self !== top" code which makes preg_match not work. I am wondering if there is any universal/general test that I can implement to know whether or not a given URL will block an iframe or not.
thanks

Custom URL routing with PHP and regex

I'm trying to create a very simple URL routing, and my thought process was this:
First check all static URLs
Then check database URLs
Then return 404 if neither exists
The static URLs are easy to do of course, but I'm trying to figure out the best way to do dynamic ones. I would prefer not to have to set a static prefix, despite knowing that it would make this a lot easier to code.
This is what I currently have:
$requestURL = $_SERVER['REQUEST_URI'];
if ($requestURL == '/') {
// do stuff for the homepage
}
elseif ($requestURL == '/register') {
// do stuff for registration
}
// should match just "/some-unique-url-here"
elseif (preg_match("/([\/A-Za-z0-9\-]+)/",$requestURL)) {
// query database for that url variable
}
// should match "/some-unique-url/and-another-unique-url"
elseif (preg_match("(^\/[A-Za-z0-9\-]+\/[A-Za-z0-9\-]+)/",$requestURL)) {
// query database for first and second variable
}
else {
// 404 stuff
}
My problem is that if I have "/register" URI, it will match the second elseif statement as well as the regex statement. But I want to avoid having to specifically exclude each static URL from regex statement, such as this:
// should match just "/some-unique-url-here"
elseif ((preg_match("/([\/A-Za-z0-9\-]+)/",$requestURL)) &&
($requestURL !== '/register') &&
($requestURL !== '/')) {
// query database for that url variable
}
What's the easiest way to solve this problem? I'll probably have like 15-20 static URLs, so specifically excluding all of them would be very clunky.
Your problem does not exist. If the first elseif ($requestURL == '/register') matches, all subsequent elseifs on the same level won't get evaluated.
You're already doing it right, just make sure you do the string comparisons (==) first.
On another note, don't reinvent the wheel.
https://github.com/bramus/router
http://toroweb.org/
http://zaphpa.org/

Categories