Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: PHP include() Not Working [PHP Level: Newbie] [SOLVED]  (Read 3754 times)

0 Members and 1 Guest are viewing this topic.

EchoLynx

    Topic Starter


    Rookie

    Thanked: 1
    PHP include() Not Working [PHP Level: Newbie] [SOLVED]
    « on: September 25, 2009, 05:22:32 PM »
    First off, I'm a newbie to making anything from scratch - I've been brainwashed with CMSs. Please be patient with me.
    Next, I'm a big fan of the strict doctype. Please don't recommend hacks that will invalidate a webpage using the strict doctype.
    Finally, I am trying to make this website from scratch. Please don't re-post the code with all the corrections made - I would much rather you described the problem. If credit is an issue, please let me know!
    Now that's established, the question!
    --
    According to W3Schools, the following include should work.

    index.html
    Code: [Select]
        <!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">
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="style.css" />
    <title>EchoLynx.com</title>
    </head>
    <body>
    <?php require("header.php"); ?>
    <div class="maintext"></div>
    <?php require("footer.php"); ?>
    </body>
    </html>

    header.php
    Code: [Select]
       
    <!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">
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
    <div class="header"><img src="http://www.echolynx.com/main/images/header.gif" alt="Header Image"/> </div>
            <div class="menubar">

    <ul>
    <li><a href="about.html">About</a></li>
    <li><a href="ham.html">Amateur Radio</a></li>
    <li><a href="clstr.html">Computer Cluster</a></li
    </ul>
    </div>
    </body>
    </html>

    The footer is similar to header; <body> contains a copyright statement.

    For whatever reason, the source for index.html ends up rendered as follows.
    Code: [Select]
        <!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">
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="style.css" />
    <title>EchoLynx.com</title>
    </head>
    <body>
    <?php require("header.php"); ?>

    <div class="maintext"></div>
    <?php require("footer.php"); ?>
    </body>
    </html>
    Nothing but whitespace is displayed in IE or FireFox.

    Any ideas as to what could be wrong?

    Thanks in advance,
    Ian
    « Last Edit: September 25, 2009, 06:39:46 PM by EchoLynx »
    Ian

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: PHP include() Not Working [PHP Level: Newbie]
    « Reply #1 on: September 25, 2009, 05:49:35 PM »
    I can not give you help on PHP.
    But I have some questions.
    What have you got, so far, that works good?
    Why do you need to have an include?

    From My own past experience as a commercial programmer, I only use includes when I am very confident that my code is working well, I use the includes to make my work load easier on a very a large project. Otherwise, I do not use includes on anything that I have trouble with.

    When I try to learn a new coding tool, I break everything down into the smallest possible objects and still have the thing work. Also, when working with server side tools, I tried to do it on a local server in the same room with me until I am sure the code is good. Bur is this what you are doing?

    I avoid doing anything from scratch until I am confident that I know all the basics and have a measure of success understanding how others do coding teaks. The only time I do any PHP code is when I need to modify somebody 's code because it has a feature That I need to change.

    I understand you desire to do it from scratch. Yes, is a noble ambition. The best I can recommend is break you code down into small bits that can be tested one step at a time.

    Another thing. I see you have an index.html that has php code. Thar is not allowed, I think, on my server. If I have an index.html, it must be only  html. To get the thing going in PHP I had to rename the index.htnml file and let the browser load the index.php file. My server is running Apache on Linux and that is just the way it works.

    Hope this is of some help.

    EchoLynx

      Topic Starter


      Rookie

      Thanked: 1
      index.html > index.php
      « Reply #2 on: September 25, 2009, 06:39:22 PM »
      Geek-9am, you are obviously very experienced in your profession, and I appreciate your advice immensely.

      The purpose of using the include was ease of maintenance of my website. I had built the basic framework (which wasn't much in the first place) and then attempted to use includes, as they were (and I guess still are) new to me. (Unfortunately, I learned this technique the hard way, and I'm sure you know what I mean.)

      As for testing on a separate server, if find it's easier to simply create a subdirectory/domain one the same server/hosting account rather than trying to match the configurations on your live server with your local test server. Regardless, I hadn't been doing either, which was part of my problem. The other part was I'd tried switching the header file to .php, but hadn't thought to switch the index file. Even if I had, unless I'd uploaded it to my server I wouldn't have seen the change, as I'm simply viewing local files in my browser.

      So that solved my problem - index.html had to be index.php, and the file had to be hosted in Apache for it to work. Thanks again for your help.
      Ian

      kpac

      • Web moderator
      • Moderator


      • Hacker

      • kpac®
      • Thanked: 184
        • Yes
        • Yes
        • Yes
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 7
      Re: PHP include() Not Working [PHP Level: Newbie] [SOLVED]
      « Reply #3 on: September 26, 2009, 10:54:27 AM »
      So that solved my problem - index.html had to be index.php, and the file had to be hosted in Apache for it to work. Thanks again for your help.
      Just another note: this might not work on all servers.

      Code: [Select]
      <?php require("header.php"); ?>can sometime errors if the file can't be found.

      So, to solve this (presuming "header.php" is in the same directory as the file where this line is located), use this:
      Code: [Select]
      <?php require(dirname(__FILE__) . "/header.php"); ?>

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: PHP include() Not Working [PHP Level: Newbie] [SOLVED]
      « Reply #4 on: September 26, 2009, 11:01:59 AM »
      Quote
      "/header.php"
      should be:
      Quote
      "./header.php"

      ./ means the same directory
      / is the root

      This rule even applies to Windows servers, not just Linux.

      We had this discussion somewhere else recently.

      kpac

      • Web moderator
      • Moderator


      • Hacker

      • kpac®
      • Thanked: 184
        • Yes
        • Yes
        • Yes
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 7
      Re: PHP include() Not Working [PHP Level: Newbie] [SOLVED]
      « Reply #5 on: September 26, 2009, 01:26:56 PM »
      Quote
      ./ means the same directory
      / is the root
      Errr, it's the other way around. Anyway, this has nothing to do with the include or require functions.

      Code: [Select]
      <?php require(dirname(__FILE__) . "header.php"); ?>This would give an error, saying that "home\site1\public_html\folderheader.php" could not be found. The dirname function gives the name of the directory without a \ after the directory name.