homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

PHP Rotator for loading different include.php file

 1:11 am on Mar 9, 2012 (gmt 0)


I am trying to create a simple rotator that will randomly load iclude1.php on the first load then include2.php on the second load or if refreshed then include3.php and so on and on.

What I am actually trying to achieve is: I have few difrent adds scripts in separate php files that I would like to change every time when page is being loaded or reloaded.

Thanks for you help in advance



 2:04 am on Mar 9, 2012 (gmt 0)

Is your goal site wide or per visitor?

Per visitor is pretty easy, just track the current counter in a session variable and increment the session variable each time a page is loaded.

Site wide is equally as easily, just track the current counter in a flat file or in a MySQL database. Read it, increment it, write it back out, then display the appropriate file for the current count.


 9:56 am on Mar 9, 2012 (gmt 0)

Thanks for you respond to my question, in theory it makes sense to me, but when it comes to coding, I am a bit slow on PHP. any chance of helping me out?

here is what I have:

/includes/file1.php ... file2.php ... file3.php

so at this stage I simply use the following line where I am including one of the files above

<?php include("/var/www/vhosts/testdomain.com/httpdocs/includes/file1.php");?>

so the question is how to accommodate all of the above files in this one line so that wen page is loaded or reloaded then above line will include file2.php or file3.php insted of file1.php?

any help is appreciated as always.


 10:35 am on Mar 9, 2012 (gmt 0)

The code would probably look something like this, I didn't test it...


if (isset($_SESSION['cnt'])){
$_SESSION['cnt'] += 1;
if( $_SESSION['cnt'] > 3 ) $_SESSION['cnt'] =1;
}else {
$_SESSION['cnt'] =1;

include("/var/www/vhosts/testdomain.com/httpdocs/includes/file" . $_SESSION['cnt'] . ".php");

The above should display files 1 thru 3.


 12:20 pm on Mar 9, 2012 (gmt 0)

Sweet, works as intended, thanks a million.

Security wise, how secure to use this script on the regular html output page?


 4:16 pm on Mar 9, 2012 (gmt 0)

thanks a million.

A bit more compensation than it's probably worth, but I'll accept it in small denomination unmarked bills.

Shouldn't have any security issues.


 6:53 pm on Mar 31, 2012 (gmt 0)

Hi again,

All through the script works fine, but I have started to get an errors like the one below:

[Sat Mar 31 12:47:18 2012] [error] [client] PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /var/www/vhosts/test_site.com/httpdocs/some_files/file.html:50) in /var/www/vhosts/test_site.com/httpdocs/some_files/file.html on line 55, referer: [test_site.com...]

[Sat Mar 31 12:47:29 2012] [error] [client] PHP Notice: A session had already been started - ignoring session_start() in /var/www/vhosts/test_site.com/httpdocs/some_files/file_1.html on line 67, referer: [test_site.com...]

any ideas as to why this has been happening?


 12:08 am on Apr 1, 2012 (gmt 0)

Maybe you have session_start() stated more than once in your file.

Indeed the code snippet above that you added has one, but maybe you already had one in your file.


 10:25 am on Apr 1, 2012 (gmt 0)

Could happen if some of your files invoked before the session_start output something. In many cases it accidentally happens by leaving blank lines outside the php tags in files (like at the start of the file).


 2:06 pm on Apr 1, 2012 (gmt 0)

Thanks for your response, I have placed the following to the beginning of the page and it seems that this has resolved the error in logs.
if (!isset($_SESSION)) {

but now I am facing slightly different problem with headers that are being sent of the following character:
Set-Cookie:PHPSESSID=ac588btkl03a3t7g9tjb8dvun6; path=/
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control:no-cache, no-store, must-revalidate, post-check=0, pre-check=0

and in reality what I'd like to have instead is simple

Expires:Sun, 01 Apr 2012 20:03:21 GMT

I should probably use some other method rather then session_start() to do what I need, but have run out of ides because of my poor knowledge of php.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved