Enjoy an ad free experience by logging in. Not a member yet?
Register .
01-27-2012, 01:26 PM
PM User |
#1
Senior Coder
Join Date: May 2006
Posts: 1,526
Thanks: 26
Thanked 4 Times in 4 Posts
Checking for http:// in urls ?
Hi
I have used the this code to ensure that I get a vaild
url ( or I use "none" value )
PHP Code:
if(! filter_var ( $Db_return , FILTER_VALIDATE_URL )) {
$Db_return = "none" ;
} // end if
Now I just want to make sure that there is an
http://
in place so that the re-direct will work.
PHP Code:
if( $return == "none" ){
$return_path = "thankyou.php" ;
} // endif
else {
if( substr ( $return , 0 , 7 ) != "http://" || substr ( $return , 0 , 8 ) != "https://" ) {
$return = "http://" . $return ;
} // endif
else {
$return_path = $return ;
} // end else
} // end else
header ( "Location: $return_path" );
Is this a good way to deal with checking for the re-direct ?
Or is there another built-in function I should use ?
Thanks
.
__________________
If you want to attract and keep more clients, then offer great
customer support .
Support-Focus.com . automates the process and gives you a
trust seal to place on your website.
I recommend that you at least take
the 30 day free trial .
01-27-2012, 01:48 PM
PM User |
#2
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
You can use parse_url. If http[s] is omitted there will be no value for the returned array's 'scheme' index. So then you can just check
PHP Code:
$sURL = $return ; if ( false !== ( $aURL = parse_url ( $return )) { if (!isset( $aURL [ 'scheme' ])) { $sURL = 'http://' . $sURL ; } }
If you have pecl installed, there is also an http_build_url, so you can just append the 'scheme' to the $aURL and then build it.
01-27-2012, 03:03 PM
PM User |
#3
Senior Coder
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
Some more solutions:
PHP Code:
// recommended if ( 0 !== stripos ( $return , 'http://' ) ) { $return = 'http://' . $return ; } // will also work if ( 'http://' != substr ( $return , 0 , 7 ) ) { $return = 'http://' . $return ; }
The use of
stripos() ensures case-insensitivity... should the URL begin with
HTTP:// , etc.
Last edited by kbluhm; 01-27-2012 at 03:22 PM ..
01-27-2012, 03:06 PM
PM User |
#4
Senior Coder
Join Date: May 2006
Posts: 1,526
Thanks: 26
Thanked 4 Times in 4 Posts
That certainly looks neater than my effort
Just thinking ...
Because of the "not equal" should ...
PHP Code:
if( substr ( $return , 0 , 7 ) != "http://" || substr ( $return , 0 , 8 ) != "https://" ) {
really be ...
PHP Code:
if( substr ( $return , 0 , 7 ) != "http://" && substr ( $return , 0 , 8 ) != "https://" ) {
I get confused with negatives
.
__________________
If you want to attract and keep more clients, then offer great
customer support .
Support-Focus.com . automates the process and gives you a
trust seal to place on your website.
I recommend that you at least take
the 30 day free trial .
Last edited by jeddi; 01-27-2012 at 03:11 PM ..
01-27-2012, 03:16 PM
PM User |
#5
Senior Coder
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
You could use a regular expression to match both
http:// and
https:// without having to use multiple conditions:
PHP Code:
if ( ! preg_match ( '~^https?\://~i' , $return ) ) { $return = 'http://' . $return ; }
~ are delimiters
^ binds the match to the beginning of the string
? makes he previous character optional (matchs both
http and
https )
\ escapes the colon because it is a special character in PCRE
i makes the match case-insensitive
Last edited by kbluhm; 01-27-2012 at 03:20 PM ..
01-27-2012, 04:01 PM
PM User |
#6
Regular Coder
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
Quote:
Originally Posted by
jeddi
That certainly looks neater than my effort
Just thinking ...
Because of the "not equal" should ...
PHP Code:
if( substr ( $return , 0 , 7 ) != "http://" || substr ( $return , 0 , 8 ) != "https://" ) {
really be ...
PHP Code:
if( substr ( $return , 0 , 7 ) != "http://" && substr ( $return , 0 , 8 ) != "https://" ) {
I get confused with negatives
.
it should && because || will always be true
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 05:52 PM .
Advertisement
Log in to turn off these ads.