HELP! Need help with "include" function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sevi27
Forum Newbie
Posts: 3
Joined: Thu Apr 10, 2003 9:49 am

HELP! Need help with "include" function

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
sevi27
Forum Newbie
Posts: 3
Joined: Thu Apr 10, 2003 9:49 am

Post 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??
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post 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.
sevi27
Forum Newbie
Posts: 3
Joined: Thu Apr 10, 2003 9:49 am

Post 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
skyeflye
Forum Newbie
Posts: 1
Joined: Thu Apr 10, 2003 11:55 am

Post 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.
Post Reply