Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    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_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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 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.

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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.
    Last edited by kbluhm; 01-27-2012 at 03:22 PM.

  • #4
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    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($return07) != "http://" || substr($return08) != "https://") { 
    really be ...

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


    .
    Last edited by jeddi; 01-27-2012 at 03:11 PM.
    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.

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    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.

  • #6
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •