SitePoint Sponsor

User Tag List

Page 4 of 4 FirstFirst 1234
Results 76 to 89 of 89
  1. #76
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I beg my pardon Scallio, you were right.
    You've got the problem right, even in spite of the OP said that it were hosts to differ, not folders.

  2. #77
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With dirname(__FILE__), doesn't it get the location of the current file? So if this header.php is included into several sublevels, the directory mapping will be inconsistent. Rather, if you use the while-loop, you create your own root with a root.txt (yes, the manual part) and then work from there.

    As for the second "bliss", I see your point. I think my main problem is understanding the differences between an application and resources. I considered all PHP files (application) and images and CSS files (resources) to be equals. They are not, it seems?

    So while the / will work with CSS, it will not work with PHP. Is this correct?

  3. #78
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,087
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by alexp91k View Post
    With dirname(__FILE__), doesn't it get the location of the current file? So if this header.php is included into several sublevels, the directory mapping will be inconsistent. Rather, if you use the while-loop, you create your own root with a root.txt (yes, the manual part) and then work from there.
    dirname(__FILE__) will give you the path of the file that calls that statement, so if your header.php is in /home/scallio/ and you include it in /home/scallio/public_html/some/terribily/long/path/index.php, it will still give you /home/scallio
    The files that include header.php might have different locations, but header PHP has one location, and putting dirname(__FILE__) in header.php will give you that location. __FILE__ refers to the file that it is in, not to the file that is called by the browser.

    Quote Originally Posted by alexp91k View Post
    As for the second "bliss", I see your point. I think my main problem is understanding the differences between an application and resources. I considered all PHP files (application) and images and CSS files (resources) to be equals. They are not, it seems?

    So while the / will work with CSS, it will not work with PHP. Is this correct?
    Correct. / on your website is your document root, / in PHP is the file system root.

  4. #79
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll test this later tonight, but thank you very much for the clarity so far. I didn't understand the difference between the two before.

    How does CSS know what my document root is? Would it be "public_html", or "website" (which is 1 sublevel beneath public_html)?

  5. #80
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so if your header.php is in /home/scallio/ and you include it in /home/scallio/public_html/some/terribily/long/path/index.php,
    Please, do us a favor: can you please provide a code to perform highlighted action?
    Thank you

  6. #81
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,087
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Alright, let me give you all an elobarate example on how I used to set up websites (before I started using Yii) to hopefully clear up this problem once and for all.
    Suppose you are building a website that a home page, an "about us" page and a contact page.
    You would need 3 files, home.php, about-us.php, and contact.php.
    Typically these files go into a directory that is not accessible by web browsers and get included by index.php that is accessible by the browser.

    You will get a file setup that roughly looks like this:
    /home/scallio/config.php
    /home/scallio/includes/home.php
    /home/scallio/includes/about-us.php
    /home/scallio/includes/contact.php
    /home/scallio/includes/header.php
    /home/scallio/includes/footer.php
    /home/scallio/public_html/index.php

    Now, since index.php is the only file in the public_html directory, it's the only one that the web brower can access. The browser doesn't know and doesn't need to know about all the other website.

    Furthermore, suppose you want to run this website on different hosts. On the one host the file layout is as I specified above, but on the other hosts all files live in /var/www/scallio.

    Now in the config, you want to know in which path it resides, because this could either be /home/scallio, or /var/www/scallio, so the config file would look something like this

    PHP Code:
    <?php
    $root 
    dirname(__FILE__);
    // more config here for databases etc
    Now for the index.php you need to include the config.php, which is one directory up, so

    PHP Code:
    <?php
    include('../config.php');
    Now we have a variable called $root that gives us the root path for the website, for the first host it is /home/scallio and for the other host it is /var/www/scallio, which is exactly what we want.

    Now to include the header.php and the footer.php in the index.php, we can extend index.php as follows:

    PHP Code:
    <?php
    include('../config.php');
    include(
    $root.'/includes/header.php');
    include(
    $root.'/includes/footer.php');
    That will tell PHP to include /home/scallio/includes/header.php and /home/scallio/includes/footer.php on the one host, and /var/www/scallio/includes/header.php and /var/www/scallio/includes/header.php on the other host.

    Now, to include the different pages, let's say we give the index.php a GET parameter called page, that indicates which page to include, but if no page is given, we want to include home.php

    That would mean we need to alter the index.php to become

    [PHP]
    PHP Code:
    <?php
    if (isset($_GET['page'])) $page=$_GET['page']; else $page='home';
    $page ltrim($page'/.');
    if (
    file_exists($root.'/includes/'.$page.'.php')) {
      
    $file $root.'/includes/'.$page.'.php';
    } else {
      
    header('HTTP/1.0 404 Not Found');
    }
    include(
    '../config.php');
    include(
    $root.'/includes/header.php');
    if (isset(
    $file)) {
      include(
    $file);
    } else {
      echo (
    '404 - The page you were looking for could not be found');
    }
    include(
    $root.'/includes/footer.php');
    So, now we can access /index.php, /index.php?page=home, /index.php?page=about-us, /index.php?page=contact

    Now suppose we don't have the website in the root of the TLD, but in some folder called mywebsite. Thus we get a file layout like

    /home/scallio/config.php
    /home/scallio/includes/home.php
    /home/scallio/includes/about-us.php
    /home/scallio/includes/contact.php
    /home/scallio/includes/header.php
    /home/scallio/includes/footer.php
    /home/scallio/public_html/mywebsite/index.php

    The only thing we need to change to take this into account is the include of the config.php file in the index.php

    Namely, we now need to do
    PHP Code:
    <?php
    include('../../config.php');
    Because the config.php is now two levels up, instead of one. Note that $root has not changed, since config.php is still in /home/scallio and dirname(__FILE__) will still return that path.
    After this change your website will run happily in the /mywebsite subdirectory.
    Try it if you don't believe me, I promise you it works.

    So now that we have set up all PHP paths correctly, let us worry about the images and CSS files.
    Suppose we add a main.css and a logo.jpg to our file layout as follows (assuming the website is running in the root of the TLD again, and not in /mywebsite):

    /home/scallio/config.php
    /home/scallio/includes/home.php
    /home/scallio/includes/about-us.php
    /home/scallio/includes/contact.php
    /home/scallio/includes/header.php
    /home/scallio/includes/footer.php
    /home/scallio/public_html/index.php
    /home/scallio/public_html/css/main.css
    /home/scallio/public_html/images/logo.jpg

    Now, if want to include the css file and the image in the header.php, I would get
    Code:
    <link rel="stylesheet" type="text/css" href="/css/main.css" />
    <img src="/images/logo.jpg" alt="My Company Logo" />
    Note that the paths I indicate here start at /, where / is equal to /home/scallio/public_html on the file system, but the browser doesn't care about that!

    Now, if I add the directory mywebsite again and leave the image and css file where they are, the links still work!

  7. #82
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, you're talking of the relative path and you just don't have a solution for the absolute one
    quod erat demonstrandum

  8. #83
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,087
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    well, you're talking of the relative path and you just don't have a solution for the absolute one
    quod erat demonstrandum
    Wow, schrapnel N5 doesn't agree with me.
    He also doesn't explain exactly what the problem is.

    Quel surprise

    Frankly Shrapnel, I'm done with you and this thread.
    alexp91k, if you want more explanations from me, please PM me, I'm ignoring this thread from now on.

  9. #84
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I spent whole page trying to explain it to hash

    Then you came in, and said:
    Using dirname(__FILE__) is much nicer in my opinion.
    But you failed to provide a code that utilizes this language construct in this topic's context.

    Okay, I'll try once more.

    I hope we all going to agree that use of absolute path is very handy.
    Because one can use the same piece of code, no matter where it called.
    For the virtual server it's just full path from the virtual server root.
    As I can see, we all share same opinion: using absolute path for the virtual server is very good.

    Example:
    Say we have 2 pages,
    /templates/users/short.php and
    /super/long/path/from/virtual/root/long.php
    also we have a picture,
    /image/pic.jpg
    and if we use absolute path to address this picture, like this:
    <img src="/image/pic.jpg">
    it work like a charm in both long.php and short.php
    because the path is absolute.
    And we all agree it is good. (I hope)

    Now we move to the filesystem. Here is our apple of discord.
    I assume, one may want to have the same "master key", same magic wand as we have in the virtual server. Same absolute path thing, which give us one piece of code to be used everywhere. No matter where it called and where it moved afterwards.
    The thing the OP, I hope, were asking for.

    Same example
    Same old 2 pages,
    /templates/users/short.php and
    /super/long/path/from/virtual/root/long.php
    but now we want to include a file, say
    /includes/page.php

    Here is the problem:
    What code we have to use in both long.php and short.php to include /includes/page.php?
    How can we utilize absolute path thing in this case?
    Is there a universal code to include your global config.php?

    Is this explanation exact enough?

  10. #85
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So above, I recently got how CSS and PHP look differently into the directories (browser vs file system).

    and if we use absolute path to address this picture, like this:
    <img src="/image/pic.jpg">
    it work like a charm in both long.php and short.php
    Isn't that line of code, <img src="/image/pic.jpg"> inside of a PHP file? Therefore, wouldn't it look deeper into the file system?

    Or: is it only with PHP includes that you have to worry about the file system root depth? Whereas with <link type=".." rel=".." href=".." /> or <script type=".." />, it is the browser looking, and the simply / will work. And the same, I assume works while you're in the .css files, that you only need to worry about /.

    So again, to reiterate, the only problem of finding the root comes in with PHP-includes, not with anything else?

  11. #86
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP works on the server side.
    the result of this work is HTML text
    HTML text being sent to the browser.
    as if it was HTML file on the server.

    when browser read this HTML,
    it find <img src="/image/pic.jpg">
    then it make another request to the server
    to get /image/pic.jpg

    so, this line yes, in the php file.
    but it become part of HTML sent to the browser.
    clear pure HTML with no sign of PHP
    as if it was just HTML file

  12. #87
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    Is this explanation exact enough?
    lol, what did you explain?

    I think you are trying to say the web root is absolute for php? How so? The php can live anywhere, so you either have to know where it is in relation to the doc root, or where it is in relation to where your entry point is. It's all relative.

    FACT: It's easier to type include $root.'includes/file.php' than include $_SERVER['DOCUMENT_ROOT'].'../includes/file.php'
    and also easier to read.


  13. #88
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have finally resolved my problem! Success!

    I want to greatly thank everyone for all the support on this problem.

  14. #89
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FACT: It's easier to type include $root.'includes/file.php' than include $_SERVER['DOCUMENT_ROOT'].'../includes/file.php'
    and also easier to read.
    but you have to define $root first.


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
  •