David Nelson Posted April 20, 2008 Share Posted April 20, 2008 Hey, On my site there are a lot of php-generated links, and sometimes they are broken. I've seen it before, but I was wondering if someone could point me in the right direction of a script that you could define a link to and have it check to see if the page exists/resolves, then return a result either "Working" or "Down" to the user. Thanks everyone. Quote Link to comment Share on other sites More sharing options...
monkeypaw201 Posted April 20, 2008 Share Posted April 20, 2008 Well, you could go with the fopen() and if it returns a result it means the page is there.. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 20, 2008 Share Posted April 20, 2008 You could use cURL and check the HTTP response code: <?php $url = 'http://www.google.co.uk'; $ch = curl_init($url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); curl_exec($ch); echo curl_getinfo($ch,CURLINFO_HTTP_CODE); ?> http://en.wikipedia.org/wiki/List_of_HTTP_status_codes Quote Link to comment Share on other sites More sharing options...
David Nelson Posted April 20, 2008 Author Share Posted April 20, 2008 Okay Sweet! How would I return a result? I'm real new to PHP so I don't know how to take that code and make it return "Working" or "Down" based off of what it finds. Thanks for all your help guys. This forum rules! Quote Link to comment Share on other sites More sharing options...
monkeypaw201 Posted April 20, 2008 Share Posted April 20, 2008 http://us.php.net/fopen Read up on it, post the code, and we'll fix it Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 20, 2008 Share Posted April 20, 2008 With my method: <?php function checkurl($url){ $ch = curl_init($url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); curl_setopt($ch,CURLOPT_FAILONERROR,TRUE); if(curl_exec($ch) !== FALSE){ return 'URL ok'; }else{ return 'Could not connect to site'; } } echo checkurl('http://www.google.co.uk'); echo '<br />'; echo checkurl('http://www.madeupurl.com'); ?> Quote Link to comment 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.