?
Solved

check urls are alive with javascript and romote php

Posted on 2004-10-13
3
Medium Priority
?
726 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

752 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