How to check if the user given link is a link belonging to a particular website or not? I am using php. Should I use preg or is there a function for it? Can i also check if the link is broken or not? Is there a function for it? How can i make sure to delete malacious links or is it not possible to handle it automatically? Should i us the following function?
$link=mysql_real_escape_string($_GET['link']);

- 1,391
- 1
- 16
- 41
2 Answers
How to check if the user given link is a link belonging to a particular website or not?
You could attempt to fetch the link using either file_get_contents()
, get_headers()
or curl
.
Should I use preg or is there a function for it?
Validating the contents of a URL can be done with filter_input()
:
$url = filter_input(INPUT_GET, 'link', FILTER_VALIDATE_URL);
// $url would be false if invalid or null if not available
Can i also check if the link is broken or not?
As said earlier, you would have to fetch the link and determine the status code.
Is there a function for it?
Yes, as stated earlier.
How can i make sure to delete malacious links or is it not possible to handle it automatically?
If the link turns out to be invalid, it's up to you to delete the respective entries in your database, or wherever you store them.
Should i us the following function?
$link=mysql_real_escape_string($_GET['link']);
That's used to prepare the value for database storage. It's also using the old mysql_
functions, you should switch to either mysqli or PDO and use prepared statements to take care of escaping altogether.

- 170,779
- 38
- 263
- 309
Always use get_headers, which helps you validate the state of the website, if its available or not.
$val = @get_headers("http://www.google.com");
print_r($val[0]);
use the following preg_match pattern to check the broken link.
%^((https?://)|(www.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i

- 1,126
- 1
- 7
- 17