Site Reference Forums

You are not logged in.

#21 2007-12-31 10:25:06

Steven_A_S
Member
From: San Antonio, TX
Registered: 2006-08-07
Posts: 440
I've been thanked 21 times.

Thank me Website

Re: Includes

Laurie,
Is the extension on your display page .php or .htm/.html?  It needs to be .php so that it goes through the PHP processor before getting sent out to the browser.  There's ways to get around this, but I don't think we need to make this more difficult than it is.

Offline

 

#22 2008-01-03 08:57:08

griffinsbridge
Member
From: York, England
Registered: 2005-11-04
Posts: 595
I've been thanked 11 times.

Thank me Website

Re: Includes

Just to stick my oar in!!

There's somethings you'll need to consider when using includes:

1. most servers won't allow you to include a remote file from another server. Similarly, most servers won't allow a file to be remotely included (make sense?)

2. You can include any kind of file (pretty much) but it's best to stick with php, txt or html files. including Javascript isn't as quick as the standard way (and bloats the outputted html code) and why anyone would bother including an image, i don't know!!

3. It's possible to include within a .html file using the same code by making alterations to your .htaccess file.  you would tell your server to treat all HTML files (files with a .html extension) as php files. Personally I wouldn't though. You can get a similar effect (ie, .html extension in your URLs) by using a proper php file and using .htaccess mod rewrite to change the extension on the fly.
Search keyword: mod rewrite

4. There are 4 kinds of includes. They're all used the same, but have slightly different effects. eg:

this code in test.php:

Code: php

<?php include("file.php")?>

include() includes the file. If the included file is missing, there'll be an error in place of the include, but the file (test.php) will continue to execute after the include line.

Code: php

<?php require("file.php")?>

require() includes the file. If the included file is missing there'll be an error in place of the include, AND the file (test.php) will STOP executing at the include line.

Code: php

<?php include_once("file.php")?>

include_once() includes the file. During execution of test.php, include_once would check to see if file.php has already been included. if it has, it'll ignore the new request and carry on executing test.php. however, there will be an error where the include_once statement is.

Code: php

<?php require_once("file.php")?>

require_once() includes the file. During execution of test.php, require_once would check to see if file.php has already been included. if it has, it'll ignore the new request and STOP executing test.php where the require_once statement is.


So, 4 ways, how do you know what to do?

easy peasy really. if you're including a static html file, test using include(). Once the page is ready for the public, change to include_once()
If you're including a PHP file, especially if it's connected to your database or has other security issues, use require() to test (helps with debugging) and switch to require_once() once live.

Always go public with _once() or you might find a hacker can add a string to the URL and reinclude just about anything!

If your included file contains functions, classes or any other kind of PHP working out, it's best to include it BEFORE any html output (ie, before the HTML tag).
You can include a file that creates a variable, then echo out that varibal in different ways too:

eg:

Code: php

<?PHP echo $variable; ?>

Code: php

<? echo $variable; ?>

Code: php

<?PHP echo "$variable"; ?>

Code: php

<?= $variable; ?>


The last one is my personal fav.

Anyhoo, hope this gets the creative juices flowing alittle in the New Year!!
Well done for making the leap to PHP, you'll never look back!


So many pages, so little time.

Offline

 

#23 2008-01-03 09:45:01

Northie
Moderator
From: Yorkshire, UK
Registered: 2006-08-19
Posts: 2578
I've been thanked 62 times.

Thank me Website
Buy me a beer

Re: Includes

I'd just like to haul you up over your definitions

require_once() includes the file. During execution of test.php, require_once would check to see if file.php has already been included. if it has, it'll ignore the new request and STOP executing test.php where the require_once statement is.

The first part is true - php will check to see if the file has already been included.

If the file has been included, it will not include it again and will continue to execute
If the file has not been included it will try to include it.

any kind of php include will throw an error if the file cannot be found and continue to execute

require has a different meaning so execution will stop (fatal error) if the file cannot be found

_once is to ensure you don't have multiple includes of the same file and is there to prevent problems that can occur such as server load and duplicate function names

you can do

require_once("file.php");
require_once("file.php");
require_once("file.php");

without error

this usually happens if you try to include more than one file, one of them including the other one

eg

include("file1.php");
include("file2.php");

yet file2.php already includes file1.php

    /********************************
     *
     *    A Side Note
     *
     ********/



HTML does not do any processing - therefore you cannot include HTML documents using HTML - you would need a server-side scripting language like PHP, ASP, Ruby, etc etc


Now taking free-lance inquiries; Please contact me for more details
Xeneco - My Internet Ramblings
Web 20 - Web Apps
Nothing's Impossible, just let me think about it for a while....

Offline

 

#24 2008-01-05 00:27:26

laurie_m
Member
From: Bega, Sapphire Coast Australia
Registered: 2005-08-18
Posts: 1339
I've been thanked 30 times.

Thank me Website
Buy me a beer

Re: Includes

Thanks Fellas.

Getting just a bit complex for this country lad.

Got me worried about the security issues.

Regards,
Laurie.


John McDouall Stuart - Explorations in Australia
First Expedition Second Expedition Third Expedition Fourth Expedition

Offline

 
Never
Sponsored Results


Board footer

Powered by PunBB
© Copyright 2002–2008 Rickard Andersson