Page 1 of 1

HELP! Need help with "include" function

Posted: Thu Apr 10, 2003 9:49 am
by sevi27
Hello all,

i am new to PHP and I am trying to use the "include" function in an HTML page and can't get it to work. Basically I have a file called "test.php" that I have uploaded to my ISP. test.php just reads some text from a text file ( I have verified that my ISP supports PHP4). I then created an HTML file in which I placed this line and also uploaded....

<? include("test.php"); ?>

when I go to the HTML page from my browser I get nothing. No error messages or anything. However, if I create a link to the PHP file from the HTML page, it works; I can see the text outputed from the textfile. It also works if I access the test.php file directly (ie. http://www.mydomain.com/test.php). I have tried changing permissions and using an actual path ("/path/to/file.php"). WHat am I doing wrong? Could it be something related to my ISP? Any help would be appreciated.

Thanks,
Sevi27

Posted: Thu Apr 10, 2003 10:12 am
by twigletmac
Does your ISP support short tags (<? ?>)? Have you tried using long tags:

Code: Select all

<?php include 'test.php'; ?>
What do you get if you view the source of the page?

Mac

Posted: Thu Apr 10, 2003 10:32 am
by sevi27
Thanks for the reply. I tried using long tags with the same result. When I view the source of the page it shows the HTML code and the <?php include 'test.php'; ?>. I have placed the include within the body of the HTML page. This is correct right??

Posted: Thu Apr 10, 2003 10:49 am
by werlop
I am trying to use the "include" function in an HTML page
Has the page with the include function got a .php extention? Just a guess sorry if im talking my usual crap.

Posted: Thu Apr 10, 2003 11:03 am
by sevi27
well, that worked. instead of using the .html extension for my page, I changed it to .php. All my other html code works as well as the php include function. Is this the way it is supposed to work?? This is the first I've heard of doing it this way, but then again I am very new to PHP. Thank you.

Sevi27

Posted: Thu Apr 10, 2003 11:55 am
by skyeflye
It was not working before because, according to your server, the "include" function was not inside a ".php" file.

You had it inside a ".html" file, therefore the server did not execute the PHP code you had inserted, it just served up the ".html" file and probably treated the PHP code you had on there as plain text to just show on the page.

The ONLY way a typical web server will know whether or not to execute PHP code in a file is if the file has the correct extension (such as ".php").

There are ways to make it so that files with a ".html" extension get executed through the PHP module as well, but usually you need access to edit the "php.ini" or the apache configuration file. Also, although it probably will not cause any problems, it is slightly inefficient to parse/execute every single ".html" file through the PHP module whether or not it has PHP in it or not. I have done it on some sites though and the performance hit is negligible unless you have tons (TONS!) of traffic.