使用 PHP 从 HTTP 重定向到 HTTPS
我正在开发一个购物车网站,我想在用户输入帐单详细信息时将其重定向到 HTTPS 页面,并保持下一页的 HTTPS 连接,直到他注销。
为了做到这一点,我需要在服务器上安装什么(我正在使用 Apache),以及如何从 PHP 完成此重定向?
I'm working on a shopping cart website and I would like to redirect the user to a HTTPS page when he's entering his billing details and maintain the HTTPS connection for the next pages until he logs out.
What do I need to install on the server (I'm using Apache) in order to do this, and how can this redirect be done from PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试这样的事情(应该适用于 Apache 和 IIS):
Try something like this (should work for Apache and IIS):
这是一个很好的方法:
This is a good way to do it:
在 IIS 上使用 PHP 从 HTTP 重定向到 HTTPS
我在重定向到 HTTPS 以在 Windows 服务器上工作时遇到问题
它运行 MS Internet 信息服务 (IIS) 版本 6。我更
习惯于在 Linux 主机上使用 Apache,所以我转向互联网
帮助,这是我搜索时排名最高的 Stack Overflow 问题
对于“php 将 http 重定向到 https”。然而,所选的答案不起作用
为我。
经过一番尝试和错误后,我发现使用 IIS,
$_SERVER['HTTPS']
是对于非 TLS 连接,设置为
关闭
。我认为下面的代码应该帮助通过搜索引擎找到此问题的任何其他 IIS 用户。
编辑:来自另一个Stack Overflow答案,
一个更简单的解决方案是检查
if($_SERVER["HTTPS"] != "on")
。Redirecting from HTTP to HTTPS with PHP on IIS
I was having trouble getting redirection to HTTPS to work on a Windows server
which runs version 6 of MS Internet Information Services (IIS). I’m more
used to working with Apache on a Linux host so I turned to the Internet for
help and this was the highest ranking Stack Overflow question when I searched
for “php redirect http to https”. However, the selected answer didn’t work
for me.
After some trial and error, I discovered that with IIS,
$_SERVER['HTTPS']
isset to
off
for non-TLS connections. I thought the following code shouldhelp any other IIS users who come to this question via search engine.
Edit: From another Stack Overflow answer,
a simpler solution is to check
if($_SERVER["HTTPS"] != "on")
.您始终可以使用
重定向到保存 URL。
但我建议通过 .htaccess 和 Apache 重写规则来完成。
You can always use
to redirect to the save URL.
But I would recommend to do it by .htaccess and the Apache rewrite rules.
在我的 AWS beanstalk 服务器上,我没有看到 $_SERVER['HTTPS'] 变量。我确实看到 $_SERVER['HTTP_X_FORWARDED_PROTO'] 可以是“http”或“https”,因此如果您在 AWS 上托管,请使用以下内容:
On my AWS beanstalk server, I don't see $_SERVER['HTTPS'] variable. I do see $_SERVER['HTTP_X_FORWARDED_PROTO'] which can be either 'http' or 'https' so if you're hosting on AWS, use this: