ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Automatic config[base_url]

September 05, 2007 11:19am

Subscribe [27]
  • #16 / Dec 06, 2007 6:29pm

    tonanbarbarian

    650 posts

    actually the ternary operator x ? y : z; is faster than an if statement

    so use this and it is even faster

    $config['base_url'] = ($_SERVER['HTTP']=='localhost') ? 
      'http://localhost/path/to/local/folder/' : // local base url
      'http://www.server.tld/path/to/online/folder/'; // live base url

    maybe not quite as readable however although the commenting helps

  • #17 / Dec 06, 2007 6:58pm

    paulopmx

    164 posts

    Guys, this Automatic config[base_url] solution is actually useful in letting you use multiple domains or addresses for the same website, not just 2 or 3.

  • #18 / Dec 07, 2007 4:46am

    Thoer

    111 posts

    @paulopmx: I’m aware of that, but setting up a new website starts with setting the configs anyway so I don’t really care about multiple domains. What I do care about is the problem mentioned in the opening post, which is setting up a site, making a slight config mod and uploading a config file that will not work on that server.

    @tonanbarbarian: Nice, although I only compared mine to statements with multiple operators in it.

  • #19 / Mar 18, 2008 2:38pm

    nikefido

    67 posts

    Please excuse my ignorance, how do you set certain pages to be HTTP while other pages are HTTPS? (for instance, a home page vs vs a enter billing info page)

  • #20 / Mar 18, 2008 2:51pm

    Rick Jolly

    729 posts

    Please excuse my ignorance, how do you set certain pages to be HTTP while other pages are HTTPS? (for instance, a home page vs vs a enter billing info page)

    First, be sure all links to secure pages are https. Second, in secure pages verify that the protocol is https. If not, then redirect using https.

    It’s helpful to extend a common parent controller for all secure controllers. Then do the protocol check from the constructor of the secure parent controller.

  • #21 / Mar 18, 2008 2:53pm

    Code Arachn!d

    92 posts

    Well depending on the server you would install a SSL certificate for the site - thus when https is used for the domain it knows where to pull the files from. What I’ve done is checks on certain pages so that if the user goes to the page and it’s not https that it will redirect them to the https connection.

  • #22 / Mar 18, 2008 2:54pm

    Code Arachn!d

    92 posts

    Rick beat me to the answer - I guess I need to work on my typing speed.

  • #23 / Mar 18, 2008 3:49pm

    nikefido

    67 posts

    awesome, thanks for the tip.

  • #24 / Mar 21, 2008 3:19pm

    nikefido

    67 posts

    What I’ve done is checks on certain pages so that if the user goes to the page and it’s not https that it will redirect them to the https connection.

    can this test just be

    if(!$_SERVER['HTTPS']) {
    //redirect to HTTPS
    }

    ?

    I’m not sure if that’s all encompassing or not (in other words, if that’s a good enough test for your suggestion above).
    This is a “redundant” test as all links will be pointing to an HTTPS location, but have to make sure since people can type link locations manually.

  • #25 / Mar 21, 2008 4:33pm

    nikefido

    67 posts

    What I wrote above is what I did, but CI’s redirect function is only relative, so I had to use good old fashioned “header(‘location: mypate.com’);”

  • #26 / Apr 09, 2008 2:10am

    Tony Nash

    42 posts

    Sorry, But can someone help me to understand following code. I am not sure why we need to check $_SERVER[‘HTTPS’] == “on”?

    (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on")
  • #27 / Apr 09, 2008 11:44am

    Pascal Kriete

    2589 posts

    Quoting php.net:

    Set to a non-empty value if the script was queried through the HTTPS protocol. Note that when using ISAPI with IIS, the value will be off if the request was not made through the HTTPS protocol.

  • #28 / Apr 22, 2008 1:56pm

    bhogg

    42 posts

    So, adding the localhost condition turns into something like:

    $proto = "http" .
        ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "s" : "") . "://";
    $server = isset($_SERVER['HTTP_HOST']) ?
        $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
    $server .= '/'; // Added since the trailing slash was not present in the above code
    
    // Add the current subdirectory on a local server
        if (strstr($server, 'localhost'))
            $server .= 'bh/';

    Test in a subdirectory on dev, upload to production on any domain.

    Not sure there would be a way to automatically detect a subdirectory when also eliminating the index.php?  And in theory you could setup a nice dev environment with .testdomain.com…

  • #29 / Apr 23, 2008 7:07am

    Adods

    22 posts

    What if user use .htaccess to remove ‘index.php’ from url??

  • #30 / Apr 23, 2008 7:14am

    xwero

    4145 posts

    index.php is not a part of the base_url setting that is the index_page setting so if you add index.php to the base_url you are going to have trouble with certain methods and functions.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases