Help needed with session/include

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
textahead
Forum Newbie
Posts: 4
Joined: Sat Mar 07, 2009 10:43 pm

Help needed with session/include

Post by textahead »

Im wanting to count each page a visitor goes to including if the same page is visited more than once ie; if MR X vivits INDEX.php then CONTACTS.php then back to INDEX.php I need it to count as 3 page visits. I then need a different .php page to display for each page visited. i have the php files loaded as fb00.php, fb01.php , fbo2.php , fb03.php ect ect. heres the code im using

Code: Select all

<?php 
if (isset($_SESSION['hit']))
$_SESSION['hit']++; 
else 
$_SESSION['hit']=0; 
$try_file='fb0'.$_SESSION['hit'].'.php'; 
if (file_exists($try_file)) 
include $try_file; 
else 
{ 
$_SESSION['hit']=0; 
} 
?>
It works but only displays fb00.php and I need this to change with each new page
Thanks for any help

P.S. before anyone asks I need this because Im part of a banner exchange that requires a new banner to be displayed on each page. If you have the same banner on each page you get credited with 1 visit even if the visitor goes to 10 pages. If you have different banner on each page you will be credited with 10 visits rather than 1
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Help needed with session/include

Post by Benjamin »

Code: Select all

 
<?php
session_start();
 
if (!isset($_SESSION['hit'])) {
    $_SESSION['hit'] = 0;
}
 
$try_file = 'fb0' . (int) $_SESSION['hit'] . '.php'; 
 
if (file_exists($try_file)) {
    include $try_file;
} else {
    include 'fb00.php'; 
}
 
$_SESSION['hit'] += 1;
 
textahead
Forum Newbie
Posts: 4
Joined: Sat Mar 07, 2009 10:43 pm

Re: Help needed with session/include

Post by textahead »

Thanks for all your help. Ive figured it out. didnt realise you needed session_start(); on each page till i stumbled across it in an online document. Thanks lots. Cheers
Post Reply