Solved

check urls are alive with javascript and romote php

Posted on 2004-10-13
3
718 Views
Last Modified: 2012-06-27
hi there

I have a bit of code here that checks urls if they are active

<?php

$urls = @file('urlstocheck.txt');
if (!is_array($urls)) {
    die ("No input found");
}

if (!$of = fopen('validurls.txt', 'w+')) {
    die ("Can't create output");
}

foreach ($urls as $url) {
    if ($fp = @fopen(trim($url), 'r')) {
        fclose($fp);
        fputs($of, trim($url) . "\n");
    }
}

fclose($of);

would it be possible to alter that php script with a javascript part in html to connect to the php script or feed it a url then from 2 choices active and dead select a graphic for each

so

javascript with url ----> connects to activatetest.php returns yes or no graphic which reside on the server

regards

0
Comment
Question by:playstat
  • 2
3 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 12305815
Well you could always just use:

<img src="http://www.someremoteserver/someremoteimage.jpg" onerror="this.src='list_as_broken.php?' + this.src;" />

If the remote image does not exist the list_as_broken.php is called:

<?php
$brokenlink = $_SERVER['QUERY_STRING'];
/*do something with this info*/
..
..
/*output broken link image*/
header('Content-type:image/png');
$f = fopen('broken.png');
fpassthru($f);
fclose($f);
?>

Regards -r-
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12306582
Heres my take on it, this will only check 1 url at the moment, i can modify it to handle several if needed. Instead of pointing it to your current script i have just incorperated the relevant parts.

<?php
$active_img = "active.gif";
$dead_img = "broken.gif";

if (!empty($_POST['url'])) {

 if (!$of = fopen('validurls.txt', 'a')) die ("Can't create output");
 
 if ($fp = @fopen(trim($_POST['url']),'r')) {
  fclose($fp);
  fputs($of,trim($_POST['url'])."\n");
  $image = $active_img;
 }
 else {
  $image = $dead_img;
 }
      
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="url" value="<?php if (!empty($_POST['url'])) echo $_POST['url']; ?>">
<input type="submit" name="submit" value="Check URL">
</form>

<?php if (isset($image)) echo 'Status: <img src="'.$image.'">'; ?>
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
ID: 12306636
heres a "quick and dirty" modification to check up to 5 url's at a time

<?php
$active_img = "active.gif";
$dead_img = "broken.gif";

if (!empty($_POST['url'])) {

 if (!$of = fopen('validurls.txt', 'a')) die ("Can't create output");
 
 foreach ($_POST['url'] as $link) {
  if ($fp = @fopen(trim($link),'r')) {
   fclose($fp);
   fputs($of,trim($link)."\n");
   $image[] = $active_img;
  }
  else {
   $image[] = $dead_img;
  }
 }
 
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="url[]" value="<?php if (!empty($_POST['url'][0])) echo $_POST['url'][0]; ?>"><?php if (isset($image)) echo 'Status: <img src="'.$image[0].'">'; ?><br>
<input type="text" name="url[]" value="<?php if (!empty($_POST['url'][1])) echo $_POST['url'][1]; ?>"><?php if (isset($image)) echo 'Status: <img src="'.$image[1].'">'; ?><br>
<input type="text" name="url[]" value="<?php if (!empty($_POST['url'][2])) echo $_POST['url'][2]; ?>"><?php if (isset($image)) echo 'Status: <img src="'.$image[2].'">'; ?><br>
<input type="text" name="url[]" value="<?php if (!empty($_POST['url'][3])) echo $_POST['url'][3]; ?>"><?php if (isset($image)) echo 'Status: <img src="'.$image[3].'">'; ?><br>
<input type="text" name="url[]" value="<?php if (!empty($_POST['url'][4])) echo $_POST['url'][4]; ?>"><?php if (isset($image)) echo 'Status: <img src="'.$image[4].'">'; ?><br>
<input type="submit" name="submit" value="Check URL">
</form>
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question