Jump to content

Recommended Posts

 

That was written 7 years ago; the code looks pretty deprecated.

 

Anyway, what kind of validation do you mean?  Actually checking if it's online, or making sure it's just a valid URL.

Link to comment
https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-705498
Share on other sites

I would just send a HEAD header...

 

-Open a connection to the remote server

HEAD /path/to/file.ext HTTP/1.1

Host: <host>

Connection: close

 

Then see if it returns a 200 status.  If it doesn't, something could be wrong (301 could be fine for example, but 404 is obviously not valid.)  If a connection couldn't be opened, it's obviously not a valid link.

Link to comment
https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-705520
Share on other sites

I need to check if site exist or if link is broken or not.

Example

www.site.com/images/image.jpeg

I dont mean it is valid or not. I mean is it broken or not. The point is. If the link is ok if I clikc with mouse on it, it will open a picture file in my browser. If its broken (so the picture didnt exist on the server) i will receive in my browser error message. So I need some php code who will check if link is ok (file exist on the server and everithing is cool) or its broken :D

 

;D

Please I must to make some solution on this one. I find a free source scripts on the web but they check a whole site for a broken or valid links. I need something simple, short, fast and useful script for checking broking links.

Thank you.  ;)

Link to comment
https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706029
Share on other sites

I actually had to do this recently and the easiest way to check if an image exists remotely, I found, is the command

 

getimagesize

http://us3.php.net/getimagesize

 

An example would be:

 

//This will get the image size of the image, if its not here it returns an error.  the @ symbol will suppress the error.
if (@GetImageSize(www.site.com/images/image.jpeg))
{
  //do stuff here if it can get the image size of the image. 
}
else
{
  //do stuff here if its not there.
}

 

That will work locally as well if you use local paths as well.

Link to comment
https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706143
Share on other sites

I actually had to do this recently and the easiest way to check if an image exists remotely, I found, is the command

 

getimagesize

http://us3.php.net/getimagesize

 

An example would be:

 

//This will get the image size of the image, if its not here it returns an error.  the @ symbol will suppress the error.
if (@GetImageSize(www.site.com/images/image.jpeg))
{
  //do stuff here if it can get the image size of the image. 
}
else
{
  //do stuff here if its not there.
}

 

That will work locally as well if you use local paths as well.

 

This look to me like a good idea and I try it. But its really slow if a link is not correct.

Link to comment
https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706144
Share on other sites

Try using get_headers.  Do as corbin suggested and check the status.

 

<?php
$url = 'http://www.example.com';
$headers = get_headers($url);
if ($headers[0] != 'HTTP/1.1 200 OK') {
    //link is bad
}
?>

 

Note that status 301 is a permanent redirect and the link may still work.

Link to comment
https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706155
Share on other sites

Your dilemma interested me, so I wrote a class to test urls.  Maybe you or someone else could find it useful.

 

It works like so:

<?php
$url = new UrlInfo('http://www.google.com/intl/en_ALL/images/logo.gif');
echo $url->getStatus();
print_r($url->getHeaders());
echo $url->getHeader('Content-Length');
?>

 

prints something like this:

 

200
Array
(
    [0] => HTTP/1.0 200 OK
    [Content-Type] => image/gif
    [Last-Modified] => Wed, 07 Jun 2006 19:38:24 GMT
    [Expires] => Sun, 17 Jan 2038 19:14:07 GMT
    [Cache-Control] => public
    [Date] => Thu, 04 Dec 2008 22:49:50 GMT
    [server] => gws
    [Content-Length] => 8558
    [Connection] => Close
)
8558

 

So to use it to test links:

 

<?php
$url = 'http://www.example.com/images/image.jpg';
$url_info = new UrlInfo($url);
if ($url_info->getStatus() == 200) {
    echo $url . ' returned status: 200';
}
?>

 

and here is the class:

 

<?php
class UrlInfo {

private $url;
private $headers;
private $status;

// fetch headers for url
public function __construct($url = '') 
{
	$this->url = $url;

	if ($this->url) 
	{
		$this->headers = @get_headers($this->url, 1);
		if (!$this->headers)
		{
			$this->headers = array();
		}
		else
		{
			$this->getStatus();
		}
	}
}

public function getUrl() 
{
	return $this->url;
}

public function getHeaders() 
{
	return $this->headers;
}

public function getHeader($key) 
{
	if (isset($this->headers[$key]))
	{
		return $this->headers[$key];
	}
	else
	{
		return '';
	}
}

public function getStatus()
{
	if (!$this->status)
	{
		$status = trim(end(explode(' ', $this->headers[0], 2)));
		switch ($status)
		{
			case '200 OK':
				$this->status = 200;
				break;
			case '301 Moved permanently':
				$this->status = 301;
				break;
			case '302 Found':
				$this->status = 302;
				break;
			case '303 See Other':
				$this->status = 303;
				break;
			case '403 Forbidden':
				$this->status = 403;
				break;
			case '404 Not Found':
			default: 
				$this->status = 404;
		}
	}
	return $this->status;
}

}
?>

Link to comment
https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706302
Share on other sites

hmmm I receive error 403 Forbidden from a valid site address.

From my local server is ok. But when I upload to a web host server i receive error.

Any solution?

 

Most likely it realizes you are hotlinking to that image and the does not allow hotlinking.

Link to comment
https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706835
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.