Constant code not working

Discussion in 'PHP' started by gwh, Mar 29, 2010.

  1. #1
    Hi everyone,

    When I was building my site locally, I was using the following code to ensure all my links resolved:

    
    <?php
    if(!defined('__ROOT__')){ 
    	if ($_SERVER['HTTP_HOST'] != "mysite.com") { 
    	   define ('__ROOT__', $_SERVER['DOCUMENT_ROOT'] . '/mysite');
    	} else {
    	   define ('__ROOT__', $_SERVER['DOCUMENT_ROOT']);
    	}
    }
     ?>
    
    PHP:
    Then I'd used this to include some code:

    
    <?php
    include_once(__ROOT__ . "/includes/config.inc.php");
    include_once(__ROOT__ . "/includes/helpers.inc.php");
     ?>
    
    PHP:
    I thought that once I deployed my site on the remote server that I wouldn't need to change any code, ie. that the constant definition above would ensure everything worked. However when I uploaded the site, I found that I needed to swap out the above mentioned constant with the following line so that all the links worked correctly:

     <?php
    define ('__ROOT__', $_SERVER['DOCUMENT_ROOT']);
     ?>
    
    PHP:
    Can anyone tell me why it didn't work or what I can do next time to ensure that it does?

    Appreciate any advice.
     
    gwh, Mar 29, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In the address bar, do you have mysite.com or www.mysite.com. If it's the latter, then it will still think it's not your website
     
    JAY6390, Mar 29, 2010 IP
  3. meloncreative

    meloncreative Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I use

    if(strstr($_SERVER['SERVER_NAME'], "devserver.com")) {
    }
    elseif(strstr($_SERVER['SERVER_NAME'], "liveserver.com")) {
    }
    Code (markup):
     
    meloncreative, Mar 29, 2010 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Personally I would just use a switch

    switch ($_SERVER['SERVER_NAME']) {
        case 'mysite.com':
        case 'www.mysite.com':
            define('__ROOT__', $_SERVER['DOCUMENT_ROOT'] . '/mysite');
            break;
        case 'localhost':
            define('__ROOT__', $_SERVER['DOCUMENT_ROOT']);
            break;
    }
    PHP:
     
    JAY6390, Mar 29, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    str_replace("www.", "", $_SERVER['HTTP_HOST']);
    PHP:
    That way, either way www. will be removed.
     
    danx10, Mar 29, 2010 IP
  6. gwh

    gwh Active Member

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    93
    #6
    Thanks for the replies,

    I tried the above code but when I put the page on the remote server I got the following error:

    Parse error: syntax error, unexpected T_STRING, expecting T_CASE or T_DEFAULT or '}' in /home/mysite/www/www/business/index.php on line 3

    Line 3 is as follows:

        case 'mysite.com':

    Obviously I replaced the domain names with the correct ones but I still don't know why it's not working. It was only when I replaced the code with the following did it work:

    define ('__ROOT__', $_SERVER['DOCUMENT_ROOT']);

    Does anyone have any suggestions?
     
    gwh, Mar 29, 2010 IP
  7. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #7
    Read wordpress sources and learn how to make your code flexible and independent on the directory you put it.

    It seems your hosting provider has no subdirectories for virtual hosts or substituted DOCUMENT_ROOT straight to your vhost dir.

    do
    echo $_SERVER['DOCUMENT_ROOT']
    and see what it is.
     
    stOK, Mar 30, 2010 IP
  8. gwh

    gwh Active Member

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    93
    #8
    I inserted the echo statement that you suggested but nothing was printed. It just continued to give me the same error.

    I'm not sure how to solve it. Any other further advice appreciated.
     
    gwh, Mar 30, 2010 IP
  9. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #9
    The code posted here is being parsed good. Check your uploaded text for missing quotation marks.
     
    stOK, Mar 30, 2010 IP
  10. gwh

    gwh Active Member

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    93
    #10
    The error I'm getting now is the following:

    Warning: include_once(/home/mysite/www/www/mysite_2010/includes/config.inc.php) [function.include-once]: failed to open stream: No such file or directory in /home/mysite/www/www/business/index.php on line 12

    Line 12 is:

    include_once(__ROOT__ . "/includes/config.inc.php");

    Does the above line need to be different now that the constant code has been changed?
     
    gwh, Mar 30, 2010 IP