Header location not working properly

前端 未结 2 1651
自闭症患者
自闭症患者 2020-12-20 07:31

I have a php script that renders an image (with imagick) and saves it to some directory \"SITE_ROOT.$filePath\", then does a header(\'Location: \' . SITE_

2条回答
  •  情歌与酒
    2020-12-20 08:05

    You are using file paths which is not working with header location. You are supposed to use urls.

    It's best practice to use absolute urls in header location. PHP documentation says:

    HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. (Source)

    Also always exit the script afterwards because otherwise in my experience in certain circumstances code that comes after the redirect might still be executed. So a good example would look like this:

    header("location:http://www.mysite.com/path/to/myfile.php");
    exit;
    

    Often you would use a server variable for this case:

    $url = $_SERVER["HTTP_HOST"]."/path/to/myfile.php";
    header("location:".$url);
    exit;
    

    Cheers!

提交回复
热议问题