show image based on presence of a URL - php

I would like to add an image in case a URL is present/active. Ive tried many things myself to make it work but without success.
What I want is: if url1 exist show image1 else nothing, if url2 exist show image2 else nothing, etc.
foreach ($urlsArray as $url) {
if(#fopen($url,'r')){
echo 'Exist';
}
else {
echo 'Doesnt exist';
}
}
Sorry I have to be more specific!
$urlArray = array(
'http://www.domain.com/page1.php' => 'images/image1.jpg',
'http://www.domain.com/page2.php' => 'images/image2.jpg',
etc);
foreach($urlsArray as $url){
if(#fopen($url,'r')){
echo '<img src="$image" />' /* if url1 exist show image1, etc */
}else{
echo '';
}
}
How to get this to work?

assuming that $url = path/to/pic/pic.gif - meaning that url actually leads to a pic to be displayed
foreach ($urlsArray as $url) {
if(#fopen($url,'r')){
echo '<img src="'.$url.'" />';
}
}
updated (use $key => $value for your foreach):
foreach ($urlsArray as $url => $img) {
if(#fopen($url,'r')){
echo '<img src="'.$img.'" />';
}
}

Maybe this will help ?:
<?php
foreach($urlsArray as $url){
if(!empty(file_get_contents($url))){
echo "exist";
}else{
echo "Doesn\'t exist";
}
}
?>

To the extent of my knowledge and past experience fopen for an external URL requires allow_url_fopen to be set to On. Normally it would be disabled as a security measure in deployment environments, suggest using some alternate method like cURL to check the URL is present or not with the http response code returned. Then proceed to show or not show the image based on the response code.
Sample code:
$url_exists_1 = check_url($url1);
if ($url_exists_1) {
// show image 1;
}
else {
// don't;
}
function check_url($url='') {
if (!function_exists('curl_init')) {
die("Please enable curl");
}
$curl_options_array = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_MAXREDIRS => 5
);
if ($url != '') {
$ch = curl_init();
curl_setopt_array($ch, $curl_options_array);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code == 200) {
return TRUE;
}
else {
return FALSE;
}
}
else {
return FALSE;
}
}

Related

checking header response in PHP for multiple URLs

I am trying to check the header response of multiple URLs at the same time without having a complicated php block.
<?php
$url = array("http://www.simplysup.co.uk/download/dl/trjsetup692.exe");
$headers = get_headers($url);
$response = substr($headers[0], 9, 3);
if ($response != "404") {
echo "PASS";
} else {
echo "FAIL";
}
?>
The above code checks for a single URL at a time. How to perform the same for multiple URLs at the same time? I will also need to trigger an email with the URL when the Header response is 404. Any help would be much appreciated.
I think this could solve your problem.
$fail = false;
$urls = array("http://www.simplysup.co.uk/download/dl/trjsetup692.exe");
foreach ($urls as $url) {
$headers = get_headers($url);
$response = substr($headers[0], 9, 3);
if ($response === "404") {
$fail = true;
}
}
if ($fail) {
echo "FAIL";
} else {
echo "PASS";
}

Check if file exist in multiple domains

I need to check if a file exist in multiple domains/servers and then show the download link to the user or write an error message. I have this script working for 1 domain:
<?php
$domain0='www.example.com';
$file=$_GET['file']
$resourceUrl = 'http://$domain0/$file';
$resourceExists = false;
$ch = curl_init($resourceUrl);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
//200 = OK
if ($statusCode == '200') {
$resourceExists = true;
}
if ($resourceExists == true) {
echo "Exist! $file";
} else {
echo "$file doesnt exist!";
}
?>
Now I need to check if that file exist in 4 domains, how can I do this? I don't know how to use arrays, so maybe if someone explain me ho to do this, I'll be very grateful.
I would create an array for domains
I would loop through the array with "foreach"
I would call a function to get the result
function checkFileOnDomain($file,$domain) {
$resourceUrl = "http://$domain/$file";
$ch = curl_init($resourceUrl);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($statusCode == '200')
return true;
}
$file=$_GET["file"];
// $_GET should be sanitized!
$domain_list=array("www.test1.com","www.test2.com");
foreach ($domain_list as $domain) {
echo "Check DOMAIN: $domain <hr/>";
if (checkFileOnDomain($file,$domain)) {
echo ">> [ $file ] EXISTS";
} else {
echo ">> [ $file ] DOES NOT EXIST";
}
echo "<br/><br/>";
} unset($domain);
EDIT:
To apply your specifications, you need an extra variable before foreach.
$link_to_file="";
foreach ($domain_list as $domain) {
if (checkFileOnDomain($file,$domain)) {
$link_to_file="$domain/$file";
break; // get first result and quit
}
} unset($domain);
if (!empty($link_to_file)) {
echo $link_to_file; //file is here
} else {
echo "404";
}
An array should solve the problem for you. This creates an array of the domains you want to check, and then cycles through them one by one running the code you wrote.
If you're struggling with arrays, have a look here for some more information
<?php
// Create an array of domains
$domains = ['www.example.com', 'www.example2.com', ...];
// Cycle through all the domains and run the code
foreach($domains as $domain) {
$domain0='www.example.com';
$file=$_GET['file']
$resourceUrl = 'http://$domain0/$file';
$resourceExists = false;
$ch = curl_init($resourceUrl);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
//200 = OK
if($statusCode == '200') {
$resourceExists = true;
} else if($resourceExists == false) {
}
if ($resourceExists == true) {
echo "Exist! $file";
} else {
echo "$file doesnt exist!";
}
}
?>

how to use elseif statement in dom php html find

How to add statement, when I search and it doesnt exist on the url, it will show nothing.html?
$url1 = "http://www.pengadaan.net/tend_src_cont2.php?src_nm=";
$url2 = $_GET['src_nm']."&src_prop=";
$url3 = $_GET['src_prop'];
$url = $url1.$url2.$url3;
$html = file_get_html($url);
if (method_exists($html,"find")) {
echo "<ul>";
foreach($html->find('div[class=pengadaan-item] h1[] a[]') as $element ) {
echo ("<li>".$element."</li>");
}
echo "</ul>";
echo $url;
}
else {
}
There are two ways to move to another page in PHP. you can do header("Location: http://www.yourwebsite.com/nothing.php"); or you can have PHP echo JavaScript to do a reidrect (if you already defined your headers):
if (method_exists($html,"find")) { // If 'find exist'
...
} else { // Otherwise it does not exist
header("Location: http://www.pengadaan.net/nothing.php"); // redirect here
}
Or if you already sent you headers you can get around it using JavaScript:
...
} else {
echo '<script>window.location.replace("http://www.pengadaan.net/nothing.php")</script>';
}

Php backlink checker on another website

I am trying to make a script to check if a webpage has a back link to my page. I have found this script but the problem is that it returns the error message "No back link found" even if there is a backlink. Could someone tell me what is wrong with this script?
Here is the script I am using:
require('simple_html_dom.php');
function CheckReciprocal( $targetUrl, $checkLinkUrl, $checkNofollow = true )
{
$html = file_get_html($targetUrl);
if (empty($html))
{
//# Could not load file
return false;
}
$link = $html->find('a[href^='.$checkLinkUrl.']',0);
if (empty($link))
{
//# Link not found
return false;
}
if ( $checkNofollow && $link->hasAttribute('rel') )
{
$attr = $link->getAttribute('rel');
return (preg_match("/\bnofollow\b/is", $attr) ? false : true);
}
return true;
}
$targetUrl = 'http://example.com/test.html';
$checkLinkUrl = 'http://mysite.com';
if ( CheckReciprocal($test, $checkLinkUrl) )
{
echo 'Link found';
}
else { echo 'Link not found or marked as nofollow'; }
Thank you!
I don't know how does that simple_html_dom.php's $html->find() works cos never used it, but it seems that your problem is there. I would trust the good ol' DOMDocument + regex.
Just wrote a function and tested it, just use on the $url the plain domain + whatever you want, don't worry about the http(s) or www and stuff like that:
function checkBackLink($link, $url, $checkNoFollow = true){
$dom = new DOMDocument();
$dom->loadHTMLFile($link);
foreach($dom->getElementsByTagName('a') as $item){
if($checkNoFollow){
if(preg_match('/nofollow/is', $item->getAttribute('rel'))) continue;
}
if($item->hasAttribute('href') === false) continue;
if(preg_match("#^(https?\://)?(www\.)?$url.*#i", $item->getAttribute('href'))) return true;
}
}
if(checkBacklink('the link', 'example.com')){
echo "link found";
} else {
echo "Link not found or marked as nofollow";
}
If you don't like it and still want to use the simple_html_dom just make sure how that find() works, because if it only match exact values that could be troublesome.

Getting header response code

This is part of a PHP script I am putting together. Basically a domain ($domain1) is defined in a form and a different message is displayed, based on the response code from the server. However, I am having issues getting it to work. The 3 digit response code is all I am interested in.
Here is what I have so far:
function get_http_response_code($domain1) {
$headers = get_headers($domain1);
return substr($headers[0], 9, 3);
foreach ($get_http_response_code as $gethead) {
if ($gethead == 200) {
echo "OKAY!";
} else {
echo "Nokay!";
}
}
}
$domain1 = 'http://google.com';
function get_http_response_code($domain1) {
$headers = get_headers($domain1);
return substr($headers[0], 9, 3);
}
$get_http_response_code = get_http_response_code($domain1);
if ( $get_http_response_code == 200 ) {
echo "OKAY!";
} else {
echo "Nokay!";
}
If you have PHP 5.4.0+ you can use the http_response_code() function. Example:
var_dump(http_response_code()); // int(200)
Here is my solution for people who need send email when server down:
$url = 'http://www.example.com';
while(true) {
$strHeader = get_headers($url)[0];
$statusCode = substr($strHeader, 9, 3 );
if($statusCode != 200 ) {
echo 'Server down.';
// Send email
}
else {
echo 'oK';
}
sleep(30);
}
You directly returned so function wont execute further foreach condition which you written. Its always better to maintain two functions.
function get_http_response_code($domain1) {
$headers = get_headers($domain1);
return substr($headers[0], 9, 3); //**Here you should not return**
foreach ($get_http_response_code as $gethead) {
if ($gethead == 200) {
echo "OKAY!";
} else {
echo "Nokay!";
}
}
}

Categories