help with .$_SERVER['HTTP_HOST'];

Discussion in 'PHP' started by AtSeaDesign, Feb 16, 2011.

  1. #1
    Quick question. Hoping someone can point me in the right direction.

    At the top of all my pages I have:
    
    $url_web = "http://".$_SERVER['HTTP_HOST'];
    
    Code (markup):
    So I include that in all my paths to add an absolute URL to my links
    
    src="<?php echo $url_web.'/images/banners/choose_webdesign9.jpg';?>"
    
    Code (markup):
    This works fine on my home page but when I reference an include file on that home page, my links don't seem to recognize the $url_web.

    Am I missing something somewhere? How do I get the absolute path to apply to all files?
     
    AtSeaDesign, Feb 16, 2011 IP
  2. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #2
    It all depends on the content. If you have declared the variable within a function, it will only have a local scope. Likewise, if you've declared the variable in a global context but attempt to reference to within a local scope (i.e. function), you will be unable to do so unless you take advantage of the global keyword.

    One thing you could try using is $GLOBALS['url_web'];
     
    Alex Roxon, Feb 16, 2011 IP
  3. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #3
    I think he tries include php script from absolute page

    (

    include($url_web."something.php");

    )

    that's wrong, you are not allowed to call absolute url in including :p
     
    G3n3s!s, Feb 16, 2011 IP
  4. AtSeaDesign

    AtSeaDesign Active Member

    Messages:
    172
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    93
    #4
    so there's no other way to set an absolute path in an include?
     
    AtSeaDesign, Feb 16, 2011 IP
  5. AtSeaDesign

    AtSeaDesign Active Member

    Messages:
    172
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    93
    #5
    found a fix. To set an absolute path for includes use:

    
    <?php include($_SERVER['DOCUMENT_ROOT']."/includes/file.php"); ?>
    
    Code (markup):
    Then update your php.ini in your root directory with include_path

    
    include_path=".:path to your server/includes"
    
    Code (markup):
     
    AtSeaDesign, Feb 18, 2011 IP
  6. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #6
    You could, you just need allow_url_include enabled, which isn't really recommended.
     
    Alex Roxon, Feb 18, 2011 IP
  7. AtSeaDesign

    AtSeaDesign Active Member

    Messages:
    172
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    93
    #7
    Alex, the way I found seems to be secure. Any reason not to use what I found?
     
    AtSeaDesign, Feb 19, 2011 IP