Solved

check urls are alive with javascript and romote php

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.

759 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

19 Experts available now in Live!

Get 1:1 Help Now