Forum Moderators: coopster
Ideally, I'd like to use my existing HTML skills and add snippets of PHP codes that I've just learnt. I know there is a steep learning curve, so I plan to take it step-by-step. However I do have some questions about PHP and here they are:
1) Is PHP well supported with browsers like IE 6-7, Firefox 1-2, and Safari, and Opera?
2) Will there be any conflicts in using stylesheets to control HTML tags?
3) Is it better to use SSI or PHP's include command?
Appreciate your feedback.
Is PHP well supported with browsers like IE 6-7, Firefox 1-2, and Safari, and Opera?
Will there be any conflicts in using stylesheets to control HTML tags?
Is it better to use SSI or PHP's include command?
Basically, as far as I understand PHP, its a just the same as HTML it just that it will allows you to create dynamic pages, plus you will be able to control a lot of things, let say you want to display a certain content to your visitor if he is using IE, but will display a different content is he is using FF. Then display a different content if he is from US compared to the content of visitors from Asia. Plus, plus plus...
[edited by: Gian04 at 3:04 am (utc) on July 16, 2007]
expose_php=Off
If you use Apache you can run PHP on specific HTML/XHTML/XML files with
LoadModule phpX_module path-to-libphpX.so
<FilesMatch A.html¦B.html¦C.html ...>
Sethandler application/x-httpd-php
</FilesMatch>
You'll want to put the <FileMatch ...> in each virtual server.
Doing both allows you to hide PHP from your users completely. As far as they're concerned it's just another web page. They don't know it's a dynamic web page create with php.
the only way they would get to see the underlying PHP code is if you messed up the server settings.
website.com/php/header.php
website.com/php/footer.php
website.com/php.copyright.php
etc...
1) How do I reference these files? Do I type out the full path everytime or is there a shortcut?
2) Which require command do I use - require (); or require_once ();
3) I saw the following command in another post for referencing a file.
include($_SERVER['DOCUMENT_ROOT'] . '');
Can this command be used as a short cut to reference the PHP folder mentioned above? If it is how do I reference the files? Is there a require version for it?
<?php include("website.com/php/header.php");?>
<?php include("../php/header.php");?>
You can use "require" instead of "include" which saves a little processing time because (I believe) the syntax of the include is not checked every time it is called.
<?php require("website.com/php/header.php");?>
It is common practice, but not essential, to name all includes as .inc. E.g. It also might be best to avoid the term "php" as a directory name.
<?php require("website.com/includes/header.inc");?>
website.com/php/header.php
website.com/php/footer.php
website.com/php.copyright.php
etc...
I personally think it is simpler to keep all the html in one template file. Then in each page you only have to specify the page specific items as php variables. You can then change the layout of the entire site by tweaking the html in the template and the css stylesheet.
The example below is a much-simplified version of what I use. I also use other variables to specify such things as alternative indexes, whether or not to display ads, etc. It's similar to a database system except I am using flat files.
page.php
<?php
$title = "Page title";
$descrip = "Page meta tag description";
$keywords = "kw1 kw2 kw3";
$content = <<< eof
Page-specific content goes here
eof;
// $content can include text, HTML tags, images, etc.
// You can also use full quotes.
require("www.domain.com/includes/template.inc");
?>
template.inc
<?php
// set default for variables (in case one gets forgotten!)
if (!isset($title) ) $title = "";
if (!isset($descrip) ) $descrip = "";
if (!isset($keywords) ) $keywords = "";
if (!isset($content) ) $content = "";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Language" content="en" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php print $title;?></title>
<meta name="Description" content="<?php print $descrip;?>" />
<meta name="Keywords" content="<?php print $keywords;?>" />
<link href="www.domain.com/css/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<!-- HTML -->
<?php print $content;?>
<!-- HTML -->
</body>
</html>
Just to reiterate: the file extension of an included file doesn't matter; the contents of an included file are run as if the extension was .php.
Please though, for the sake of whoever may need to work on the site in the future (including you ;)), make sure the extension accurately reflects the content type of the file!
[edited by: WesleyC at 3:03 pm (utc) on July 19, 2007]
Just to reiterate: the file extension of an included file doesn't matter; the contents of an included file are run as if the extension was .php.
Are you sure about this? I saw a tutorial that said everytime you use the include/require file, PHP will by default read and treat that file as HTML - even if the file extension was saved as .php. He goes on to say that you should name the file as .php and use the <?php?> tag if the file contains php codes.
It is good to have a parsed extension on an included file so that if the included file is called directly people will not see the source.
As far as parser tags go it may depend on where in the including file you call it. I do believe though that I use parser tags in included files all the time. It's one of those things I never think about until someone asks.