php absolute path question??

Discussion in 'Web Design and Development' started by ScheitelDesign, Apr 19, 2011.

  1. ScheitelDesign macrumors newbie

    Joined:
    Apr 19, 2011
    #1
    Hey guys n gals! New account here.. So hello everyone! Got a newbe question in PHP if any one can help.. lynx remote server & MAMP local on a :apple:MacBook Pro.

    I'm trying to create a php variable that specifies absolute path so I can always point to root so php items at different directory depths always work... Here is my file structure as it looks from root (dumbed down version so it's not so long):

    public_html/

    index.php
    css/
    • layout.css
    images/
    includes/
    • head.php
    • header.php
    • footer.php
    pages/
    • about.php
    config.php​


    This is my index.php file:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <?php $thisPage="HOME";
    require_once( dirname(__FILE__) . '/config.php' );
    include (ABSPATH."includes/head.php");
    include (ABSPATH."includes/header.php");?>
    <div id="content-wrapper"><!-- Content --></div>
    <?php include (ABSPATH."includes/footer.php"); ?>
    
    This is my my config.php file:
    Code:
    <?php define( 'ABSPATH', dirname(__FILE__) . '/' ); ?>
    This is where my problem starts... when I want to use my /pages/about.php file the only way I can figure out how to get it to display correctly is by adding ../../ to the config.php require_once:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <?php $thisPage="ABOUT";
    require_once( dirname(__FILE__) . '../../config.php' );
    include (ABSPATH."includes/head.php");
    include (ABSPATH."includes/header.php");?>
    <div id="content-wrapper"><!-- Content --></div>
    <?php include (ABSPATH."includes/footer.php"); ?>
    
    Maybe I'm misunderstanding PHP but do I have to call the:
    <?php require_once( dirname(__FILE__) . 'config.php' ); ?>
    on every page and if so, do I have to set the exact root directory on every page that is nested in by using ../ to back up in my file structure??

    Note: I had a lot of trouble using $_SERVER['DOCUMENT_ROOT'] and I didn't like it. To many problem with some of my shared hosting accounts.

    Also, not sure how to handle calling my CSS, JS, images, etc. IE normally I would call the style sheet using this in the head.php file:
    Code:
    <link rel="stylesheet" href="css/layout.css" type="text/css" media="screen" />
    How should I update this href="css/layout.css" (will be used on all pages) to PHP so the directory is always root.. and how do I update the images inside of the CSS document to always look into "images/file-name.jpg" rather than look into "/page/images/file-name.jpg" if it was called from "page/about.php"... ALSO considering that this layout.css file will be used for all pages, so it has to be a function thats supported across all of the pages..

    Thats not all of my questions but I'll stop at that for now. baby steps right?

    Any constructive help would be great. I might even be writing my PHP all wrong for all I know.

    Thanks
     
  2. manueld macrumors 6502

    Joined:
    Jun 8, 2009
    #2
    sounds like you're making it more difficult on yourself. Why not just do it this way:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <?php 
    include ("/includes/head.php");
    include ("/includes/header.php");?>
    <div id="content-wrapper"><!-- Content --></div>
    <?php include ("/includes/footer.php"); ?>
    and basically anything in those three files like the css would be formatted similarly:

    Code:
    <link rel="stylesheet" href="/css/layout.css" type="text/css" media="screen" />
     
  3. ScheitelDesign thread starter macrumors newbie

    Joined:
    Apr 19, 2011
    #3
    Hey manueld.

    Thanks for the reply. If I was able to keep my file structure simple this works just fine. If I'm building a website and am only doing 10 pages or less I never run into a problem and can do some simple PHP to make my life a little easier.. The problem started with a site I've inherited from a fried who went out of business.. This site has over 300 pages and no PHP. Because of the file structure and the way it's all organized I'm having to figure out some new PHP tricks to get it to all work. For instance there is one folder on the root with 48 sub folders with HTML files in that sub folder.. IE

    http://sitename.com/devisions/tennessee/nashville.html

    So any time I call a footer.php file from nashville it looks in /tennessee/

    And with this many pages I NEED something I don't have to change/tweak for each page to convert it all to PHP so I can do the site maintenance.

    Thanks
     
  4. Yvan256 macrumors 601

    Joined:
    Jul 5, 2004
    Location:
    Canada
    #4
    http://php.net/manual/en/function.phpinfo.php

    What you're looking for is probably $_SERVER['DOCUMENT_ROOT']

    So it would become something like

    <?
    include( $_SERVER['DOCUMENT_ROOT'] . '/includes/head.php' );
    ?>

    You should also look into virtual directories (using htaccess/mod_rewrite), it could help a lot, though it's a bit complicated at first, especially with the (possibly) complex regular expressions required for most setups (so you don't redirect requests for CSS files, image files, PDF files... the list goes on).
     
  5. ScheitelDesign thread starter macrumors newbie

    Joined:
    Apr 19, 2011
    #5
    Hey Yvan256

    With the holiday I forgot to check back over the weekend

    I've looked into htaccess and haven't decided what I'm going to do.. The redirect requests for CSS files, image files, PDF files... are exactly what I've been running into. there are a lot of hurdles I'm looking at.

    Thinking that I might use a content management program instead of writing it all by hand (looking at Joomla or a custom modded version WP (dont want the client/enduser typing wp-admin to the end and seeing that its wordpress))... But I'm really itching to do it all by hand so I understand the code incase I have to fix something...

    Still open to some input.. Thanks
     
  6. innocentsheep macrumors newbie

    Joined:
    Aug 27, 2007
    #6
    Not sure if this would work, but couldn't you just make a new variable in header.php (or whatever file is being called on every page) and then make it equal to the absolute path?

    $absolutepath = "/home/www/.com/whateverwhatever/";

    And then if you needed to call footer.php you just add the variable before the file name.

    Again, not sure if this actually works. I'm still a PHP noob.
     
  7. ScheitelDesign thread starter macrumors newbie

    Joined:
    Apr 19, 2011
    #7
    Thanks innocentsheep

    I've decided to just put in the work and create a CMS for myself that covers all of my basses.. I've been doing some PHP research and have also been studying the ins and outs of Wordpress and Drupal. I've almost got a good CMS developed that will cover all of my general needs.. Something I can build on I guess.

    I've been also doing some real detailed stuff in Wordpress dealing with "Theme Menus" and "page attributes" on a custom theme I built from scratch.. With all of the new added features I'm almost thinking that Wordpress work work well as a good client CMS.. Also rewriting the htaccess to reflect a different admin url keep prying eyes form knowing that your using WP (comes in handy).
     

Share This Page