各种PHP问题

发布于 2024-12-09 05:32:17 字数 454 浏览 0 评论 0原文

  1. 关于此功能:

    函数 requireSSL() {     
    
        如果($_SERVER['SERVER_PORT']!= 443){
            header("HTTP/1.1 301 永久移动");
            header("位置:https://" . $_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI']);
            出口;
        }
        返回假;
    }
    

    SSL需要端口443,所以在这段代码中如果不是443那么就强制使用https?如果是 443,它会自动使用我假设的 https。永久移动是用来做什么的?

  2. 代码 header("位置:.");

    上面有什么用,无非是告诉它停留在同一页面。默认情况下应该停留在同一个页面,不是多余吗?

  1. about this function:

    function requireSSL() {     
    
        if($_SERVER['SERVER_PORT'] != 443) {
            header("HTTP/1.1 301 Moved Permanently");
            header("Location: https://" . $_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI']);
            exit;
        }
        return false;
    }
    

    SSL requires port 443, so in this piece of code if not 443 then forced to use https? and if 443 it would automatically use https I assumed. What's the moved permanently used for?

  2. The code header("Location: .");

    What's use of the above, it's nothing more than telling it to stay at the same page. By default it should stay at the same page, isn't it redundant?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

掀纱窥君容 2024-12-16 05:32:17

SSL 需要端口 443

并非总是如此。这是默认端口。 HTTPS 可以配置为在其他端口上运行。更好的检查方法是:

if ( !isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) != 'on') {

}

移动后的永久用途是什么?

来自 w3

所请求的资源已被分配一个新的永久 URI 和任何
未来对此资源的引用应该使用返回的之一
URI

换句话说,浏览器应该记住所请求的 URI 已被移动,并在用户再次请求旧 URI 时请求新 URI。

默认情况下它应该停留在同一页面,这不是多余的吗?

这不是代码所做的。当用户请求非 https 版本时,它会重定向到 https 版本。

SSL requires port 443

Not always. That is the default port. HTTPS can be configured to run on other ports. A better way to check is:

if ( !isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) != 'on') {

}

What's the moved permanently used for?

From w3:

The requested resource has been assigned a new permanent URI and any
future references to this resource SHOULD use one of the returned
URIs

In other words, a browser should remember that the requested URI is moved and request the new URI anytime the user requests that old URI again.

By default it should stay at the same page, isn't it redundant?

That's not what the code is doing. It's redirecting to the https version when the user requests the non-https version.

惟欲睡 2024-12-16 05:32:17

这段代码将客户端重定向为使用 https,以防客户端不使用 https301 告诉客户端始终使用新位置。

重定向是通过 Location 标头完成的,您只说对了一部分,页面是相同的,但连接是通过 https 而不是 http< 完成的/代码>。如果客户端已通过 https 连接,则不会发生重定向。

This piece of code is redirecting the client to use https in case it does not use https. 301 tells the client to always use the new location.

The redirection is done via the Location header, and you are only partly right, the page is the same, but the connection is done via https and not http. If the client is already connected via https, the redirect does not occur.

他是夢罘是命 2024-12-16 05:32:17

1) HTTP/1.1 301是为了让浏览器和搜索引擎知道页面已经移动到https://example。 com/thepage.php

2) 此代码用于需要 SSL 的特定页面或网站。示例:购物车、银行、登录

1) HTTP/1.1 301 is for letting the browser and search engines know that the page has moved to https://example.com/thepage.php

2) This code is used for specific pages or sites that require SSL. Example: Shopping carts, banks, logins

嘦怹 2024-12-16 05:32:17

HTTPS 默认通过端口 443。如果用户通过纯 HTTP 访问,此代码将使用位置标头将用户重定向到 HTTPS。

301 指示浏览器在该 URL 上的后续请求时始终将 HTTP 重定向到 HTTPS。

在伪代码中,它这样做:

if (user is NOT visiting through HTTPS)
  redirect permanently to the same URL, but then through HTTPS

所以不,这不是多余的,因为如果有人使用 HTTPS 访问该页面,则 if 块的主体将不会被执行,并且不会发生重定向。

HTTPS goes through port 443 by default. This code redirects the user, using the Location-header, to the HTTPS if the user is visiting through plain HTTP.

The 301 instructs the browser to always redirecting HTTP to HTTPS on succeeding requests on that URL.

In psuedo-code it does this:

if (user is NOT visiting through HTTPS)
  redirect permanently to the same URL, but then through HTTPS

So no, it's not redundant, for if anyone visits that page using HTTPS, the body of the if-block will not be executed and no redirect takes place.

多情出卖 2024-12-16 05:32:17

首先,该函数按照您的想法执行。

让我们逐一浏览您想要了解更多信息的每个内容:

header("HTTP/1.1 301 Moved Permanently");

这设置 HTTP 响应状态代码。代码:301。消息:永久移动301 是一个永久重定向,这只是告诉浏览器(HTTP 客户端),下次直接使用新地址而不是请求的地址。

新地址由 Location: 标头给出。

header("Location: .");

这段代码只是简单的错误代码。不要指望它有效。我假设有人想再次重定向到同一页面,但这不起作用。 Location: 后面需要跟一个绝对 URL,这是相对 URL,不起作用。绝对 URL 始终以 http://https:// 开头,并包含主机名。

First of all, the function does what you think.

Let's go through each of those you want to learn more about:

header("HTTP/1.1 301 Moved Permanently");

This sets the HTTP response status code. Code: 301. Message: Moved Permanently. 301 is a permanent redirect, this just tells the browser (HTTP client), next time to use the new address directly instead of the requested one.

The new address is given with a Location: header.

header("Location: .");

This code is just plain wrong code. Don't expect that it works. I assume somebody wanted to redirect to the same page again, but this does not work this way. A Location: needs to be followed by an absolute URL, this is a relative URL and won't work. Absolute URLs always start with http:// or https:// and contain a host-name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文