Page 1 of 1

verifying a URL submitted [solved]

Posted: Fri Nov 11, 2011 11:38 pm
by s.dot
I want to verify a URL submitted by users to make sure they didn't link to a missing page or have a typo in the URL.
I can use get_headers() for this.
The problem is redirections will send multiple headers. Will the 200 OK response always be the last status header sent in the output from get_headers() ? Also, will the status always be 200 OK or will there be other successful header types?

Re: verifying a URL submitted

Posted: Fri Nov 11, 2011 11:53 pm
by s.dot
Would the following work?

Code: Select all

function header200($headers)
{
	foreach ($headers AS $header)
	{
		if (stripos($header, '200 ok') !== false)
		{
			return true;
		}
	}
	
	return false;
}

var_dump(header200('url'));
It appears to work in a few examples I've thrown at it.

Re: verifying a URL submitted

Posted: Sat Nov 12, 2011 12:43 am
by s.dot
Nevermind, solved it myself. :-D

1) Verify http:// or https:// with preg_match or stripos
2) Verify parse_url can parse it
3) Verify it sends headers
4) Verify the header is 200 ok using the function above.

Sorry guys.