Okay, I have a list of URLs in a MySQL table. I want the script to automatically check each link in the table for 404, and afterward I want it to store whether the URL was 404'd or not, as well as store a time for last checked.
Is this even possible to do automatically, even if no one runs the script? ie, no one visits the page for a few days, but even with no one visiting the page, it automatically ran the test.
If its possible, how could I go about making a button to do this?
You would use a cron job to do this. Using a cron job you pick when the script is run e.g. every hour, every 6 hours, etc...
To check 404 you can loop through the urls and use get_headers updating a status row each time.
Try using
curl
:If you are running on a shared hosting server, look for the possibility of setting up timed actions (cron jobs). Some hosting services have it, some don't.
I would recommend using
curl
as well, but makeHEAD
request instead ofGET
:Snipplet taken from here.
Recurring execution can be achieved by *nix cron command.
No need to use CURL,
file_get_contents($url);
will return false if the request fails (any other HTTP code other than 2xx), which might be more useful for what you're trying to do, an example:Will return true if the URL returns useful content, false otherwise.
EDIT: Here is a faster way (it only requests the headers) and the first byte instead of the whole page:
However, in combination with your other question it may be wiser to use something like this:
Or if you're using PHP 5.1+ you only have to do:
$hash
will be false when the URL fails to load, otherwise it will return the MD5 hash of the contents.Graciously stolen from @Jamie. =)
This way you only have to make one request instead of two. =)