PHP includes - help needed

He has: 3 posts

Joined: Mar 2005

I spent the last few months learning web stuff, and i have done ok with my building my first site... but i am doing all the work and writing myself, and i really need to figure out how to use php-include functions. i php'd my blog into the page,but i cant just cant figure this out, and the biggest lesson i have learned is that it sucks to change the same thing on 50 pages.

If you could check out 2 pages and see if there is a short answer to this, i would appreciate hearing it: the file im trying to "include" is a htm file, but i have also tried a .doc file but that was even worse:

anyway, id like to "include" the links at the top for now first... i know im probably just missing a stupid step, but ive tried for too long already on it

PAGE WITHOUT CODE and PAGE WITH CODE ATTEMPT

I appreciate you taking the time,

thanks
michael

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

OptionBlock, how are you including the page? It would help if we saw the relevant code on test.php

Just for reference, there are a few ways to include pages in another .php page:

<?php
include(\"file.html\");
include_once(\"file.html\");

require(\"file.html\");
require_once(\"file.html\");
?>

If you want to get a bit more complicated, you could check to see if the file exists before including it.

<?php
if(file_exists(\"file.html\")){
include(\"file.html\");
} else {
echo \"File 'file.html' does not exist\";
}
?>

They have: 2 posts

Joined: Jun 2005

Also, while I am not positive about this, but I believe the "require" oiption is set for deprication from php isn't it? I might be wrong there, but I thought I read that recently. If proven to be true, using only include or include_once would be the best course of action.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Well right now it looks like that on the page where you tried the code, you are telling it to include a .PNG file. The reason it is messed up is becasue it is trying its best to display the CHARACTERS in the file.

Both INCLUDE and REQUIRE take the file you specify, and read it like it was part of the current file. Therefore, you cannot just say something like include ('myresume.doc'); and then expect to see a Word document formatted on the screen. It will take the file as it sees it (open it with a text editor such as notepad) and send it straight to the browser.

The included file is EXPECTED to be a valid HTML and/or PHP file. Note that when the included file gets processed, it starts out with HTML, not PHP, so if you have PHP code in the included file, inside the included file you need to start off with <? and end with ?>

Lastly, make sure that you do not have one "complete" web page include another. By complete, I'm referrign one that has all the proper tags, only one of the files should have this.

See http://www.php.net/include/ for more information and examples. From this page I didn't see anything about require not being valid anymore. It is not the exact same as include anyhow. A failed include gives a warning, a failed require errors out.

-Greg

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

oh man, I see the problem I think, you're trying to include an image. "‰PNG  IHDR!k.)ÅósRGB" that not how to do it.

php include's purpose is to include other php files, so you can write code and functions once, and include them on multiple pages. What you should do is have "image_include.php" that contains then include that in all the pages.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.