我想重定向到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
,它只是不会重定向到它。
答案 0 :(得分:0)
请改为尝试:
$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
答案 1 :(得分:0)
确保以下变量必须具有值:
if(!empty($artist) && !empty($song))
{
header("Location: http://www.jango.com/music/$artist/$song/video?l=0");
}
请使用empty()检查变量,然后在header()中输入。