我在用php的header作跳轉時,報錯誤。php
Header may not contain more than a single header, new line detectedapp
先貼一下代碼:編碼
class advertJumpUrl { /** * 廣告惟一的key * @return array */ private static function aGetKey() { $sKey = Ko_Tool_Input::VClean("r","k",Ko_Tool_Input::T_STR); $sKey = \apps\brand\cpm\MFacade_LogApi::sDecrypt($sKey); $aPara = explode('_', $sKey); return $aPara; } /** * 廣告跳轉的url * @return url */ private static function sGetUrl() { $sUrl = Ko_Tool_Input::VClean("r","t",Ko_Tool_Input::T_STR); return \apps\brand\cpm\MFacade_LogApi::sDecrypt($sUrl); } /** * 程序執行 */ public static function vRun() { $aPara = self::aGetKey(); $sUrl = self::sGetUrl(); // $sUrl = rawurldecode($sUrl); if($aPara && $sUrl) { // 記錄點擊事件 \apps\brand\cpm\MFacade_LogApi::vClick($aPara[1], $aPara[0]); header('HTTP/1.1 301 Moved Permanently'); header('location:'.$sUrl); exit; } header('HTTP/1.1 301 Moved Permanently'); header('location:/'); exit; } } advertJumpUrl::vRun();
在網上搜索一下,是由於URL編碼有問題。我嘗試用urlencode 而後再urldecode。發現不行。url
有人建議用rawurlencode。我試了試,成功了。spa
urlencode 和rawurlencode 的區別:.net
urlencode 將空格則編碼爲加號(+)
rawurlencode 將空格則編碼爲加號(%20)code
參考文檔:htm
https://stackoverflow.com/questions/16320403/getting-warning-header-may-not-contain-more-than-a-single-header-new-line-deteblog
http://www.jb51.net/article/24123.htm事件