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

posted @ 2017-07-25 15:39  KoMiles  阅读(7173)  评论(0编辑  收藏  举报