header 跳轉時報錯誤。Header may not contain more than a single header, new line detected


我在用php的header做跳轉時,報錯誤。

Header may not contain more than a single header, new line detected

先貼一下代碼:

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。發現不行。

有人建議用rawurlencode。我試了試,成功了。

urlencode 和rawurlencode 的區別:

urlencode 將空格則編碼為加號(+)
rawurlencode 將空格則編碼為加號(%20)

 

參考文檔:

https://stackoverflow.com/questions/16320403/getting-warning-header-may-not-contain-more-than-a-single-header-new-line-dete

http://www.jb51.net/article/24123.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM