Solved

check urls are alive with javascript and romote php

Posted on 2004-10-13
3
714 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now