SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php and root url paths

    I have just figured out how to define the root path and paths relative from root with php, it works on localhost and my live server.

    <?php
    include $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/includes/file.html';
    ?>

    Still it is slightly long. Is there a way to shortern this, or delcare this function once somewhere, so I only have to type "/" or something similar to specify the root directory in localhost and live server?

  2. #2
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this?

    PHP Code:
    define('SITE_PATH'realpath(dirname(__FILE__))); 
    If you are using front-controller pattern (you are controlling whole site via index.php), just define the constant there and you can use it sitewide.

  3. #3
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also use <base> tag to define base URL for all links and images on your site, so you won't need to write the full path every time:

    PHP Code:
    <base href="<?php echo SITE_PATH?>" />
    Put it somewhere between <head> and </head>

  4. #4
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also is it a good idea to include every single thing that is repeated on my webpages.

    For example put the DTD, <html><body> tags, css link in head etc in "include.html", and then in my webages just use the php to include that file at a tag at the top and then put the content for that webpage.

    I know I would need to change different things such as meta tags in head between webpages, but just theoretically, is there anything wrong with including everything but the webpage content in an external file.

    Like this in my "include.php":
    ****
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
    </title>
    <link rel="stylesheet" type="text/css" href="/layout/global.css"/>
    </head>
    <body>
    <div id="centre">

    <div id="top">
    <div class="floatr">ON MEDIAS BETA VERSION 2008. COMMERCIAL AND CULT ENTERTAINMENT SOURCE</div>
    </div>

    <div id="logo">
    <a href="/copyright.php">Copyright notice</a>
    </div>


    <div id="nav">
    <div id="navbarl"></div>
    <div id="navbarr"></div>
    <ul class="nav">
    <li><a href="/">MAIN</a></li>
    <li><a href="/tv">TV</a></li>
    <li><a href="/movies">MOVIES</a></li>
    <li><a href="/systems">SYSTEMS</a></li>
    <li><a href="/news">NEWS</a></li>
    </ul>

    </div>

    <div id="conttop">
    <div id="conttopl"></div>
    <div id="conttopr"></div>
    </div>

    <div id="content">
    ****

    and this in all my webpages

    *****
    <?php
    include $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/includes/include.php';
    ?>

    individual page content here

    *****

  5. #5
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know I would need to change different things such as meta tags in head between webpages, but just theoretically, is there anything wrong with including everything but the webpage content in an external file.
    Nothing bad with it at all. In fact, that's how it is usually done. My webpages usually all look like this (simplified of course):

    PHP Code:
    <?php include header.php?>

    <div id="content">
        <h2><?php echo htmlspecialchars($h2); ?></h2>
        <?php echo htmlspecialchars($content); ?>
    </div>

    <?php include footer.php?>
    You get the point. Then, header.php could look for example like this:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head profile="http://gmpg.org/xfn/11">

    <title><?php echo htmlspecialchars($title); ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="css/default.css" type="text/css" media="screen" />

    </head>
    <body>
        
    <div id="header">
        <h1><?php echo htmlspecialchars($h1); ?></h1>
    </div>
    footer.php might look like this:

    PHP Code:
    <div id="footer">
        <p>All contents copyright &copy; <?php echo date('Y'); ?> Richard Knop</p>
    </div>

    </body>
    </html>

  6. #6
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea what **If you are using front-controller pattern (you are controlling whole site via index.php), just define the constant there and you can use it sitewide.*** means.

    Are you saying I only need to put it in my index.php in my root directory? I have called all my webpages index.php in seperate subfolders so that people can just type x.com/, instead of x.com/xyg.php.

    So if I put this in my head of my webpage <base href="<?php echo SITE_PATH; ?>" />, then what do I put to call the root path... "/" ?

    Actually since I am including a lot of content anyway I could put something in my include.php which also declares the root path..... or not -because I need to specify the root path before I include anything...

  7. #7
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks risoknop. It makes my life a lot easier.

    It does lead me to more questions though.... suppose I wanted to declare
    <?php include mainframe.php; ?>
    only once.

    And then have different meta tags, content, and information in the header. I could specify in mainframe.php all my page headers and content, but a function which calls upon to go back to the original webpage and insert the different content, this way I don't have to use the <?php include header.php; ?> a million times between bits of content in my webpages.

  8. #8
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh. is that what <?php echo htmlspecialchars($title); ?> means? getting the content from the original page?

  9. #9
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea what **If you are using front-controller pattern (you are controlling whole site via index.php), just define the constant there and you can use it sitewide.*** means.
    What I meant was that when you have just one index.php file which acts as a single point of entry to your website, you can just define the constant there and it will be sitewide.

    Are you saying I only need to put it in my index.php in my root directory? I have called all my webpages index.php in seperate subfolders so that people can just type x.com/, instead of x.com/xyg.php.
    Well in that case it won't work. You will have to put in every index.php file. Or, you can put it in header.php file (or call it differently if you wish) and include that file in every index.php.

    So if I put this in my head of my webpage <base href="<?php echo SITE_PATH; ?>" />, then what do I put to call the root path... "/" ?

    Actually since I am including a lot of content anyway I could put something in my include.php which also declares the root path..... or not -because I need to specify the root path before I include anything...
    SITE_PATH is a constant containing absolute URL to your website. Just do:

    PHP Code:
    echo SITE_PATH
    to see what is there.

    See http://www.w3schools.com/TAGS/tag_base.asp for <base> tag explanation.

  10. #10
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyjetsu View Post
    oh. is that what <?php echo htmlspecialchars($title); ?> means? getting the content from the original page?
    Well, it's a little bit more complicated... I'm using MVC design pattern so I just set the $title in every action so it dynamically changes.

  11. #11
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Be careful blindly applying design methodologies. In risoknop's example, while he may be using MVC, he violates DRY in his template files with the header and footer includes.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  12. #12
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea what that means bhanson, could you elaborate?

    If something works, then I can see no problem, since it is executed on the server the same way whatever visitor access's the site with whatever browser or platform, so once you test it once, it should always work.... as long as you don't edit the php code.

  13. #13
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyjetsu View Post
    I have no idea what that means bhanson, could you elaborate?
    DRY stands for Don't Repeat Yourself. In that example for every content page you have to include code for both the header and the footer. With just a small amount of extra thought in the design process, you could eliminate that need by using something closer to a template system.

    Template file:
    PHP Code:
    <html>
    <body>
    <?=$this->content?>
    </body>
    </html>
    Content file example:
    PHP Code:
    <h1>Title</h1>
    <
    p>Lookno unneeded header/footer includes</p
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  14. #14
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks bhanson, I haven't realized I am doing this mistake

  15. #15
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I still do not understand it all fully, I will research this template system more. I don't know much php, but already it has optimized my website development by an amount I cannot begin to describe. I should have started php a long time ago.

  16. #16
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a new problem.

    I have a tracking script I have added.

    <?php
    $ipaddress = $_SERVER['REMOTE_ADDR'];
    $page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";
    // $page .= iif(!empty($_SERVER['QUERY_STRING']), "?{$_SERVER['QUERY_STRING']}", "");
    $referrer = $_SERVER['HTTP_REFERER'];
    $datetime = mktime();
    $useragent = $_SERVER['HTTP_USER_AGENT'];
    $remotehost = @getHostByAddr($ipaddress);




    $logline = $ipaddress . '|' . $referrer . '|' . $datetime . '|' . $useragent . '|' . $remotehost . '|' . $page . "\n";


    $logfile = $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/logfile.txt';


    if (!$handle = fopen($logfile, 'a+')) {
    die("Failed to open log file");
    }


    if (fwrite($handle, $logline) === FALSE) {
    die("Failed to write to log file");
    }

    fclose($handle);
    ?>

    <?php include $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/logfile.txt'; ?>

    I added the last line to output the contents in the browser, just for testing. The problem is, $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/logfile.txt'; is not working on my live host anymore(I get failed to open logfile), it works on localhost though.

    It is strange because this script is in a file and I am using $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/logfile.txt'; to call this file. so this include method is working, unless I use the method in a php script which is called upon with this include method. If you can understand that.

  17. #17
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually no thats not right, i have used it in the last line i added for testing. so there must be something wrong with the syntax of $logfile = $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/logfile.txt'; only when on a live server?

    Also strangly enough, $logfile = 'logfile.txt'; only works on localserver too.....
    Last edited by cyjetsu; Dec 26, 2008 at 21:53.

  18. #18
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,834
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by cyjetsu View Post
    actually no thats not right, i have used it in the last line i added for testing. so there must be something wrong with the syntax of $logfile = $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/logfile.txt'; only when on a live server?

    Also strangly enough, $logfile = 'logfile.txt'; only works on localserver too.....

    *edited*
    I have now tried a few methods... it seems all of them work on localhost and all fail on my server... i suspect it has something to do with permissions on my webhost server now.
    I have rightly or wrongly always used getcwd() to find out where the file is situated. I also have include a '_common.php' file that and this is one of the settings:
    PHP Code:
      error_reporting(E_ALL E_STRICT);
      
    ini_set('display_errors','On'); 
      
      
    define('LOCALHOST''localhost' === $_SERVER['SERVER_NAME']); 
    I would solve your problem with the different paths for the "logfile.txt" file like this.

    PHP Code:

      
    // default to Online
      
    $logfile $_SERVER['DOCUMENT_ROOT'] . '/' $dir '/logfile.txt';
      if (
    LOCALHOST)
      {
        
    $logfile getcwd() . '/logfile.txt';
      }

      if (!
    $handle fopen($logfile'a+'))
      {
        die(
    "Failed to open:  " .$logfile);
      } 

    As far as using a CMS and passing different $title parameters I use this code at the top of all files:
    PHP Code:

      <?php echo $doctype ?>
      <head>
        <?php echo $header ?>
      </head>
    and here is my header:
    PHP Code:

    <?php 
      
    // sometimes these variables are not set - easier to check here than to have an error
      
    $keywords     = isset($keywords)    ? $keywords     'Johns-Joke of the day';
      
    $description  = isset($description) ? $description  $keywords;
    ?>
    <title>Johns Joke of the Day: <?= $joke_title ?></title>
    <meta name="keywords"     content="<?= $keywords ?>" />
    <meta name="description"  content="<?= $description ?>" />
    <meta http-equiv="Content-Type" content="text/html charset=UTF-8" />  
    <link type="text/css" href="style_001.css" rel="stylesheet" />
    <meta name="author"       content="John_Betong" />
    <meta http-equiv="imagetoolbar" content="no" />
    <meta name="robots" content="follow, all" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <script type='text/css'>
    <!--[if !IE]>-->
    body {font-size:10px}
    <!--<[endif]-->
    </script>
    As a matter of interest I use CodeIgniter an "Open Source Web Application Framework that helps you write kick-*** PHP programs". What application framework are you using?
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  19. #19
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks thats a lot of useful information, am processing it all now.

    First of all, Failed to open: /var/www/vhosts/onmedias.com/httpdocs/logfile.txt, is the error I get when I use your script(when on my live host, localhost shows no error as usual:

    <?php
    // default to Online
    $logfile = $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/logfile.txt';
    if (LOCALHOST)
    {
    $logfile = getcwd() . '/logfile.txt';
    }
    if (!$handle = fopen($logfile, 'a+'))
    {
    die("Failed to open: " .$logfile);
    }
    ?>

    Since this is still only happening on my live server, I suspect it is my file permissions on my live server, so far only include works on my live server.... I have tried various chmods at the beggining of the file to try and set permissions for this file but I always get a parse error or a T_variable error or something.

    As for apps, I am using none yet, just emEditor, the text/code editor I use for everything.

  20. #20
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    now I am sure of it.

    <?php

    chmod("logfile.txt", 0777);




    $filename = 'logfile.txt'; // Unix

    if (is_writable($filename)) {
    print file_get_contents($filename);
    } else {
    print 'File not readable!';
    }

    ?>

    I get the error file not readable(well writable whatever), but I have put
    chmod("logfile.txt", 0777);
    I dont get it.

    I tried
    chmod($_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/logfile.txt', 0777);

    But I still get "failed to open file" only on my livehost

  21. #21
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyjetsu View Post
    now I am sure of it.

    <?php

    chmod("logfile.txt", 0777);




    $filename = 'logfile.txt'; // Unix

    if (is_writable($filename)) {
    print file_get_contents($filename);
    } else {
    print 'File not readable!';
    }

    ?>

    I get the error file not readable(well writable whatever), but I have put
    chmod("logfile.txt", 0777);
    I dont get it.

    I tried
    chmod($_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/logfile.txt';, 0777);
    which is the same url(from root path), but I get:
    Parse error: parse error in C:\operator\bases\onmedias\primary\includes\tracker.php on line 3
    Please use the PHP tags when displaying code.

    PHP Code:
    chmod("logfile.txt"0777); 
    That will only change logfile.txt to 0777 if you already have permission to. Chmod() returns a boolean value so you can use that to check to see if it's changing it.

    PHP Code:
    $changed chmod("logfile.txt"0777);
    if (
    $changed) {
        echo 
    "Success\n";
    } else {
        echo 
    "Failure\n";

    As for the parse error on line 3, well... there's a parse error on line 3. You have a semi-colon where there shouldn't be one.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  22. #22
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are right bhanson. I added that test code, and on my localhost I get "success" and on my livehost I get "failure" Now I just need to work out how to get permission to change the permissions of my files on my livehost.

  23. #23
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed the permissions in my webhost control panel instead. works ok


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •