qbox Posted December 1, 2008 Share Posted December 1, 2008 Any way to check if link is valid or not with php? Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/ Share on other sites More sharing options...
The Little Guy Posted December 1, 2008 Share Posted December 1, 2008 what type of link? http://somesite.com/page.html or ../page/page.html or /page/page.html or all of the above? Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-703449 Share on other sites More sharing options...
qbox Posted December 1, 2008 Author Share Posted December 1, 2008 http://www.thsite.com/images/image.jpeg if you can tell me to check is site exist http://www.thesite.com Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-703472 Share on other sites More sharing options...
qbox Posted December 3, 2008 Author Share Posted December 3, 2008 I need to check is link valid www.sitename.com.images.image.jpg or site exist www.sitename.com Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-705479 Share on other sites More sharing options...
flyhoney Posted December 3, 2008 Share Posted December 3, 2008 This might help: http://www.zend.com/zend/spotlight/php-link-validation1.php Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-705491 Share on other sites More sharing options...
DarkWater Posted December 3, 2008 Share Posted December 3, 2008 This might help: http://www.zend.com/zend/spotlight/php-link-validation1.php 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. Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-705498 Share on other sites More sharing options...
corbin Posted December 3, 2008 Share Posted December 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-705520 Share on other sites More sharing options...
qbox Posted December 4, 2008 Author Share Posted December 4, 2008 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 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. Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706029 Share on other sites More sharing options...
flyhoney Posted December 4, 2008 Share Posted December 4, 2008 Corbin, that is an interesting idea. Any idea how to actually implement that? Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706138 Share on other sites More sharing options...
flyhoney Posted December 4, 2008 Share Posted December 4, 2008 http://us3.php.net/get_headers I think that may be the answer. Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706142 Share on other sites More sharing options...
bgadberry Posted December 4, 2008 Share Posted December 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706143 Share on other sites More sharing options...
qbox Posted December 4, 2008 Author Share Posted December 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706144 Share on other sites More sharing options...
flyhoney Posted December 4, 2008 Share Posted December 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706155 Share on other sites More sharing options...
qbox Posted December 4, 2008 Author Share Posted December 4, 2008 Its 50% faster. With image check it need around 6 seconds to make the link check. With headers it need only 3 seconds. I will probably use this method (with headers check) Thanks to all for the help. Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706298 Share on other sites More sharing options...
flyhoney Posted December 4, 2008 Share Posted December 4, 2008 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706302 Share on other sites More sharing options...
qbox Posted December 5, 2008 Author Share Posted December 5, 2008 flyhoney this script helped me a lot. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706714 Share on other sites More sharing options...
qbox Posted December 5, 2008 Author Share Posted December 5, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706833 Share on other sites More sharing options...
premiso Posted December 5, 2008 Share Posted December 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-706835 Share on other sites More sharing options...
qbox Posted December 5, 2008 Author Share Posted December 5, 2008 Or the server block all http requesting Quote Link to comment https://forums.phpfreaks.com/topic/135055-solved-link-check/#findComment-707104 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.