Welcome to WebmasterWorld Guest from 54.167.86.211

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Rotator for loading different include.php file

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

Full Member

5+ Year Member

joined:Apr 26, 2009
posts: 246
votes: 2


Hi,

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)

Administrator from US 

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 25, 2005
posts:14624
votes: 88


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)

Full Member

5+ Year Member

joined:Apr 26, 2009
posts: 246
votes: 2


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)

Administrator from US 

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 25, 2005
posts:14624
votes: 88


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

session_start();

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)

Full Member

5+ Year Member

joined:Apr 26, 2009
posts: 246
votes: 2


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)

Administrator from US 

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 25, 2005
posts:14624
votes: 88


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)

Full Member

5+ Year Member

joined:Apr 26, 2009
posts: 246
votes: 2


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 000.000.000.00] 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 000.000.000.00] 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)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Apr 30, 2007
posts:1394
votes: 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)

Full Member

5+ Year Member

joined:Apr 26, 2009
posts: 246
votes: 2


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.
<?php
if (!isset($_SESSION)) {
session_start();
}
?>


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
Pragma:no-cache


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


Cache-Control:max-age=21600
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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members