Solved

check urls are alive with javascript and romote php

Posted on 2004-10-13
3
722 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

688 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