A direct answer to your question:
<?php
session_start();
if (!isset($_SESSION['FirstVisitOfDay']))
{
include('header.txt');
$_SESSION['FirstVisitOfDay']="1";
}
else
{
include('staticheader.txt');
}
?>
This will only display the "header.txt" on the first time the page(s) is access for that session. After that, the "staticheader.txt" will show. If the browser is restarted or the session is ended in some other way, this will be reset and the logic will repeat. Is this what you were wanting?
Rather than using includes like that, I would do it more like this:
main file:
<?php
include('headers.inc');
CheckHeaders(); ?>
headers.inc:
<?php
function HandelSession()
{
session_start();
}
function CheckHeaders()
{
HandelSession();
if (!isset($_SESSION['FirstVisitOfDay']))
{
ShowFirstVisit();
$_SESSION['FirstVisitOfDay']="1";
}
else
{
ShowRemainingVisits();
}
}
function ShowFirstVisit()
{
print("Stuff that would have been in the");
print("header.txt");
}
function ShowRemainingVisits()
{
print("Stuff that would have been in the");
print("staticheader.txt");
}
?>
This separates out the repeating logic from that pages, so there is little that you need to worry about when creating new pages. You can also make it auto-execute by moving the "CheckHeaders();" to the bottom of the "headers.inc" file. eg:
main file:
<?php
include('headers.inc');
?>
headers.inc:
<?php
function HandelSession()
{
session_start();
}
function CheckHeaders()
{
HandelSession();
if (!isset($_SESSION['FirstVisitOfDay']))
{
ShowFirstVisit();
$_SESSION['FirstVisitOfDay']="1";
}
else
{
ShowRemainingVisits();
}
}
function ShowFirstVisit()
{
print("Stuff that would have been in the");
print("header.txt");
}
function ShowRemainingVisits()
{
print("Stuff that would have been in the");
print("staticheader.txt");
}
CheckHeaders();
?>
This means that the only thing you need to put in the pages is "include('headers.inc');" which will make maintenance a lot easier.
You may need to find a better place to put the "HandelSession()". The placing is fine while the project is small, but if it grows, this could be quite restrictive is something needs to change. Somewhere generic would be good. I was wanting to keep the number of Auto-executes down, but it could work well to do it in this case if your logic is going to change.
Some rules I use when doing this type of thing:
- Keep includes in one place at the top. This means everything included is loaded before anything later in the file so that any pre-requisites are satisfied.
- Don't do conditional includes. This creates headaches as things get bigger.
- Keep Auto-executes at the bottom. This makes sure that everything they might need is already loaded before they run.
- Use Auto-executes really sparingly. They create huge problems when the project gains size and something needs to be changed. You are much better to put everything in functions and begin the logic from one place rather than haveing it start itself in various places.