i have a lot of pages, most all have head sections and can 'stand alone'.
Also, most of them get 'included' in 'larger' documents or articles.
So, in a, lets call it a 'big page' I could have 3 or 4 included pages, all having their own head information.
Is there a fancier way to include a 'head.html' with all the meta's, style etc, BUT only once so that if the 'parent', lets say index.php has already 'included' 'head.html' the inclusion of say, specialcharacters.html will Not also load a head, but if I were to load specialcharacters.html on it's own, it Would 'include' the 'head.html'????
(exp: index.php includes, nav.html, nav_r.html, header (logo, welcome etc), footer.html, body01.html, specialcharacters.html, etc.. BUT, i want to use specialcharacters.html as a stand alone document with head, style etc for doc formating Also.)
so, some kind of include if... so head.html is only included Once.
I hope that is relatively clear..
Thanks you, in advance,
Landis.
landisreed dot com/index.php - head.html
I suppose you can use
include_once 'header.html';
Then if it was included before it won't be included again.
This said you would have to include header information into every file, so your specialcharacters.html will have to use it as well as body01.html. Then whichever include_once first - there header.html will appear.
EDIT:
To differentiate titles or other information you can do as follows in header.html:
<title><?=$title;?></title>
And then in every of your scripts
$title = 'Whatever';
include_once "header.html";
Now, whoever calls header first will set the $title first and render it inot the header. Once its rendered into header, subsequent changes of $title by any other includee will simply be ignored by your page.
have you tried using 'include_once'? http://www.php.net/manual/en/function.include-once.php
Example:
include_once "header.php";
include_once() is what you can use, according to PHP's documentation
The include_once statement includes and evaluates the specified file
during the execution of the script. This is a behavior similar to the
include statement, with the only difference being that if the code
from a file has already been included, it will not be included again.
As the name suggests, it will be included just once.
more information here http://in3.php.net/include_once
If I got the question right, I think what you need is a single Header file header.php to be included in all pages with require_once. The trick isto put all different kinds of heads you have , like one for head.html and one for specialcharacters.html, into the header.php file separated by if statements. The header.php might look like this :
if ($caller == 'head') { // HTML for head.html}
elseif($caller != 'head' && $caller == 'specialcharacters')
{ // HTML specific to specialcharacters.html for standalone viewing}
Once this header.php is written with all conditions in place, you need to set $caller accordingly at the top of each file (e.g. for specialcharacters.php first line of code should be $caller = 'specialcharacters';. Then include header.php to all files as require_once("header.php") just after specifying $caller.
EDIT
Your index.php file will look like this:
$caller = 'in_my_index';
require_once('header.php');
Your specialcharacters.php file will look like this:
$caller = ($caller != 'in_my_index')?'in_my_specialchars':'in_my_index'; // This is to make sure that when specialcharacters.php is included inside index.php then it should still show index.php title but when loaded standalone, it will show its own title.
require_once('header.php');
Now your header.php looks like this :
<html><head>
<?PHP if($caller == 'in_my_index') { echo '<title>I am Index Title</title>';}
elseif($caller == 'in_my_specialchars') { echo '<title>I am standalone Specialcharacters.php Title</title>';}
?>
This should give a better idea I hope.
Hope this helps!
Related
for my website i have some static header/footer HTML and some dynamic content generated by PHP. When it comes to render the output I just include a file with HTML inside from my PHP code. This works perfect - also when I switch between pages.
<?php
...
public function render() {
...
// file for output
include $fileName;
...
}
?>
But I also need some header.hmtl and footer.html that contains the static information (text and some divs for formating) and want to put that in front of each dynamic content, represented by $fileName.
So I simply add two includes that represent the static information.
// file for output
include "./Views/html/header.html";
include $fileName;
include "./Views/html/footer.html";
So this does what I like (formatting, etc.), but if I switch from page to page it flickers for one time. As much as I can see the page is first renderd without header/footer information and then a second time with header/footer information. Looks like this generates the flickering.
How can I avoid this ? Is this probable related to a RewriteRule of my MVC-Framework ?
Any hint is appreciated.
thanks to Lawrence who led me to the right term. It solved my problem completely.
I put a <body><script>0</script><!-- rest of the code --></body> in my code and it works now. –
I have 2 types of header on my website, so instead of creating 2 include files for each type of header, I want to create a single include file with an if/else statement that will display the right type of header to each page.
Here is the code for the "testpage.php":
<?php
$testpage="testpage.php";
$currentpage = $_SERVER['SCRIPT_FILENAME'];
?>
<!DOCTYPE html>
<html>
<head>
<title>testpage</title>
</head>
<body>
<?php include_once('include/header.php'); ?>
</body>
</html>
And here is the code for the include file "header.php":
<?php
if($currentpage==$testpage) {?>
<p>Woaw!!! You are a genius. you should work for NASA</p>
<?php }else{ ?>
<p>this doesn't seem to work. Try something else.</p>
<?php }
?>
So far, I've tried this:
$currentpage = $_SERVER['SCRIPT_FILENAME'];
$currentpage = $_SERVER['PHP_SELF'];
$currentpage = $_SERVER['REQUEST_URI'];
$currentpage = __FILE__;
I've tried on my XAMPP local server, and as well on my "bluehost" live server, and I just cant make it work.
Also, I created a small test on github, to test that, so if you want, you can take a look, here is the link: https://github.com/nobody7/test001
So, please let me know if you have any suggestions, any help will be highly appreciated.
Thanks
From http://www.php.net/manual/en/reserved.variables.server.php
$_SERVER['REQUEST_URI'] is the web address that the user inputted into the address bar minus the hostname and protocol (e.g. a request to http://example.com/my/web/site/123, will return /my/web/site/123), and is usually what people use to differentiate between different pages when using a Front Controller since front controllers mean all requests go through a single php script first.
$_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_FILENAME'] will show the filename of the initial script that you executed, they are pretty much the same thing. SO if you did a request to http://example.com/abc.php those 2 variables will return the filepath to abc.php (e.g. /var/www/abc.php) even if you executed this in an included file, it will show the filepath foe abc.php.
__FILE__ will show the current file you are in, this is similar to the above, but it will show the file path of the file you are currently in even for included files. So if you have a header.php that you have included inside abc.php, and you use __FILE__ inside of header.php, it will return the filepath to header.php. This is probably not what you need.
Your attempts are almost there, but because $_SERVER['SCRIPT_FILENAME'] returns the full path of your file, you need to somehow only get the filename portion of it, luckily there is a built in function for that already basename(). So you only need to change a little bit of your code:
$currentpage = basename($_SERVER['SCRIPT_FILENAME']);
In a website say abc.com, I have products, about and contact page. These pages have same Header, Footer section. Should we include header and footer with PHP or We copy/paste HTML/CSS coding done in Index page? Which is the best proctise?
Use PHP to include your header and footer, that way if you ever need to make a change you only have to edit one file to update the entire website.
You can set up a cool method like this:
<?php
$title = "This is the page title"; // Make your header use this
require "Header.php";
?>
Put the page contents in between.
<?php
require "Footer.php";
?>
The widely accepted way is to have one central file (e.g. index.php) that has the header and footer (you can choose to include them or just have them in the file), and that central file includes the content files in its content area.
There are other ways to do this, but definitely do not copy&paste the same header/footer to many pages as that will make future editing a nightmare.
You should use PHP to include them.
Also you could think about using a Template-Engine which would offer you some nice features
like Nesting content and defining base-layouts which you can extend.
Twig has those features:
http://twig.sensiolabs.org/
create file like footer.php
Set your code in this and you can add this in index.php page like below
<?php
require_once( 'footer.php' );
?>
same way for header
I want to use php to easily maintain my website, but I simply can't figure out the language - I've found some tuts online, and some other questions here, but none help me.
I've divided my site into some .php files, header/footer and such - And using
works fine..
Now I want the content of my site, to update according to which menu I click on at my site.
http://dawtano.com/pp/
If I click on "about" I want the "Hello World" to open inside my content div, but I can't get the right php code to do it.
I think you should do this---
Note: This will only work if the CSS styling are on the current directory! ()
<div>
<?php
$html_page = implode('', file('http://dawtano.com/pp/'));
echo $html;
?>
</div>
Hope this helps!
well currently your links are taking you to a separate page entirely. So why not just code it so that your include file is specific to the page. i.e, on about.php, use something like
include 'about_content.php
in your contetnt div.
If you're looking for your content to load dynamically into the content div you'll need to look into using ajax to fetch the content pages.
One popular way to construct the site is to have a single php script which displays content based upon a $_GET variable like 'page' or 'content', and then make the link as:
'http://dawtano.com/pp/index.php?page=helloworldcontent'
Using this method, you would need to check if the variable ($_GET['page']) is set using isset(), and then make sure the string is safe... as anybody with a browser could just type in some mumbo-magic script and hijack your site:
'http://dawtano.com/pp/index.php?page=somecleaverlycraftedhax'
Once it exists and is safe, add the '.php' to the file name and include that file... if it exists! If it doesn't exist, then you will need some code to handle that, probably by displaying a 'File not Found' message, or redirecting home, or something.
I prefer not to do this because it is a pain to make safe, and I feel like it is pretty ugly. What I do instead is put all the header/footer/navbar/title bar scripts into seperate 'display' functions, and put them in another file.
Then include this file with the function definitions, and call all the 'display' functions to set up the page. So every php script in your site might look like:
<?php
include 'html_display_functions.php';
/* put lines here to parse $_GET and $_POST, session_start()/$_SESSION, etc... */
print_html_pre_content();
print '<p>Hello, world!</p>';
print_html_post_content();
?>
Since every script will have this structure, you can just create a template file once. When you want to create a new page for your site, copy the template, rename the copy to the php filename you want, and add content between the two print functions.
You also keep the ability to modify the header/footer/navbar/title bar for the whole site in a central location, namely the included file with the functions.
You might be looking for some sort of Template Engine which allows you to create your pages out of variable parts. You could have a look at TBS, which is more or less what is suggested by the name. But there is a whole lot more engines out there which could do the job.
If that's already too much over the top, maybe Apache SSI (Server Side Includes) are a try for you.
A little suggestion from my side, I am often using Apaches mod_rewrite in connection with a single controller.php file. Apaches mod_rewrite will then send all request to the controller.php which will fetch the appropriate page parts for the requested page using TBS and return the respective page. So you have the controll of the page in one location only.
To your original question about.php could look like:
<?php
include('header.php');
?>
// original page content as html for about.php
// assuming header ends with the starting div <div> where you like the content to appear
// and footer starts with the closing div </div>
// if you need variable content here, simply use <?php echo $your_variable ?>
<?php
include('footer.php');
?>
The best way would be to use a switch statement:
http://php.net/manual/en/control-structures.switch.php
Something like this:
<?php
include("header.php");
$page = $_GET['page'];
switch($page)
{
case "about":
include "about.php";
break;
case "faq":
include "faq.php";
break;
case "help":
include "help.php";
break;
default:
include "home.php";
}
include("footer.php);
?>
Then just make all of your links look like this:
http://www.example.com/index.php?page=home
Just replace home with the correct page.
I'm currently using include 'header.php' and include 'footer.php' in every page, and as far as I know that's how most people do it. I thought of a way that I personally thought would be better, however. I thought of making index.php, then in the index include the page. This would both eliminate the need for a footer and eliminate the need for include twice in every page. I'm really new to php, however, so I don't know how I would do this. I tried using POST and GET methods, but it doesn't seem to work. What I want to achieve is including pages in the header using a URL such as http://mysite.com/index.php?page=history and then load history.php. If I need to clarify something, just ask. Sorry if I don't accept an answer right away, I'm really drowsy. I'll get to it when I can.
It is not a problem if you include 2 pages in a file, like header.php and footer.php...
Just writing 2 lines of code in each page is not a matter.
You can decide what pages you want to include dynamically in every page by using if statement, instead of passing the page name in the url.
If you'll do it via index.php, you will no doubt do it wrong.
Nothing bad - every newbie does it this way.
Just because you're thinking of includes, while you should be thinking of templates.
You can make it via index.php, no problem. But there should be not a single HTML tag in this index! As well as in the actual page.
No matter if you're doing it in separate pages or via index.php, the scenario should be the same:
Get all data necessary to display particular page.
Call a template.
Thus, your regular page would look like
code
code
code
include 'template.php';
while index.php would look like
get page name
sanitize page name
include page
include 'template.php';
now you can decide what to choose
First off i agree with Meager... Take a look at soem frameworks. Most will use a two step view which essentially does this althoug in a more complex and flexible way.
With that said it would look something like this:
<?php
$page = isset($_GET['page']) ? $_GET['page'] : 'home'; // default to home if no page
if(file_exists($page.'.php')) {
// buffer the output so we can redirect with header() if necessary
ob_start();
include($page.'.php');
$content = ob_get_clean();
}
else
{
// do something for error 404
}
?>
<html>
<head></head>
<body>
<?php echo $content; ?>
</body>
</html>
You could get more complex than that. One thing you want to do uis make sure you dont blindly assume that the page in the $_GET var is safe... make sure the file exists on your server or otherwise sanitize it...