GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   php redirect shows blank page (https://gfy.com/showthread.php?t=1001109)

klinton 12-08-2010 02:12 PM

php redirect shows blank page
 
hey, i setup some php rotation redirect...It should redirects people to some url from list.

however i see now, that it returns blank page every now and then...and i had to refresh page to get redirection...why is that ?

myneid 12-08-2010 02:19 PM

thats kind of like saying that your car doesnt work whats wrong with it, without giving the specifics

klinton 12-08-2010 02:30 PM

<?php

$urls[] = "url1";
$urls[] = "url2";
$urls[] = "url3";
$urls[] = "url4";
$urls[] = "url5";

srand ((double) microtime( )*1000000);
$random = rand(0,count($urls));

header("location:" . $urls[$random]);
exit;
?>

it sometimes redirects&rotate url,sometimes just show blank page....

Serge Litehead 12-08-2010 02:35 PM

perhaps count($urls) needs -1
count($urls)-1

Serge Litehead 12-08-2010 02:38 PM

when debugging you can print out url and random number vars and see where problem lays :2 cents:

myneid 12-08-2010 02:41 PM

yea thats what it is you need count($urls)-1 because count returns the number of elements in teh array but the array is indexed starting at 0

klinton 12-08-2010 02:50 PM

so i should add this line somewhere :-P ???

count($urls)-1 ??

where ;-) ?

edit: ups now i see it...

THANKS !!! :)

myneid 12-08-2010 02:53 PM

actually i take that back, dont do the $count-1
instead cahnge this line to this
header("location:" . $urls[$random-1]);

grumpy 12-08-2010 02:56 PM

Quote:

Originally Posted by myneid (Post 17760190)
actually i take that back, dont do the $count-1
instead cahnge this line to this
header("location:" . $urls[$random-1]);

nope, that can result in -1 your first suggestion was ok

replace
$random = rand(0,count($urls));
with
$random = rand(0,count($urls)-1);

myneid 12-08-2010 03:04 PM

ha! yea looks like the sudafed is affecting my judgement more than i previously thought

grumpy 12-08-2010 03:06 PM

Quote:

Originally Posted by myneid (Post 17760229)
ha! yea looks like the sudafed is affecting my judgement more than i previously thought

i know exactly what you mean :thumbsup

Tempest 12-08-2010 03:10 PM

Quote:

Originally Posted by grumpy (Post 17760200)
nope, that can result in -1 your first suggestion was ok

replace
$random = rand(0,count($urls));
with
$random = rand(0,count($urls)-1);

What he said...

woj 12-08-2010 03:19 PM

using array_rand is probably more straightforward... :2 cents:

GrouchyAdmin 12-09-2010 09:06 PM

Quote:

Originally Posted by woj (Post 17760273)
using array_rand is probably more straightforward... :2 cents:

Certainly takes out some minor overhead by calling count() manually for each parse. I'd suggest this as well for something as simple-tarded as mentioned.

Code:

<?php
$urls = array(
        'http://www.google.com',
        'http://www.msn.com',
        'http://www.bing.com',
        'http://www.webcrawler.com',
        'http://www.dogpile.com',
);
header("location: ". array_rand(array_flip($urls), 1));
?>


Tempest 12-09-2010 09:22 PM

Quote:

Originally Posted by GrouchyAdmin (Post 17763769)
Certainly takes out some minor overhead by calling count() manually for each parse. I'd suggest this as well for something as simple-tarded as mentioned.

Code:

<?php
$urls = array(
        'http://www.google.com',
        'http://www.msn.com',
        'http://www.bing.com',
        'http://www.webcrawler.com',
        'http://www.dogpile.com',
);
header("location: ". array_rand(array_flip($urls), 1));
?>


Or he could just do a shuffle.

Code:

<?php
$urls[] = "url1";
$urls[] = "url2";
$urls[] = "url3";
$urls[] = "url4";
$urls[] = "url5";

shuffle($urls);

header('location: '.$urls[0]);
?>

Gotta love how's there multiple solutions to the same thing.


All times are GMT -7. The time now is 08:42 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123