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.

PHP include hyperlinks and subdirectories

Discussion in 'PHP' started by l3l00, Nov 2, 2013.

  1. #1
    Okay so I made a configuration file. Also I am testing this out with Xampp.

    
    
    $config = parse_ini_file("config/global.ini");
    $tester = parse_ini_file("config/tester.ini");
    
    $serverName = $_SERVER['SERVER_NAME'];
    
    //~ $serverName = $_SERVER['DOCUMENT_ROOT'];
    
    
    if (isset($tester[$serverName])) {
        $serverName= $tester[$serverName];
        }
    
    
    Code (markup):
    It works across the board however, if a file is in a subdirectory, the menu hyperlink will start in the same folder then look for $serverName.

    
    The file is in: "websites name"/tuts/lib/a/sideflow.php
    
    Code (markup):
    it links there just fine, the CSS does not show and the menu links look like:

    
    http://localhost/zerocore/work/www."websites name".com/tuts/lib/a/students.php
    
    Code (markup):
    it should not have the (tuts/lib/a) when inside the sideflow.php file, this is for the menu across the site. What am I doing wrong?
     
    l3l00, Nov 2, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I tend to make all hyperlinks start at the document root - that way they always work

    so regardless of the folder I'm in the link will always be /folder/subfolder/page
    it makes life a lot easier.
     
    sarahk, Nov 2, 2013 IP
  3. l3l00

    l3l00 Greenhorn

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    see the issue with that is:
    
    <a href="/students.php" class="ex2">Students</a> 
    (this is inside the include "header.php")
    
    Code (markup):
    it goes to:

    
    localhost/students.php
    
    Code (markup):
    I am using Xampp

    on the live server I have a Dev directory, that method works. However what about localhost Xampp testing?
     
    Last edited by a moderator: Nov 2, 2013
    l3l00, Nov 2, 2013 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    I'm not sure the Xampp matters.

    You have a site you are testing and it's in a subfolder in a subfolder etc.

    In your config file "declare" a variable that holds what you think the root is, ie not localhost but "/localhost/zerocore/work/www."websites name".com/" and put that at the beginning of every hyperlink.
    then when the site goes live and onto proper hosting you just have to change the value of that declared variable
     
    sarahk, Nov 2, 2013 IP
  5. l3l00

    l3l00 Greenhorn

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    
    "/localhost/zerocore/work/www."websites name".com/"
    
    Code (markup):
    That only shows in Xamppp

    
    http://www."websites name".com/tuts/lib/a/students.php
    
    Code (markup):
    shows on the live site when I click on the students link
     
    l3l00, Nov 2, 2013 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #6
    That was my point.
     
    sarahk, Nov 2, 2013 IP
  7. l3l00

    l3l00 Greenhorn

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    I do not think I am getting my problem properly across.

    see the side flow is two directories under the root. Everything else is inside root pretty much.
    so the link above to go to students.php is:

    
    <a href="/students.php" class="ex2">Students</a>
    
    Code (markup):
    found inside an include "header.php" which is in the root directory. It goes to students.php everywhere, however, inside sideflow.php which is in directory: /tut/lib/a/sideflow.php

    if you click on it, the result is:

    
    www."my website".com/tut/lib/a/students.php
    (THIS IS AN ERROR 404, STUDENTS.PHP IS LOCATED AT:    WWW."MY WEBSITE".COM/STUDENTS.PHP
    
    Code (markup):
     
    l3l00, Nov 2, 2013 IP
  8. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #8
    I think what Sarah is trying to say, create a file, ex: definitions.php (include the file in all files, or in a shared file if you have any, ex: dbconnect, header, template, etc.. that is already included in all files). In it place:

    
    <?php
    // Different set of configs if you are on localhost or live
    if ($_SERVER['HTTP_HOST'] == 'localhost') {
        define('APPLICATION_URL', 'http://localhost/site.com');
        define('APPLICATION_PATH', '/localhost/zerocore/work/www.site.com/');
    } else {
        define('APPLICATION_URL', 'http://www.site.com');
        define('APPLICATION_PATH', '/var/zerocore/work/www.site.com/');
    }
    
    PHP:
    Then anywhere you create a link, append the URL as so:

    
    <a href="<?php echo APPLICATION_URL;?>/students">Link</a>
    
    Code (markup):
     
    ThePHPMaster, Nov 2, 2013 IP
    sarahk likes this.
  9. l3l00

    l3l00 Greenhorn

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #9
    [RESOLVED]

    YAY! ^-^

    Thank you soooooo much ThePHPMaster, that resolved my issue across the board. It functions on localhost and when I load it into development. If it works in dev it will work when I switch it over to production. Thank you Thank you Thank you.
     
    l3l00, Nov 3, 2013 IP