banner rotation, with php, or javascript


#1

Ive tried placing $r = rand(1,2); in the script and all, but for some reason it will rotate if I put the $r in place of the $c for counting the lines… but when it does sometimes it wont show banner at all, and sometimes it will show both banners… what do I need to do, not used to using random in anything, what do I do?
the text file goes as follows:
id,date-start,date-stop,company-name,company-email,company-phone,to-url,to-banner,alt,target
1,11/04/2006,2,Tusc-oh.com,admin@tusc-oh.com,330-364-5709,http://tusc-oh.com,http://tusc-oh.com/b/ads/728x90.gif,Advertise With Us!,_blank
2,11/04/2006,3,Tusc-oh.com,admin@tusc-oh.com,330-364-5709,http://tusc-oh.com,HTTP://localhost/b/728x90.gif,Advertise With Us!,_self

the script in which i just include in the page:
[php]

<? $fd =fopen ($_REQUEST[s].".txt", "r"); if ($fd) { $line = null; while (!feof ($fd)) { if (!$line) { // Skip header $line = fgets($fd, 4096); } else { $line = fgets($fd, 4096); if (strlen($line) > 10) { list($id,$d_start,$dur,$c_name,$c_email,$c_phone,$url1,$ban,$alt,$tar) = split (",", $line, 10); list($m,$d,$y)= split ("/", $d_start, 3); list($dd,$mm,$yyyy) = split("-",date("d-m-Y",time())); $today = mktime(0, 0, 0, $mm, $dd, $yyyy); $d_end = mktime(0, 0, 0, $m+$dur, $d, $y); $ds = date('m/d/Y',$d_end); if($d_end >= $today) { $c = count($line); for($i=0;$i<$c;$i++) { echo "".$alt."n"; } } } } } fclose($fd); } ?>

[/php]

how ive been calling it from the page:
[php]

<? include 'b/ads.php?s=728x90'; ?>

[/php]
ive tried javascript, but still get errors, cant place more than one banner on main page… using same script, and different size banners, like 120x90 in 6 different spots on page, and a 728x90 in top middle…
javascript will work, but stillhas errors, understand the php will let me have the different sizes, and more banners on a single page…

the s=728x90 is the size, also use s=120x90 to pull the 120x90 banners into page…

ADMIN EDIT: Added the [ PHP ] tags and formatted code for readability.


#2

First let me say that you should take advantage of the PHP code tags (see http://phphelp.com/forums/viewtopic.php?t=6960 ).

Next I looked at your code and with so many nested loops, you really need to format the code so it’s readable. (I have gone ahead and edited the post in an effort to do this).

Now…

Do you get any error messages? If so what are they?

I am also assuming (based on the post info) that the file containing the info is called 728x90.txt. I am a bit confused as to why or how you are calling it from
[php]

<? include 'b/ads.php?s=728x90'; ?>

[/php]

I don’t think this is an include rather a URI (i.e. tail end of http://somedomain.com/b/ads.php?s=728x90 ).

Now as for the “Randomizing”, it doesn’t look like you have anything to randomize. You loop through the file (currently only 3 lines. Header and 2 entries), then you echo out the data that you split into the variables. with a count value.

If you want to Randomize it you need to set the random value of $r BEFORE you enter the loop that reads the data from the file. Then when it comes time to “ECHO” it out, you would compare $r with (presumably) $id. If they are equal, then you would echo it.

Because you do not put the values from the file in an array with in the application, when you replaced the $c (count) line with $r (random) line, you MAY or MAY NOT have had a match.

I think what you need is more like this…

[php]

<? $r = rand(1,2) $fd =fopen ($_REQUEST[s].".txt", "r"); if ($fd) { $line = null; while (!feof ($fd)) { if (!$line) { // Skip header $line = fgets($fd, 4096); } else { $line = fgets($fd, 4096); if (strlen($line) > 10) { list($id,$d_start,$dur,$c_name,$c_email,$c_phone,$url1,$ban,$alt,$tar) = split (",", $line, 10); list($m,$d,$y)= split ("/", $d_start, 3); list($dd,$mm,$yyyy) = split("-",date("d-m-Y",time())); $today = mktime(0, 0, 0, $mm, $dd, $yyyy); $d_end = mktime(0, 0, 0, $m+$dur, $d, $y); $ds = date('m/d/Y',$d_end); if($d_end >= $today) { if ($r == $id ) { echo "".$alt."n"; } } } } } fclose($fd); } ?>

[/php]
This would echo only ONE entry and would be randomized (as much as you can randomize 2 objects).

Also You might be better to consider using a database and then just querying the database for an ID that equals a random value (within a contiguous range of ids that is)


#3

Wow, Im am totally impressed. That is like totally beautifull… anyhow…
I love what you did, and I will want to get back to this later, I want to convert it to a database later, but mostly wanted to make it into a script with javascript to deal with multiple banners on the same page, I have it working, but Ill get to that later, i have to make a new post for a classifieds im working on first, then Ill start working on this again…
Thanks peg… this is the most help, and responses I had ever gotten on the banner code,…


#4

Major security issue #1:
[php]$fd =fopen ($_REQUEST[s].".txt", “r”); [/php]

PLEASE do NOT use $_REQUEST (EGPCS superglobals pose enough of a thread, $_REQUEST is an abomination), and CERTAINLY do NOT use EGPCS superglobals unchecked to create resources!

Safest is to use a hardcoded path/to/file.txt in your PHP script, but if you must, please please please check your input!

[php]
$allowedFiles = array(“myBanners.txt”, “myButtons.txt”);

$myFile = $_GET[‘s’];
if (!in_array($myFile, $allowedFiles)) {
die(“This file does not exist!”);
}

$fd = fopen($myFile, “r”);
[/php]

Make our web a safer place :)


#5

Thanks Zyppora, Ill keep that in mind when I start working with it again,