How to check a URL with a lot of requests

I have an automatic site, and I have a SAVE SEARCH function that saves the search that the user has done on the site for future use. What I do is that I store the entire URL in the database.

But I need to check it as a valid URL. It looks like this.

http://www.anywebsite.com/cars.php?postcode=CV2+5AS&distance=&make=&min_price=&max_price=&category_type=&body_type=&fuel=&colour=&transmission=&year_of_registration=&mileage=&engine=&doors=&seller=&keywords=&sort=PRICE_LOWEST&referer_url=http%253A%252F%252Flocalhost%252Fselling%252Fcars.php&trader_id=0&case=ADVANCE 

Can anyone provide me with any idea on how I can achieve this?

I have a preg_match that is here.

 if (!preg_match('/^https?:(\/\/)?(www\.)?([a-zA-Z0-9_%]*)\b\.[az]{2,4}(\.[az]{2})?/', $fields[$field_name])) 

But it only checks URLs like http://www.anywebsite.com , while I need to check all the above URLs.

Any help would be greatly appreciated.

+2
source share
3 answers

There should be no reason why you would need to manually copy the URL check, this problem has been resolved.

Take a look at php filter_var() .

Example URL validation: http://www.w3schools.com/php/filter_validate_url.asp

 $url = "http://www.anywebsite.com/cars.php?postcode=CV2+5AS&distance=&make=&min_price=&max_price=&category_type=&body_type=&fuel=&colour=&transmission=&year_of_registration=&mileage=&engine=&doors=&seller=&keywords=&sort=PRICE_LOWEST&referer_url=http%253A%252F%252Flocalhost%252Fselling%252Fcars.php&trader_id=0&case=ADVANCE"; if (!filter_var($url, FILTER_VALIDATE_URL)) { echo "URL is not valid"; } else { echo "URL is valid"; } 
+3
source

why reinvent the wheel when you can do filter_var() ? then use the FILTER_VALIDATE_URL filter_var filter for PHP 5> = 5.2.0. You can also add FILTER_FLAG_HOST_REQUIRED to have the required domain name, which you think you need, and you can check other flags to see if they are needed.

Example:

 $url = 'http://www.anywebsite.com/cars.php?postcode=CV2+5AS&distance=&make=&min_price=&max_price=&category_type=&body_type=&fuel=&colour=&transmission=&year_of_registration=&mileage=&engine=&doors=&seller=&keywords=&sort=PRICE_LOWEST&referer_url=http%253A%252F%252Flocalhost%252Fselling%252Fcars.php&trader_id=0&case=ADVANCE'; if(filter_var($url, FILTER_VALIDATE_URL)){ #do stuff here }else{ #invalid } 

Working example: http://ideone.com/N84VGF

If you have an older version of PHP installed, you can use parse_url() , but you need to add a regular expression / validation, because it doesn't check if it is a valid / valid URL.

+2
source

All Articles