Forum Moderators: coopster
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?
>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
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.
;-)
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
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.
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