2013-01-11 5 views
0

사용자가 즉 index 파일 내 홈 페이지에 나오는 모든 시간 index.html을에서 무작위로 리디렉션 및 내 웹 사이트의 임의 페이지마다 볼 수 있습니다.내가 스크립트 그래서 다른 실행하고자하는 다른 파일

자바 스크립트 또는 PHP에서이 작업을 수행하는 것이 좋습니다. urlRedirect.

어떤 생각이에 브라우저를 리디렉션

var randomNumber = functionThatReturnsRandomNumber(10); 
var urlRedirect; 

if (randomNumber == 0) 
    urlRedirect = 'xxxx.com/folder0/index.html 

if (randomNumber == 1) 
    urlRedirect = 'xxxx.com/folder1/index.html 

if (randomNumber == 2) 
    urlRedirect = 'xxxx.com/folder2/index.html 

... 

if (randomNumber == 9) 
    urlRedirect = 'xxxx.com/folder9/index.html 

다음 몇 가지 코드 :이처럼 보일 것입니다 상상 인덱스 파일의 의사 코드?

편집

은 내가 더 명시 할 필요 같아요. 누군가가 위의 목표를 달성 할 수있는 방법을 제안 해 주시겠습니까? 감사.

+0

인가 _ "어떤 생각? "_ 실제 질문은? – wakooka

+3

url을위한 배열을 사용하면 세계의'if()'명령문을 낭비하지 않고 있습니다 ... 그들은 유한 한 자원입니다. –

+0

@ jerome.s 편집을 참조하십시오. –

답변

1

자바 스크립트를 사용하려는 경우 var randomnumber=Math.floor(Math.random()*11);을 사용하여 1에서 10 사이의 임의의 숫자를 생성 한 다음 window.location.href=urlRedirect;을 사용하여 선택한 페이지로 사용자를 리디렉션하십시오.

+0

이것은 나에게 가장 우아한 답변입니다. 어떻게 득표를하지 않습니까? –

1

리디렉션 헤더를 사용하십시오. 임의 재 지정에

<?php 
$location = "http://google.com"; 
header ('HTTP/1.1 301 Moved Permanently'); 
header ('Location: '.$location); 
?> 

:

<?php 
$urls = array('http://1.com',"http://2.com","http://3.com"); //specify array of possible URLs 
$rand = rand(0,count($urls)-1); //get random number between 0 and array length 
$location = $urls[$rand]; //get random item from array 
header ('HTTP/1.1 301 Moved Permanently'); //send header 
header ('Location: '.$location); 
?> 
4

한 우수한 사용자 경험을위한.

사용자는 PHP 수준에서 그렇게하는 것이 더 낫습니다. 그렇지 않으면 어쩔 수없이 loading->page glimpse->loading->new page의 딸꾹질이 발생합니다 (그리고 그럴 경우 방문객으로 보았을 때 매우 기분이 좋을 것입니다).

그러나 당신이 마음에서 "가능한 대상"의 목록을 가지고, 한, 당신은 당신의 index.php의 상단에 다음과 같이 사용할 수 있습니다

<?php 
    $possibilities = array(/*...*/); 
    header('Location: ' + $possibilities[rand(0, count($possibilities) - 1)]); 

을 가진이 내가 좋겠 아마 몇하지만 세션 또는 쿠키 중 하나이므로 처음 방문 할 때만 작동합니다 (매번 작동하지 않으려는 경우).

+0

'rand (0, count ($ possibilities) - 1)'이 필요합니다.'; [rand] (http://php.net/manual/en/function.rand.php) 함수는 닫힌 간격이 아닌 반 열림 예상합니다. – PleaseStand

+0

@PleaseStand : 좋은 콜, 고마워. 이것은 대체로'rand (0, count())'에 비해'rand (0,1000) % count()'를 사용하여 더 나은 대체를 위해 논쟁의 여지가있다.; p –

0

사용 PHP : 임의의 서브 디렉토리에

<?php 
$randomNumber = rand(10); 
$urlRedirect = ''; 

if ($randomNumber == 0) 
    $urlRedirect = 'xxxx.com/folder0/index.html'; 

if ($randomNumber == 1) 
    $urlRedirect = 'xxxx.com/folder1/index.html'; 

if ($randomNumber == 2) 
    $urlRedirect = 'xxxx.com/folder2/index.html'; 

... 

if ($randomNumber == 9) 
    $urlRedirect = 'xxxx.com/folder9/index.html'; 

header ('Location: '.$urlRedirect); 
0

재 :

<?php 
$myLinks = array("dir-1/", 
    "dir-2/", 
    "dir-3/", 
    "dir-4/", 
    "dir-5/"); 

$randomRedirection = $myLinks[array_rand($myLinks)]; 
header("Location: $randomRedirection"); 
?> 

재 임의의 웹 사이트 :

<?php 
$myLinks = array("http://www.my-site.ie", 
    "http://www.my-site.eu", 
    "http://www.my-site.de", 
    "http://www.my-site.it", 
    "http://www.my-site.uk"); 

$randomRedirection = $myLinks[array_rand($myLinks)]; 
header("Location: $randomRedirection"); 
?> 
관련 문제