Forum Moderators: coopster

Message Too Old, No Replies

using header includes - how to write it

         

J64sqs

2:35 pm on Apr 5, 2005 (gmt 0)

10+ Year Member



I've decided to start using php includes for my header and footer on my site. But I've seen lots of tutorials on the web, and they use slightly different words. What is the difference between using 'include' verses 'include_once' or 'require'?

Also, I notice that if I do something like
<?php
include /includes/header.php;
?>
it won't work.
But if I take away the first slash (includes/header.php), it works. Why is that? I mean, shouldn't all absolute links (that leave out the mydomain.com part) start with a slash?

sifredi

2:43 pm on Apr 5, 2005 (gmt 0)

10+ Year Member



shouldn't all absolute links (that leave out the mydomain.com part) start with a slash?

no.

tomda

3:00 pm on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not really...

From what I know, your server will check in the current folder (where your php file which call the include is located), if not found, it will check from the root.

The thing is to set yourself the location of your ionclude folder by using php_ini command


$http_dir="your_domain_name";
$docserver_root=str_replace("/","\\",$_SERVER['DOCUMENT_ROOT']);
ini_set("include_path", ".;$docserver_root."\\".$http_dir."\include");

Why I had to replace "/" by "\\" and why there is a ".;" in my ini_command is still a mystery though, but it works

It is possible that your host do not authorise you to modify you ini setting, in such case get in touch with them to know about the current setting or go to your info.php page.

Then, when you call an include, you just type

include_once ("my_include.php");

and your server will know where to look for.

About the diffenence between include_once, include and request_once, do not remember... I always use include_once though.

someone

10:02 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



include() and require() are identical in every way except how they handle failure. include() produces a warning. A warning is printed by default, but it doesn't interrupt script execution. If you're trying to include a file that doesn't exist, you will see a warning message on your page, but your page will continue to execute until the end.

require() produces a fatal error. An error is also printed by default, but it interrupts script execution. Besides seeing a warning message on your page, require() also halts your page's execution after the require() function returns. So even though the page is supposed to display a table after the require() part, you wouldn't see the table, because the execution has already been halted by require().

include_once() and require_once() work the same as include() and require() except that they guarantee that the file has only been included once.

If you have the following code in the same page:

include_once("a.php"); //includes a.php
include_once("a.php"); //doesn't include a.php again since it has already been included.

The above explanation is found at php.net.