verifying a URL submitted [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

verifying a URL submitted [solved]

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: verifying a URL submitted

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: verifying a URL submitted

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply