views:

81

answers:

1

Hi

I have this argument in php and i want this to just allow users to enter a domain name...i want it so that it doesnt allow characters like "/" "?" and so on...so i only get the domain name such as "http://somedomain.co.nz" or "http://www.somedomain.co.nz"

if(!preg_match('/^(http):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i',$_POST['story_url']))

Can someone help me add a way i can stop "/" or "?" or anything else to stop from coming on to site...

Thanks

Roshan

+1  A: 

Its better to use the function parse_url for this.

if(parse_url(rtrim($POST['story_url'],'/'),PHP_URL_PATH) || 
             parse_url($POST['story_url'], PHP_URL_QUERY)) {
        // invalid...URL has a path or a query string.
} else {
        // valid
}
codaddict
Should check for PHP_URL_QUERY too to avoid http://www.url.com/?query=foo
domsterr
Hi thanks for a quick reply...so if i use this instead of preg match...would that make "www.somedomain.co.nz/" invalid? because i just want the preg_match to collect domain name part of it only and not the information after trailing slash or question mark or dynamic urls...
Roy
@domsterr: Thanks for pointing.
codaddict
@Roy: I've updated my answer. It now treats `www.somedomain.co.nz/` as valid. But if there is anything following the `/` its invalid. Also if there is any query string in the URL it'll be treated invalid.
codaddict
@codaddict I have the case opposite...if the parse_url is true...it will execute a set of statements in my code or "else" it will consider invalid...so how do i make it work opposite way...
Roy
codaddict
@codaddict Could you please explain more please...i am a newbie to php
Roy