PHP重定向-标头-不工作


PHP Redirection - Header - Not working

我想重定向到http://www.jango.com/music/$artist/$song/video?l=0,其中$artist$song有望是最常见的组合,从一个文件中提取,其内容如下:

MONSTER,PARAMORE
MONSTER,PARAMORE
DARK HORSE,KATY PERRY

因此,对于该示例文件,URL应该是:

http://www.jango.com/music/PARAMORE/MONSTER/video?l=0

这是代码:

<?PHP
$pin = $_GET["pin"];
//LOAD THE VOTES FILE
$arraypos = 0;
$concat=array("");
$file_handle = fopen("../users/" . $pin . "/votes.php", "r");
while (!feof($file_handle)) {
   $line = fgets($file_handle);      
   $linelength =  strlen($line);
   $concat[$arraypos]=($line);
   $arraypos = ($arraypos + 1);
}
fclose($file_handle); 
//get all the frequencies
$frequencies = array_count_values($concat);
//make sure to sort it since array_count_values doesn't return a sorted array
arsort($frequencies);
//reset the array because you can't trust keys to get the first element by itself
reset($frequencies);
//get the first key
$winner = key($frequencies);
//find position of ,
   $positionofcomma = strpos($winner , ",");
   //dump left of , to songs
   $song = substr($winner , 0 , $positionofcomma);
   //dump right of , to artists
   $artist= substr($winner , $positionofcomma + 1); 
//refine for redirection
$find=" ";
$replace="+";
$song = str_replace ( $find , $replace , $song );
$artist = str_replace ( $find , $replace , $artist);
$search="Location: http://www.jango.com/music/$artist/$song/video?l=0";
   header($search);
?>

我已经试着让它发挥作用好几个小时了,我真的很累了。我不知道哪里出了问题。这可能只是一个愚蠢的错误。。。。

我知道它完美地获得了$search,只是不会重定向到它。

确保以下变量必须具有值:

if(!empty($artist) && !empty($song))
{
 header("Location: http://www.jango.com/music/$artist/$song/video?l=0");
}

请使用empty()检查变量,然后输入header()。

试试这个:

$search="http://www.jango.com/music/$artist/$song/video?l=0";
header("Location: $search");

或者在特定部件上使用urlencode,例如:

$search="http://www.jango.com/music/" . urlencode($artist) . "/" . urlencode($song) . "/video?l=0";
header("Location: $search");

此外,当您在本地对该脚本运行curl -I以查看标头时,该脚本的输出是什么?

这样的东西:

curl -I http://yourlocalsetup/thescript.php

会产生这样的东西:

HTTP/1.1 302 Moved Temporarily
Location: http://www.jango.com/music/[the value of $artist]/[the value of $song]/video?l=0