使用 PHP 从 HTTP 重定向到 HTTPS

发布于 2024-10-19 07:48:57 字数 134 浏览 1 评论 0原文

我正在开发一个购物车网站,我想在用户输入帐单详细信息时将其重定向到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

殊姿 2024-10-26 07:48:57

尝试这样的事情(应该适用于 Apache 和 IIS):

if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off") {
    $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . $location);
    exit;
}

Try something like this (should work for Apache and IIS):

if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off") {
    $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . $location);
    exit;
}
清风夜微凉 2024-10-26 07:48:57

这是一个很好的方法:

<?php
if (!(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || 
   $_SERVER['HTTPS'] == 1) ||  
   isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&   
   $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'))
{
   $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
   header('HTTP/1.1 301 Moved Permanently');
   header('Location: ' . $redirect);
   exit();
}
?>

This is a good way to do it:

<?php
if (!(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || 
   $_SERVER['HTTPS'] == 1) ||  
   isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&   
   $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'))
{
   $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
   header('HTTP/1.1 301 Moved Permanently');
   header('Location: ' . $redirect);
   exit();
}
?>
断爱 2024-10-26 07:48:57

在 IIS 上使用 PHP 从 HTTP 重定向到 HTTPS

我在重定向到 HTTPS 以在 Windows 服务器上工作时遇到问题
它运行 MS Internet 信息服务 (IIS) 版本 6。我更
习惯于在 Linux 主机上使用 Apache,所以我转向互联网
帮助,这是我搜索时排名最高的 Stack Overflow 问题
对于“php 将 http 重定向到 https”。然而,所选的答案不起作用
为我。

经过一番尝试和错误后,我发现使用 IIS,$_SERVER['HTTPS']
对于非 TLS 连接,设置为关闭。我认为下面的代码应该
帮助通过搜索引擎找到此问题的任何其他 IIS 用户。

<?php
if (! isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off' ) {
    $redirect_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header("Location: $redirect_url");
    exit();
}
?>

编辑:来自另一个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'] is
set to off for non-TLS connections. I thought the following code should
help any other IIS users who come to this question via search engine.

<?php
if (! isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off' ) {
    $redirect_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header("Location: $redirect_url");
    exit();
}
?>

Edit: From another Stack Overflow answer,
a simpler solution is to check if($_SERVER["HTTPS"] != "on").

晨光如昨 2024-10-26 07:48:57

您始终可以使用

header('Location: https://www.domain.com/cart_save/');

重定向到保存 URL。

但我建议通过 .htaccess 和 Apache 重写规则来完成。

You can always use

header('Location: https://www.domain.com/cart_save/');

to redirect to the save URL.

But I would recommend to do it by .htaccess and the Apache rewrite rules.

是伱的 2024-10-26 07:48:57

在我的 AWS beanstalk 服务器上,我没有看到 $_SERVER['HTTPS'] 变量。我确实看到 $_SERVER['HTTP_X_FORWARDED_PROTO'] 可以是“http”或“https”,因此如果您在 AWS 上托管,请使用以下内容:

if ($_SERVER['HTTP_HOST'] != 'localhost' and $_SERVER['HTTP_X_FORWARDED_PROTO'] != "https") {
    $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . $location);
    exit;
}

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:

if ($_SERVER['HTTP_HOST'] != 'localhost' and $_SERVER['HTTP_X_FORWARDED_PROTO'] != "https") {
    $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . $location);
    exit;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文