Results 1 to 29 of 29

Thread: Including a header php page *finished*

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Including a header php page *finished*

    I'd like to include a header.php page to serve as the top part of every php page I make. I'm aware that I can use include("header.php"), but I have a question...

    Is include() the right way to go about it?


    In ASP, I use the simple

    VB Code:
    1. <!--#include file="topinclude.asp"-->

    Is it better to use this, or include()?

    Any better ways?

    Tx.
    Last edited by mendhak; Jan 30th, 2003 at 12:46 AM.

  2. #2
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Yep I think include is fine. The other alternative is require which will stop execution if the file is not found, whereas include will carry on.

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    erm...

    include()

    or

    <!--#include-->

    ?

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by mendhak
    erm...

    include()

    or

    <!--#include-->

    ?
    include()

    <!--#include--> is SSI and would not work in PHP, unless you echoed it as an SSI.

    include() is what I use. I've never had a problem with it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Thanks man.

  6. #6
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    Originally posted by Rick Bull
    Yep I think include is fine. The other alternative is require which will stop execution if the file is not found, whereas include will carry on.
    but, if include didn't find the file it will give you a warning. and still carry on. stil has an error so I think it would be the same as require.

  7. #7
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Yep, but you can put an @ before it to surpress the error can't you? @include('file');

    And sorry Mendhak, wasn't reading properly

  8. #8
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    yes you could, but getting in the habit of surpressing warnings or errors is a bad idea as the error or warning is there for a reason and should be taken care of.

    the only time you should add a @ in front of something is if you make your own die() function after it.

  9. #9

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I have another question:

    I'm currently using

    PHP Code:
    include("../header.php"); 
    The above code goes into a file that is in a folder in the root folder, and is including a header.php file that is in the root.

    However, when I try to use

    PHP Code:
    include("/header.php"); 
    to specify the same header.php file in the root folder, it won't work, and instead gives me an error message. I want to use this second piece of code instead of the first, since it will save an awful lot of work for me.

    Any help?

  10. #10

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I tried


    PHP Code:
    <?php 
    include("$DOCUMENT_ROOT/header.php"); 
    ?>
    But I'm getting errors. I'm at the point of total confusion.

    Any help at all appreciated!

  11. #11
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    do the full path

    /usr/path/to/file.php

    no ../ or http:// addys

    also if you are using document_root you may need to use the $_SERVER['DOCUMENT_ROOT'] as register_globas is off.

  12. #12
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Thanks phpman, I thought my server didn't support absolute paths, but it does now that I find the full path (using realpath()).

  13. #13

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by phpman
    do the full path

    /usr/path/to/file.php

    no ../ or http:// addys

    also if you are using document_root you may need to use the $_SERVER['DOCUMENT_ROOT'] as register_globas is off.
    $_SERVER['DOCUMENT_ROOT'] wouldn't work for me, but I do have register_globals on. The strange thign is, I tried

    include("/header.php");

    and it works fine on MY pc (I have PWS configured to execute PHP pages), and on my server, I have to upload it with

    include("$DOCUMENT_ROOT/header.php");

    for it to work.

    If I interchange these, I get errors. That is what I was wondering about.

    Rick, I tried realpath() and it returns C:/ as the root!!!

  14. #14
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    the best way to do test scripts is to make your test server that exact same as your host server. in this case it sounds like your host has register globals off (which it should be this way for security reasons.) also it might be that they have apache too.

    if you get your test server setup like your host then the less changes you have to make when you upload

  15. #15
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    And did you use realpath on the include bit, or the DOCUMENT_ROOT? It should have been this: include(realpath("/header.php"));

  16. #16
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by mendhak
    $_SERVER['DOCUMENT_ROOT'] wouldn't work for me, but I do have register_globals on.
    What version of PHP are you running? If it's older than 4.2 (or maybe even 4.1), then the superglobals aren't available.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  17. #17
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    True, I am using the superglobals and I am on 4.1

  18. #18

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Er... how do I tell what version I'm using? :embarrased:

  19. #19

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by Rick Bull
    And did you use realpath on the include bit, or the DOCUMENT_ROOT? It should have been this: include(realpath("/header.php"));
    Yeah, and it was searching for header.php in c:\

    I have a feeling that this is due to the version differences. I have the PHP binary which 'auto configured' itself for PWS. The server runs on Linux, and uses Apache....

  20. #20
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    make a file and call it wahtever you want, info.php

    then ad this to it and run it

    <?php
    phpinfo();
    ?>

    that is all you put in the file.

  21. #21

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    4.3.0 !!

    I'll put up a link to the HTML page in a while, so you can look at it...

  22. #22

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I ran phpinfo() and I saved the page as html, uploaded it here:

    http://mendhak.worldjunkie.com/phpinfo.html


    Then I uploaded the php page onto the server itself, so this link shows the SERVER configuration:

    http://mendhak.worldjunkie.com/chicken.php

  23. #23

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    doc_root no value no value
    Could that be the problem?
    Last edited by mendhak; Jan 29th, 2003 at 05:06 AM.

  24. #24
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    does it not work just

    include('header.php');

    it's in the same directory as the file which is including it isn't it?

  25. #25

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by da_silvy
    does it not work just

    include('header.php');

    it's in the same directory as the file which is including it isn't it?
    header.php is in the root directory. Any php files in the root directly using include() work just fine. The problem comes up when I'm trying to include it from a php file in a subdirectory. That's why you see so many $DOCUMENT_ROOTs floating around in this thread

  26. #26
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    ok I see a lot of differences.

    for one you have 4.3, your host has 4.2.3 (big differences)

    you host has register_globals OFF, you have them ON

    you have it setup as cgi, your host is setup as a module. that will be a difference right there.

    here is your path. (on host)
    /home/butterfl/public_html/mendhak/chicken.php

    so when you inlcude something that is NOT in the root it will be like this.

    include("/home/butterfl/public_html/mendhak/folder/chicken.php");

    so basically that will work like this

    include("folder/chicken.php");

    samething.....

    the fact that you don't have a document root is bad. you should have one. I believe your install is all messed up.

    what server do you have?

    hope you didn't install that Triad thing. tht is the biggest pile of dog dodo

    for one, on your test machine you shouldn't have virtual directories enabled, no need for them. you shuold have globals OFF, you should have doc_root

  27. #27

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by phpman

    you host has register_globals OFF, you have them ON
    If I register_globals OFF, will I still be able to access the $POST and $GET info from forms?

    the fact that you don't have a document root is bad. you should have one. I believe your install is all messed up.


    what server do you have?
    I downloaded the PHP binary from php.net, and chose the option to configure it for PWS. How can I have a document root now?
    Is it possible in any way? Or should I continue with the different include() for both machines?

  28. #28
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    $_POST and $_GET are used when globals are OFF. if you had globals ON then you can just acces the variables without the POST or GET.

    I would download apache. not sure where in PWS is the config (or if it even has one) to adjust the doc_root. Apache is your best bet as your host has it too.

  29. #29

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Alright.

    Thanks to everyone for the input and for the patience they've shown here.


Posting Permissions

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



Click Here to Expand Forum to Full Width