WHMCS detect if https then convert all urls to https

Discussion in 'PHP' started by khurramalvi, Apr 15, 2011.

  1. #1
    Hi,

    I am facing a big challenge with WHMCS. I installed SSL certficate and in firefox everything works fine but in IE I got annoying popup saying that this page has unsecure items.

    I want to have function that detect that if I am on https then all links will change from http to https for e.g. http://www.domain.com/images/logo.php to https://www.domain.com/images/logo.php and so on.

    This works fine when I hardcode everything but when I go out of secure path everything messed up because it call my homepage i.e. http://www.domain.com to https://www.domain.com

    I found this piece of code and it doesn't work with whmcs.

    if ($_SERVER['HTTPS']!="on") {
    $url = "https://".$_SERVER["HTTP_HOST"]."/".$_SERVER["REQUEST_URI"];
    header("Location: $url");
    exit;
    }

    I am thinking a way around to create something like this:

    <a href="page.php" {php if ($securepage == 'https://') { }else="http://"{php } }>

    Thanks for your help.
     
    khurramalvi, Apr 15, 2011 IP
  2. AdM.as

    AdM.as Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are a few things you can do, it depends on which method you'd prefer to solve the problem (considering $_SERVER["SERVER_PORT"] works which I believe it will).

    1. You can check if the client is connecting via port 443 and have a variable store the proper scheme, i.e. http or https. This is the code to do so:

    {php}
    $scheme = ($_SERVER['SERVER_PORT'] == 443) ? 'https' : 'http';
    {/php}
    PHP:
    And then for each of your img, script, etc tags they would have to be set up in this manner:

    <img src="{php}echo $scheme;{/php}://www.domain.com/page.php" alt="Alt Text" />
    HTML:
    2. You could turn on output buffering so everything sent from PHP will be stored in a buffer. You would then see if the request is via https via the port again; if it is, replace all strings containg 'http://www.domain.com' with 'https://www.domain.com', and then finally output the buffer. The code for that would look like:

    {php}
    //This goes at the very top of your page, before anything is echoed
    //The function to replace URLs
    function callback($buffer)
    {
         if($_SERVER['SERVER_PORT'] == 443)
              return (str_replace("http://www.domain.com", "https://www.domain.com", $buffer));
         else
              return $buffer;
    }
    
    ob_start("callback");
    {/php}
    
    /* --------- Rest of the page goes here ------------ */
    
    {php}
    ob_end_flush();
    {/php}
    PHP:
    With the second method, however, the entire page must load completely before anything will be shown on the client's end. I would recommend using the first method.
     
    Last edited: Apr 17, 2011
    AdM.as, Apr 17, 2011 IP