Validating a URL

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I'm validating a URL passed to my script VIA GET method (means the user has full access to it and could change it as s/he pleases). Here is what I'm doing.
PHP Code: [ Select ]
// Setting the URL variable
$previous = $_GET['previous'];
 
// Checking if the URL is a valid URL
if(preg_match(REGEX_URL, $previous))
{
    // The URL is valid.
    $location = $previous;
}
  1. // Setting the URL variable
  2. $previous = $_GET['previous'];
  3.  
  4. // Checking if the URL is a valid URL
  5. if(preg_match(REGEX_URL, $previous))
  6. {
  7.     // The URL is valid.
  8.     $location = $previous;
  9. }

And my REGEX is (Which I found on-line... being trying to learn REGEX but they're harder to learn then I thought :lol: )
Code: [ Select ]
_^(?:(?:https?)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS

My question is: is what I'm doing enough to be able to then redirect the user to that page VIA headers()?

I'm also not very happy with that REGEX but I can't find one that I would be satisfied with.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Ok... the regex there does not give me what I want... it forces me to have an http...

The problem with that is that I need the following to be validated true on regex.

http://www.site.com
https://www.site.com
http://site.com
https://site.com
http://domain.site.com
https://domain.site.com
http://www.domain.site.com
https://www.domain.site.com
site.com
domain.site.com
http://www.site.com/path/to/dir/
https://www.site.com/path/to/dir/
http://site.com/path/to/dir/
https://site.com/path/to/dir/
http://domain.site.com/path/to/dir/
https://domain.site.com/path/to/dir/
http://www.domain.site.com/path/to/dir/
https://www.domain.domain.site.com/path/to/dir/
site.com/path/to/dir/
domain.site.com/path/to/dir/
http://www.site.com/path/to/file.html
https://www.site.com/path/to/file.html
http://site.com/path/to/file.html
https://site.com/path/to/file.html
http://domain.site.com/path/to/file.html
https://domain.site.com/path/to/file.html
http://www.domain.site.com/path/to/file.html
https://www.domain.domain.site.com/path/to/file.html
site.com/path/to/file.html
domain.site.com/path/to/file.html

And relative paths
./path/to/file.html
./path/to/dir/
./path/to/dir
path/to/file.html
path/to/dir/
path/to/dir

(ftp:// NOT permitted)

I guess I could possibly be able to use different REGEXs depending on the case but it would be really great if this could be done with one REGEX. With my knowledge of REGEX this would take me a few years to write a REGEX code for this. (It's taken me about 3 hours to create a REGEX code for a relative path yesterday, and even then not how I wanted it!!)

If I could get any help here, that would be great!
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Code: [ Select ]
((^(http[s]?:\/\/)?([w]{3}[.])?(([a-z0-9\.]+)+(com|php))(((\/[a-z0-9]+)*(\/[a-z0-9]+\/?))*([a-z0-9]+[.](html|php|gif|png|jpg))?)$)|((^([.]\/)?((([a-z0-9]+)\/?)+|(([a-z0-9]+)\/)+([a-z0-9]+[.](html|php|gif|png|jpg))))$))
Thanks to some very good people I got the following RegEx :D
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I currently (not) use the following REGEX:
Code: [ Select ]
^((https?:[/][/])?(\w+[.])+com([?]\w+=\w*([&]\w+=\w*)*)?|((https?:[/][/])?(\w+[.])+com([?]\w+=\w*([&]\w+=\w*)*)?[/]|[.][/])?\w+([/]\w+)*([/]|[.]html|[.]php|[.]gif|[.]jpg|[.]png)?)$

The only reason I'm not using it is because it throws me the following error:
Quote:
Warning: preg_match(): Unknown modifier ']' in C:\xampp\htdocs\path\to\file.php on line 26

The reason it's throwing that error is because it is written for .NET (C#).

I don't know how to fix it to make it work... I've tried numerous things but I just don't understand REGEX!!

I need this one to work though... any help in converting it into a working PHP version? Please? I've tried writing my own REGEX, tried fixing this one... spent hours, but I just can't wrap my head around it.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I got the following REGEX to work...
Code: [ Select ]
((^(http[s]?:\/\/)?([w]{3}[.])?(([a-z0-9\.]+)+(com|php))(((\/[a-z0-9]+)*(\/[a-z0-9]+\/?))*([a-z0-9]+[.](html|php|gif|png|jpg))?)(((\&|\?)\w+\=\w*)*)$)|((^([.]\/)?((([a-z0-9]+)\/?)+|(([a-z0-9]+)\/)+([a-z0-9]+[.](html|php|gif|png|jpg))))$))

The thing here though is I don't know if I did it right... it seems to me that there are two capturing groups (2 main alternatives) and I only put the query part into one alternative.

I also don't know if I put it in the right spot (though it works)... do I need it in the second alternative? If so, where?

Alternative 1
Code: [ Select ]
((^(http[s]?:\/\/)?([w]{3}[.])?(([a-z0-9\.]+)+(com|php))(((\/[a-z0-9]+)*(\/[a-z0-9]+\/?))*([a-z0-9]+[.](html|php|gif|png|jpg))?)(((\&|\?)\w+\=\w*)*)$)

Alternative 2
Code: [ Select ]
|((^([.]\/)?((([a-z0-9]+)\/?)+|(([a-z0-9]+)\/)+([a-z0-9]+[.](html|php|gif|png|jpg))))$))

The query string search pattern looks like: (at the end of first alternative... right before the dollar sign).
Code: [ Select ]
(((\&|\?)\w+\=\w*)*)

Thanks!
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Well, I did shove it into the second alternative... found out that the first alternative is for direct links (http://www.google.com) and second alternative is for relative links (./path/to/file.php).

Code: [ Select ]
((^(http[s]?:\/\/)?([w]{3}[.])?(([a-z0-9\.]+)+(com|php))(((\/[a-z0-9]+)*(\/[a-z0-9]+\/?))*([a-z0-9]+[.](html|php|gif|png|jpg))?)(((\&|\?)\w+\=\w*)*)$)|((^([.]\/)?((([a-z0-9]+)\/?)+|(([a-z0-9]+)\/)+([a-z0-9]+[.](html|php|gif|png|jpg))))(((\&|\?)\w+\=\w*)*)$))

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 184 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.