Forum Moderators: coopster

Message Too Old, No Replies

php includes

         

gulliver

1:55 pm on Sep 9, 2004 (gmt 0)

10+ Year Member



New to this, I'm trying to use php includes to make site build/change faster/easier for me. Content is static - not subject to frequent change.

I've a few 'do I understand/have I got this right queries?'

My .html pages are now renamed to .php.

Previous hard-coded elements have been removed and saved to a text file (example: file.txt) - to be called with this code:

<?php include('file.txt');?>

Seems good to me.

Instead of previous code like
<a href="/" title="load the front page"><img src="/graphics/logo/logo.gif" width="208" height="50" alt="logo" /></a>
I now have <?php include('logo.txt');?>

Smaller page. Easier to understand.
And if I change the logo/link/whatever I just need to change the .txt file and not individual site pages. Why did I not do this before? Duh.

Question...
I've also seen .php, .inc and .htm or .html used instead of .txt.
Why? What's the difference? Advantages?

Where the include files are in a different directory to the webpage, I'm having some issues with naming the path.
I prefer to 'work with a leading slash' - so everything works relative to the root. This way, in avoiding relative nonsense like ../../ my links work and images get called from the correct place.

Doesn't work with includes though - presumably using a local path like /homepages/[username]/htdocs/. Ugly. Cumbersome. Alternatives?

Relative like ../ doesn't work. Good - too prone to error.
Absolute with [[domainname].com...] works.
As does
include $_SERVER[DOCUMENT_ROOT]."/includes/file.txt";
Slightly better. Still not ideal.

Question...
Is there a way to config so's I can use a simple leading /? .ini or .htaccess possibly?

Question...
Do viewer's browsers cache the includes? Thereby speeding page load? And, once a complete page (with includes) has been loaded, is the whole thing cached?

Question...
If includes are cached, am I thereby saving bandwidth?

nbrandt

8:44 am on Sep 10, 2004 (gmt 0)

10+ Year Member



A couple of messages above StupidScript wrote:

>I still maintain the "go-getter" include() description.

>It's the server that does the parsing of the file when the request is made by the include(). The include(), itself does not parse the file.

Of corse, you are right ... I was too tired. (Just thought it was important to mention this to not cause confusion).

Nick

Zipper

9:07 am on Sep 10, 2004 (gmt 0)

10+ Year Member



>> My server chokes and spits an error message (Parse error: parse error, unexpected ';' in [page url] on line 4) if I use <? $root = $_SERVER[DOCUMENT_ROOT].;?>

It's the trailing dot. try,
<? $root = $_SERVER['DOCUMENT_ROOT'];?>

else, if it works with double slash, just leave it. ;)

gulliver

9:27 am on Sep 10, 2004 (gmt 0)

10+ Year Member



Thanks, Z. Appreciated.

That works.

Above the head element my html page now has:
<? include($_SERVER[DOCUMENT_ROOT]."/inc/config.php");?>

The document 'config.php' is:
<? $root = $_SERVER[DOCUMENT_ROOT];?>

Body includes are of the form:
<? include($root."/inc/head.php");?>
</head>

The extra / thing is odd. I've noticed some servers seem to ignore them and deliver the required page or directory regardless of how many extra ones are there. Odd. There again we are talking about 'computers and web' - so 'odd' is perhaps apt.

'I couldn't have done this without you'... thanks to all for the input. It's made sense of stuff I didn't understand - and the questions/answers here add to that which I'd found by looking around the web trying to figure this.

Appropriately editied, there's probably the basis of a sensible tutorial here. Hope others can learn, too.

Thanks, all.

;-)

toltec75

3:35 pm on Sep 10, 2004 (gmt 0)

10+ Year Member



I have some questions about include() myself
so I decided to post here instead of posting a new topic!

I wanna know how to make a separate file out of piece of code that does verification of users below so that I can just use include() function and put it there to do the same thing!

<?
session_start();

if ((empty($_SESSION['username'])) ¦¦ (empty($_SESSION['password'])) ¦¦ (empty($_SESSION['kid']))) {
echo "You are not authorized!";
echo "<p><a href=form.php>Login!</a></p>";
}

else {

// beggining of authorization code

$conn = mysql_connect("localhost", "root") or die(mysql_error());

$db = mysql_select_db("iwa2003_12", $conn) or die(mysql_error());

$sql = "select GRUPA.vrsta FROM GRUPA, OSOBE where OSOBE.KID = '$_SESSION[kid]' and GRUPA.GID = OSOBE.GID";

$result = mysql_query($sql) or die (mysql_error());
$row = mysql_fetch_array ( $result );

$vrsta = $row["vrsta"];

if($vrsta!= 0)
{
echo "You are not authorized";
}
else{

//end of authorization code


echo "<html>
<head>....

...";
}
}
?>

Thank you

Zipper

4:12 pm on Sep 10, 2004 (gmt 0)

10+ Year Member



toltec75,

It really depends on how your pages are structured..In the simplest way you redirect the unauthorized user to a seperate login page so that you have total control over the current page content.

However in this case, you can put your entire verification code in a seperate php file (except for the html part). It'll be helpful to introduce a new variable to identify whether the user has been authenticated. (I have used $userauth here)

this will be your include page (with your actual code), lets call it userauth.php

<?
session_start();

$userauth = 0; // user authentication failed/incomplete

if ((empty($_SESSION['username'])) ¦¦ (empty($_SESSION['password'])) ¦¦ (empty($_SESSION['kid']))) {
echo "You are not authorized!";
echo "<p><a href=form.php>Login!</a></p>";

} else {

// beggining of authorization code
// db connection & sql query

$vrsta = $row["vrsta"];

if($vrsta!= 0) {
echo "You are not authorized";
} else {
$userauth = 1; // user authenticated
}
}
?>

and in your content page(s), you can use it in the following way,

<?
include("userauth.php");

if($userauth == 1){
//here you can fill the page with html code, probably nesting php tags
}
?>

Hope u got the idea.

StupidScript

6:17 pm on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Zipper:
So have you ever got include() to work with "/" within a directory tree?

Throughout all of my web directories, including virtual hosts. The leading slash indicates the docroot root or the vhost root dir with equal success.

In fact, I have even used it like this modified example:

Docroot: /var/www/html/
VHosts: /var/www/sites/
Docstore: /var/docstore/inc/

From docroot:
include "../../docstore/script.php";

From subdirs of vhosts:
include "../../../docstore/script.php";

script.php can also include "inc/dbstuff.php" from within docstore and return correct behavior to docroot or vhosts.

Running RedHat7.3/PHP4.3.3/Apache2.0.47

Zipper

6:55 pm on Sep 10, 2004 (gmt 0)

10+ Year Member



Interesting... It's probably in the php.ini doc_root config. under Paths and Directories. You can also explicitly set the include_path. Unfortunately those who are on shared servers are unable to change this.

[edited by: Zipper at 7:02 pm (utc) on Sep. 10, 2004]

gulliver

6:58 pm on Sep 10, 2004 (gmt 0)

10+ Year Member



>Throughout all of my web directories, including virtual hosts.

I wish it'd work on mine. Could have saved a lot of trouble.

<start snigger>I'm on a 1and1 package</end snigger>

;-)

Zipper

7:10 pm on Sep 10, 2004 (gmt 0)

10+ Year Member



Just checked. It's indeed with the doc_root [php.net] configuration. By default it's NULL, In your case it's "/". That's solved I guess. ;)

StupidScript

8:27 pm on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for digging the answer out, Zipper! :)

toltec75

8:41 pm on Sep 10, 2004 (gmt 0)

10+ Year Member



thanx Zipper, you`re the man!
it works great! :-)
This 41 message thread spans 2 pages: 41