Using file_get_contents for multiple sites with same content - php

I want to grab a code from some websites (all of them have the same content but some of them have downtime).
So i want to make a code that checks the first site and if the code was found show it, if not check the second site etc.
The code that i have is this:
$website1 = file_get_contents("http://exemplesite1.com");
preg_match("' src=\"(.*?)\" type='si", $website1, $body);
$decoded_url = $body[1];
if ( $decoded_url == "" ) {
$website2 = file_get_contents("http://exemplesite2.com");
preg_match("' src=\"(.*?)\" type='si", $website2, $body);
$decoded_url2 = $body[1];
} elseif ...
Here i'm blocked, i have like 6 sites, i want to do this untill it finds the code i need.

Put the websites in an array, and loop through that. Assuming you only need the first one, you can exit the loop upon finding a match. Something like this:
$websites = ['http://exemplesite1.com', 'http://exemplesite2.com', ...];
foreach($websites as $website) {
preg_match("' src=\"(.*?)\" type='si", $website, $body);
$decoded_url = $body[1];
if (! empty($decoded_url)) //found proper match
break;
}

Related

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)

Replace comma with OR conditional (PHP)

I have an option for the users to write which pages they don't want to see.
Simplifying, is a text input where they will write the pages names, such "contact", "bio" etc.
While they tip "contact, bio" I have to create a conditional to hide these pages, so the content would be displayed like this on the code:
if (('contact') || ('bio')) {
hide content
}
Typing just one page would be easy but with more pages, I don't know how to replace the comma to the OR conditional. How can I do it?
Try to use below code:
$skip_pages = explode(",","contact, bio");
$current_page = "contact";
if(in_array($current_page,$skip_pages)){
//redirect page to ....
}
//continue with page
You should first take user's input in one variable
Lets say you got it in $string
$string = "contact,bio,home";
$token = strtok($string, ",");
$i=0; // To know how manny keywords are there.
while ($token != false)
{
echo "$token<br>";
$user_ip[i++] = strtok(",");
}
and then you need to parse all pages name by , and store it in array.
lets say you stored it in $user_ip
Then you need to compare it like
if ( ($user_ip[0] === "contact") || ($user_ip[0] === "bio") {
}
and check above if for all members of $user_ip.

PHP - Display results from this 'Detect' array?

re: Home Site = http://mobiledetect.net/
re: this script = Mobile_Detect.php
Download script here: https://github.com/serbanghita/Mobile-Detect
This script functions perfectly detecting the different parameters of a user's device.
However, this is how I am currently detecting these parameters:
// each part of the IF statement is hard-coded = not the way to do this
if($detect->isiOS()){
$usingOS = 'iOS';
}
if($detect->isAndroidOS()){
$usingOS = 'Android';
}
echo 'Your OS is: '.$usingOS;
My goal is to use a FOREACH to iterate thru the various arrays in this script to determine a user's device's parameters. I would need to have the "($detect->isXXXXOS())" be dynamic... (which, would be based upon the KEY). The results would display the KEY. But the detection would be based upon the VALUE.
Also, since my web page uses a REQUIRE to access this script... in the Mobile_Script.php script, the arrays are "protected." I think this is also causing me problems (but I don't know for sure).
Any help is appreciated.
In foreach loop you can call dynamic method look like this :
$array = array('Android','Windows','Linux','Mac');
foreach( $array as $value) {
$method = "is{$value}OS";
if($detect->$method()) {
$os = $value;
echo "Your OS is : {$os}";
}
}
Please rearrange your code what you want. I give you an example.
you can try to use somethin like this:
$OSList = $detect->getOperatingSystems();// will give array of operating system name => match params
foreach($OSList as $os_name=>$os_params/*unused*/)
{
$method = 'is'.$os_name;
if($detect->$method())
{
$usingOS = $os_name;
}
}

need little about variable

I created a bit of code that just condition checks, like below
if ( $pretty_link_url==$_SERVER['HTTP_REFERER']) {
If this condition is true and working, then someone is using their browser with the same URL which is in "$pretty_link_url". "$pretty_link_url" is found in the database, and the if condition runs if they match. Now my question is
Is there anyway to get the result where both url from above $pretty_link_url==$_SERVER['HTTP_REFERER'] the same same as ...
$url = "result";
I don't know if it possible thankyou
I'm not sure what you really want but from your comment I think your looking for:
<?PHP
if ( $pretty_link_url == $_SERVER['HTTP_REFERER']) {
$url = $_SERVER['HTTP_REFERER'];
}else{
$url = ""; // empty or some other value you want to use instead
}
?>
I'm taking a guess at what exactly you're looking for, but is this it?:
$url = '';
if ( $pretty_link_url==$_SERVER['HTTP_REFERER']) {
$url = $_SERVER['HTTP_REFERER'];
}

str_replace() function and multiple variables?

This is going to be pretty hard to explain, so I'll try to make it as much of a chronological story as possible and end with the question, so that anyone who needs a relatively in-depth idea of what I'm talking about has one :).
I have a theme template with four text containers named primary_headline, primary_subline, secondary_headline and secondary_subtext. The content for each container comes from echo($container_name).
Each container has it's own variable, $container_name, which posts user created content to from a form, contents which sometimes contains the variables $city_name and $ref_name as raw data (i.e. "Hello, I'm from $city_name"), as $city_name and $ref_name has assigned values, for arguments sake let's just say $city_name= Dallas and $ref_name = Facebook.
Originally, I believed that I only wanted the user to be able to use $city_name and $ref_name within the secondary_subtext container, and I was also going to make the option to use $city_name OR $ref_name the choice of the user in the first place (they'd have to select one, or the other, or none - but couldn't select both.
However, I now want to allow $city_name AND $ref_name across all four container variables ($secondary_subtext for example).
So, my question is, how do I go about doing that in the easiest possible fashion?
Here's my old code for good measure:
if($geo_text == "enable") {
$geo_init = include("inc/geo_text.php");
$secondary_headline = str_replace('$city_name', $city_name, $_POST['secondary_headline']); // $_POST data from user
} else($ref_text == "enable") {
$ref_init = include("inc/ref_text.php");
$secondary_headline = str_replace('$ref_name', $ref_name, $_POST['secondary_headline']); // $_POST data from user
} else { $secondary_headline = $_POST['secondary_headline']; }
Any comments/answers etc will be very greatly appreciated :)!!
$placeholders = array('city_name','ref_name');
$include_mapper('geo_text' => 'inc/geo_text.php','ref_text'=>'inc/ref_text.php');
$containers = array('first_headline','scondary_headline');
$found = 0;
foreach ($include_mapper as $map => $include){
if ($$map == "enable"){
include ($include_mapper[$map]);
foreach ($container as $container){
foreach ($placeholders as $placeholder){
$$container = str_replace('$'.$placeholder, $$placeholder, $_POST[$container];
}
}
$found = 1;
break;
}
}
if (!$found){
foreach ($container as $container){
$$container = $_POST[$container];
}
}

Categories