Tell if a URL is active or NOT

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Tell if a URL is active or NOT

Post by psychotomus »

how can I tell if a URL is active or not?

like devnetwork.net is active

asdlkfjasdkljk343kjadsf.com is not Active
deadoralive
Forum Commoner
Posts: 28
Joined: Tue Nov 06, 2007 1:24 pm

Post by deadoralive »

Could you use curl to try and fetch a page from the url in question?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

cURL is probably your best bet, since you can control the timeout incase there is some latency. A simple alternative to cURL (since it can be a lot to swallow for beginers -- although I do suggest you try cURL first)

Code: Select all

if (@file_get_contents('http://domain.com')) {
   echo 'i can connect!';
} else {
   echo 'i cannot connect!';
}
fsockopen() and similar functions can also accomplish this quite easily as well.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

doesn't seem like file_get_contents is working... =(

http://po2upload.com/url_checker password = blind

Code: Select all

<?
session_start();
include 'config.php';

if(!isset($_SESSION['log']))
{
	if(isset($_POST['Submit']))
	{
		if($_POST['textPass'] == $pass)
		{
			$_SESSION['log'] = true;
		}
		else
		{
			echo 'Incorrect Password.';
		}
	}
?>
<form name="form1" method="post" action="">
  <input name="textPass" type="text" id="textPass">
  <br>
  <input type="submit" name="Submit" value="Login">
</form>
<?
}

if(isset($_SESSION['log']))
{
?>
<a href="index.php?action=add_link">Add Link</a> : <a href="index.php?action=check_links">Check Links</a> 
<?
	if($_GET['action'] == "add_link")
	{
		if(isset($_POST['Submit2']))
		{
			$title = $_POST['textTitle'];
			$url = $_POST['textURL'];
			mysql_query("INSERT INTO urls (url, title) VALUES ('$url', '$title')") or die(mysql_error());
			echo 'Title and URL added to database';
		}
?>
<form name="form2" method="post" action="">
  <table width="100%"  border="0">
    <tr>
      <td>Title:</td>
      <td><input type="text" name="textTitle"></td>
    </tr>
    <tr>
      <td>URL:</td>
      <td><input type="text" name="textURL"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit2" value="Add Link"></td>
    </tr>
  </table>
</form>
<?
	}
	elseif ($_GET['action'] == "check_links")
	{
		if(isset($_POST['Remove']))
		{
			//loop threw all selected items
			for($x =0; $x < count($_POST['hidden']);$x++)
			{
				if (isset($_POST['checkbox'][$x]))
				{
					//update cheat info
					mysql_query("DELETE FROM urls WHERE id='" . $_POST["hidden"][$x] . "'") or die(mysql_error());
				}
			}
			echo '<p>Title(s) And URL(s) Removed.</p>';
		}
?>
<form name="form3" method="post" action="">
<?
$result = mysql_query("SELECT * FROM urls");
while($urls = mysql_fetch_object($result))
{
	if (!(@file_get_contents($urls->url))) 
	{ 
?>

  <table width="100%"  border="0">
	<tr>
	  <td><?= $urls->title ?></td>
	</tr>
	<tr>
	  <td><div align="right">
	   <input name="hidden[<?= $counter ?>]" type="hidden" id="hidden[<?= $counter ?>]" value="<?= $urls->id ?>">
	   <input type="checkbox" name="checkbox[<?= $counter ?>]" value="remove">
	  </div></td>
	</tr>
  </table>

<?
	$counter = $counter +1;
	}
}
?>
<input type="submit" name="Remove" value="Remove">
</form>
<?
	}
}
?>
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

fsockopen() sounds like a good solution for that.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Thinking about it, indeed you should use fsockopen since you don't actually want to retrieve the contents of the file and simply make a connection.

Try

Code: Select all

$fp = @fsockopen("domain.com", 80, $errno, $errstr, 30);
if (!$fp) {
   echo 'not connected';
} else {
	echo 'connected';
   fclose($fp);
}
Notice the 5th parameter is the timeout (in seconds), so you may want to adjust this accordingly.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

Still doesn't seem to work....

its showing all links as not being active.
even http://www.google.com

Code: Select all

<?
$result = mysql_query("SELECT * FROM urls");
while($urls = mysql_fetch_object($result))
{
	$fp = @fsockopen($urls->url,80, $errno, $errstr, 60);
	if (!$fp) 
	{ 
?>

  <table width="100%"  border="0">
	<tr>
	  <td><?= $urls->title ?></td>
	</tr>
	<tr>
	  <td><div align="right">
	   <input name="hidden[<?= $counter ?>]" type="hidden" id="hidden[<?= $counter ?>]" value="<?= $urls->id ?>">
	   <input type="checkbox" name="checkbox[<?= $counter ?>]" value="keep">
	  </div></td>
	</tr>
  </table>

<?
	$counter = $counter +1;
	}
	else
	{
		fclose($fp);
	}
}
?>
<input type="submit" name="Remove" value="Remove">
</form>
<?
	}
}
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Please add

Code: Select all

var_dump($urls->url);
above the fsockopen() call and post the output
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

do you have allow_url_fopen on?

Code: Select all

var_dump($a = ini_get('allow_url_fopen'));
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

string(37) "http://video.stage6.com/1893852/.divx"
working test

string(22) "www.comerzzzzzzzzz.com"
faled test

string(36) "www.penissszzzzzzzzRRrrrrrrrrrrr.org"
aaa

string(37) "http://video.stage6.com/1893852/.divx"
asdfasdfa

string(22) "http://www.teampo2.com"
kkk

string(21) "http://www.google.com"
Google
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

scottayy wrote:do you have allow_url_fopen on?

Code: Select all

var_dump($a = ini_get('allow_url_fopen'));


string(1) "1"


i guess its on?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Yes, it is.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

That's interesting. I have no idea, actually.

Perhaps this can help you: http://us3.php.net/manual/en/function.f ... .php#75388 (it's doing the same thing with http and https protocols)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Try replacing your while loop with this following, this should shed light to why the connections are failing.

Code: Select all

<?
$result = mysql_query("SELECT * FROM urls");
while($urls = mysql_fetch_object($result))
{
	$fp = @fsockopen($urls->url, 80, $errno, $errstr, 30);
	if (!$fp) {
		echo 'Cannot connect to "'. $urls->url .'. Reason: '. $errstr .'" <br />';
	} else {
		echo 'Connected to "'. $urls->url .'" <br />';
		fclose($fp);
	}
}
?>
[/quote]
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

Cannot connect to "http://video.stage6.com/1893852/.divx. Reason: Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?"
Cannot connect to "www.comerzzzzzzzzz.com. Reason: "
Cannot connect to "www.penissszzzzzzzzRRrrrrrrrrrrr.org. Reason: "
Cannot connect to "http://video.stage6.com/1893852/.divx. Reason: Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?"
Cannot connect to "http://www.teampo2.com. Reason: Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?"
Cannot connect to "http://www.google.com. Reason: Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?"
Post Reply