标题位置无法正常工作 [英] Header location not working properly

查看:24
本文介绍了标题位置无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 php 脚本,可以渲染图像(使用 imagick)并将其保存到某个目录 "SITE_ROOT.$filePath",然后执行 header('Location: ' .SITE_ROOT.$filePath),它重定向到的文件是一个png图像.

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_ROOT.$filePath), the file it redirects to is a png image.

如果我直接进入路径,比如在 URL 栏中键入它,我可以保存图像并且一切正常,但是当我依靠脚本重定向我并尝试右键单击并保存图像时没有识别出我实际上是在尝试保存图像,它认为我正在尝试将其保存为名为驱动程序"的非类型文件,这是脚本页面的名称.

If I go to the path directly, like just type it in the URL bar I can save the image and everything works fine, however when I rely on the script to redirect me and I try to right click and save the image it doesn't recognise that I'm actually trying to save an image, it thinks I'm trying to save it as a non-typed file called 'Driver' which is the name of the script page.

我不知道这里出了什么问题,肯定标题位置应该只是将我带到图像并且在重定向后没有驱动程序"文件的记录?

I have no idea what's wrong here, surely the header location should just take me to the image and have no record of the 'Driver' file after it has redirected?

redirect() 也会发生同样的事情.

The same thing happens with redirect() too btw.

提前感谢您的帮助!

这个问题是通过在头部命令后放置一个die()来解决的.

This problem was solved by placing a die() after the header command.

推荐答案

您使用的文件路径不适用于标头位置.你应该使用网址.

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

最好在标题位置使用绝对网址.PHP 文档说:

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

HTTP/1.1 需要一个绝对 URI,因为论据»位置:包括方案、主机名和绝对路径,但有些客户端接受相对 URI.(来源)

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;

干杯!

这篇关于标题位置无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆