1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Using php includes, how to keep links correct

Discussion in 'PHP' started by mdvaldosta, Oct 13, 2005.

  1. #1
    I'm using php includes to call the navigation bar. Im about to start adding some content and will need to go into subfolders. At that point, the links are going to need ../ in front of them to work correctly. How can I do this without having to make multiple navbar php files? Possible?

    It would be really nice to only have to edit 1 file to change my site navigation.
     
    mdvaldosta, Oct 13, 2005 IP
  2. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Either use absolute links

    /gallery/pictures/1.jpg

    instead of

    ../gallery/pictures/1.jpg

    or use the head tag

    <base href="http://www.yourdomain.com">

    and make all the links relative to that address
     
    TheHoff, Oct 13, 2005 IP
  3. just-4-teens

    just-4-teens Peon

    Messages:
    3,967
    Likes Received:
    168
    Best Answers:
    0
    Trophy Points:
    0
    #3
    or make the links proper like

    instead of link being /page.php
    make the link http:// site.com/page.php
     
    just-4-teens, Oct 13, 2005 IP
  4. mdvaldosta

    mdvaldosta Peon

    Messages:
    4,079
    Likes Received:
    362
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I dont want to use the http links, I've read it's faster, more efficient to use /page.php instead.
     
    mdvaldosta, Oct 13, 2005 IP
  5. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #5
    There is no speed or efficiency difference between fully qualified and relative urls.

    You have a few different options -
    Hard code fully qualified directories/links. For url links you could just do <a href="/path/to/your/file.jpg">... or if you are using php variables and need them to work during processing, use the full path name to the file $filename = "/path/to/your/file.jpg" or maybe $imagedir = "/path/to/your/"; $filename = $imagedir."file.jpg"

    You could also parse out $PHP_SELF to get the location of your current script, and adjust your filenames using str_replace.
     
    nevetS, Oct 13, 2005 IP
  6. mdvaldosta

    mdvaldosta Peon

    Messages:
    4,079
    Likes Received:
    362
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I dont understand whats causing me to get this error:

    Warning: main(): open_basedir restriction in effect. File(/includes/topmeta.php) is not within the allowed path(s): (/home2/modded:/usr/lib/php:/usr/local/lib/php:/tmp) in /home2/modded/public_html/articles/index.php on line 1

    line 1 in the php file is <?php include("/includes/topmeta.php"); ?>

    the topmeta.php address is mysite.com/includes/topmeta.php
     
    mdvaldosta, Oct 15, 2005 IP
  7. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #7
    the path in your php include is a filesystem path - not a web url path. You need to either use a relative path, or the absolute file system path - something like:

    
    <?
    include("/home2/modded/public_html/includes/topmeta.php");
    ?>
    
    PHP:
     
    nevetS, Oct 15, 2005 IP
    1 person likes this.
  8. GeorgeB.

    GeorgeB. Notable Member

    Messages:
    5,695
    Likes Received:
    288
    Best Answers:
    0
    Trophy Points:
    280
    #8
    You do realize you can include files in higher directories with ../ right?

    You have a file file.php in /home/subdir/file.php

    topmeta.php is in /home/includes/topmeta.php

    You want to link to /home/subdir/subdir2/file2.php

    So in file.php just do a <?php include "../includes/topmeta.php"; ?>

    The links will be relative to wherever file.php is not topmeta.php so it'll work fine.
     
    GeorgeB., Oct 15, 2005 IP
  9. mdvaldosta

    mdvaldosta Peon

    Messages:
    4,079
    Likes Received:
    362
    Best Answers:
    0
    Trophy Points:
    0
    #9

    yes I do, but I didn't realize it was pulling from the file root. I assumed, because I used absolute links in the images as well, that files were pulled the same way. I'll give it a shot.


    ***edit*** I used nevetS method, works great. thanks.
     
    mdvaldosta, Oct 15, 2005 IP