Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2012, 01:26 PM   PM User | #1
jeddi
Senior Coder

 
Join Date: May 2006
Posts: 1,526
Thanks: 26
Thanked 4 Times in 4 Posts
jeddi has a little shameless behaviour in the past
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_returnFILTER_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($return07) != "http://" || substr($return08) != "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.
jeddi is offline   Reply With Quote
Old 01-27-2012, 01:48 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 01-27-2012, 03:03 PM   PM User | #3
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Some more solutions:
PHP Code:
// recommended
if ( !== stripos$return'http://' ) )
{
    
$return 'http://' $return
}

// will also work
if ( 'http://' != substr$return0) )
{
    
$return 'http://' $return

The use of stripos() ensures case-insensitivity... should the URL begin with HTTP://, etc.
__________________
ZCE

Last edited by kbluhm; 01-27-2012 at 03:22 PM..
kbluhm is offline   Reply With Quote
Old 01-27-2012, 03:06 PM   PM User | #4
jeddi
Senior Coder

 
Join Date: May 2006
Posts: 1,526
Thanks: 26
Thanked 4 Times in 4 Posts
jeddi has a little shameless behaviour in the past
That certainly looks neater than my effort


Just thinking ...

Because of the "not equal" should ...

PHP Code:
if( substr($return07) != "http://" || substr($return08) != "https://") { 
really be ...

PHP Code:
 if( substr($return07) != "http://"  && substr($return08) != "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..
jeddi is offline   Reply With Quote
Old 01-27-2012, 03:16 PM   PM User | #5
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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
__________________
ZCE

Last edited by kbluhm; 01-27-2012 at 03:20 PM..
kbluhm is offline   Reply With Quote
Old 01-27-2012, 04:01 PM   PM User | #6
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
Quote:
Originally Posted by jeddi View Post
That certainly looks neater than my effort


Just thinking ...

Because of the "not equal" should ...

PHP Code:
if( substr($return07) != "http://" || substr($return08) != "https://") { 
really be ...

PHP Code:
 if( substr($return07) != "http://"  && substr($return08) != "https://") { 
I get confused with negatives


.
it should && because || will always be true
jmj001 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:52 PM.


Advertisement
Log in to turn off these ads.