The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Basic PHP question

JasconiusJasconius sword criminalmad onlineRegistered User regular
edited March 2009 in Help / Advice Forum
Trying to convert a site to PHP. I've never done anything professionally in PHP before. Trying to create a system of include files.

Files are stored in /includes/


Make a file in the root, call the files with

include("/includes/head.php");

works fine.


Create a file in the /about/ folder the same way, doesn't work. Remove the slash at the beginning, doesn't work. Put a ../ at the beginning, doesn't work.

How does file referencing work typically in PHP? What are the rules?

It should be noted that I can get it to work on my local machine but not when I upload it to my host. I do not know what web server they are running.

this is a discord of mostly PA people interested in fighting games: https://discord.gg/DZWa97d5rz

we also talk about other random shit and clown upon each other
Jasconius on

Posts

  • JHunzJHunz Registered User regular
    edited March 2009
    The rules are laid out fairly clearly in the first couple paragraphs here: http://us3.php.net/function.include
    I will admit, though, that just looking at it I would have expected "../includes/head.php" to have done the trick.

    How is your include_path directive set up?

    JHunz on
    bunny.gif Gamertag: JHunz. R.I.P. Mygamercard.net bunny.gif
  • FightTestFightTest Registered User regular
    edited March 2009
    include($_SERVER . '/includes/head.php');

    FightTest on
    MOBA DOTA.
  • SzechuanosaurusSzechuanosaurus Registered User, ClubPA regular
    edited March 2009
    ../ should work just as well if you know that the file in question is only one folder deep, FightTests method is going to be a more universal solution.

    Bear in mind as well that including documents has an effect on navigation URIs as well. If you are using relative navigation URIs in your includes (for example, if you are inserting the site's main navigation as an include on each page) then you will need to also use some php to automatically append the URIs relative to where the file calling the include is.

    eg

    If your folder structure is something like this:
    index.php
    includes/main-nav-include.php
    about/about.php
    

    And both index.php and about.php are calling main-nav-include.php, then the links in main-nav-include.php either need to be complete links - http://www.example.com/index.php and http://www.example.com/about/about.php - or they need to be appended contextually depending on where the current page is located in the folder structure.

    eg, in your main-nav-include.php file you'd put something like

    [PHP]<?php $currentPage = basename($_SERVER);
    $currentDirectory = basename(dirname($_SERVER));

    if ($currentDirectory == 'about') {
    $urlAppend = '../';
    } else {
    $urlAppend = ' ';
    } ?>[/PHP]

    At the beginning and then your navigation would look something like

    [HTML]<ul>
    <li><a href="<?php echo $urlAppend ?>index.php">HOME</a></li>
    <li><a href="<?php echo $urlAppend ?>about/about.php">ABOUT</a></li>
    </ul>[/HTML]

    That's not a great example because in that instance it would probably be easier and quicker to just use absolute URIs but in a situation where you have a larger site with lots of links and multiply nested folders and maybe don't want to be tied to absolute URIs then altering the first bit of code to be more flexible and allowing for automatically adding the appropriate append to any links will make your includes that much more suited to rapid development.

    Szechuanosaurus on
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited March 2009
    I use a $baseurl variable that I generate based on the location of the running script (the domain)

    [php]
    private function DetermineBase()
    {
    $serverName = $_SERVER;
    if (strpos($serverName, '127.0.0.1') !== FALSE)
    {
    return 'some/folder/structure;
    }
    else if (strpos($serverName, 'some.domain.com') !== FALSE)
    {
    return 'folder;
    }
    return '/';
    }
    [/php]

    Basically it looks at where the script is being run from and based on where I know I store that script in the different servers, I return a different base url.

    Note: I use this in an index.php page that includes everything else (single point of contact), but this function could easily be changed to work against an array such as

    base url -- file
    / -- index.php
    ../ -- about/file.php

    However this may just be extending a bad pattern - what are you including from your includes folder?

    Seguer on
Sign In or Register to comment.