简体   繁体   English

如果页面是PHP,则PHP重定向到HTTPS

[英]PHP redirect to HTTPS if page is

I need a PHP if/else statement that if the sign-in.php or register.php page is access over HTTP I would like to redirect to HTTPS else if any other page is accessed over HTTPS redirect to HTTP plus have any query string appended for example if a user tries to access a restricted page ( http://domain.com/my-account.php ) the site redirects the user to http://domain.com/sign-in.php?redirect=my-account however, I would like the page to redirect to https://domain.com/sign-in.php?redirect=my-account . 我需要一条PHP if / else语句,如果sign-in.php或register.php页面是通过HTTP访问的,我想重定向到HTTPS否则,如果通过HTTPS访问任何其他页面,则重定向到HTTP加上任何查询字符串例如,如果用户尝试访问受限页面( http://domain.com/my-account.php ),则该网站会将用户重定向到http://domain.com/sign-in.php?redirect=my-占然而,我想在页面重定向到https://domain.com/sign-in.php?redirect=my-account

I know I could simply change the header redirects to include https instead of http but users may type http://domain.com/sign-in.php?redirect=my-account so just need to ensure if this happens sign in (or others) happen over https. 我知道我可以简单地将标头重定向更改为包括https而不是http,但是用户可以键入http://domain.com/sign-in.php?redirect=my-account,因此只需确保这种情况登录即可(或其他)通过https进行。

Any help is appreciated 任何帮助表示赞赏

Here You go. 干得好。

//force the page to use ssl 
if ($_SERVER["SERVER_PORT"] != 443) {
    $redir = "Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header($redir);
    exit();
}

$_SERVER , It is an array containing information such as headers, paths, and script locations. $ _SERVER ,它是一个包含诸如标题,路径和脚本位置之类的信息的数组。

You can check against $_SERVER, specifically 'SERVER_PROTOCOL' 您可以检查$ _SERVER,特别是“ SERVER_PROTOCOL”

http://php.net/manual/en/reserved.variables.server.php http://php.net/manual/en/reserved.variables.server.php

There should be a part of your code that is always run on every page. 您的代码中应该有一部分始终在每个页面上运行。 In an MVC it would be in your base controller. 在MVC中,它将在您的基本控制器中。 Other designs may include an init.php file on every page. 其他设计可能在每个页面上都包含一个init.php文件。

In this file have a whitelist of pages that require HTTPS. 此文件中包含需要HTTPS的页面白名单。

$requires_https = array(
    'sign-in.php' => TRUE,
    'register.php' => TRUE
);

Then you need to determine which page was requested. 然后,您需要确定请求的页面。

$url_info = pathinfo($_SERVER['REQUEST_URI']);
$page = $url_info['filename'];

Next check if you are on HTTP or HTTPS 下一步检查您使用的是HTTP还是HTTPS

$is_secure = ! empty($_SERVER['HTTPS']);

Finally you can do the checking: 最后,您可以进行检查:

if (isset($requires_https[$page]) AND ! $is_secure)
    header('Location: https://www.yoursite.com/' . $page);
elseif ( ! isset($requires_https[$page]) AND $is_secure)
    header('Location: http://www.yoursite.com/' . $page); 

This could definitely be improved upon in the last part by using a custom redirect function and a site_url function that takes in the option of being secure or not and builds the proper URL. 通过使用自定义重定向功能和site_url函数,可以肯定地在最后一部分上对此进行了改进,该函数接受是否为安全选项并构建正确的URL。

It is worth mentioning that it generally doesn't matter if someone is left surfing in HTTPS. 值得一提的是,是否有人在HTTPS中浏览通常并不重要。 In fact most of Google's services are in HTTPS and with better internet connections surfing will eventually all be done in HTTPS. 实际上,Google的大多数服务都使用HTTPS,并且具有更好的Internet连接,最终将全部使用HTTPS进行冲浪。 It is only important to make sure the pages that should be secure are secure, not make sure that pages that don't need to be secure aren't. 重要的是要确保应该安全的页面是安全的,而不是确保不需要安全的页面不是安全的。

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

Using part of an init class / script - You can run code to check if this page should require SSL prior, but this is the actual code to force redirect to SSL (and REQUEST_URI provides any dirs, etc.. to get the correct path). 使用init类/脚本的一部分-您可以运行代码来检查该页面是否需要先使用SSL,但这是强制重定向到SSL的实际代码(REQUEST_URI提供了所有目录等,以获取正确的路径)。 。

Using on a single page (ie sign-in and register) - This will redirect the user to this page in SSL (put this code near the top). 在单个页面上使用(即登录和注册)-这会将用户重定向到使用SSL的此页面(将此代码放在顶部附近)。

The 301 Moved Permanently will also prevent any negative SEO. 永久移动301也可以防止产生负面的SEO。

(A more) complete method: (includes the query string) (更多)完整的方法:(包括查询字符串)

To determine if on https: 要确定是否在https上:

$secure = (!empty(filter_input(INPUT_SERVER, 'HTTPS')) &&
  filter_input(INPUT_SERVER, 'HTTPS') !== 'off') ||
  filter_input(INPUT_SERVER, 'SERVER_PORT') == 443;

as per https://stackoverflow.com/a/2886224 根据https://stackoverflow.com/a/2886224

Then to redirect and include the query string: 然后重定向并包含查询字符串:

if($secure){
  header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}

Using REQUEST_URI instead of PHP_SELF will include the query parameters (things after the ? in the URL). 使用REQUEST_URI代替PHP_SELF将包含查询参数(URL中?后面的内容)。

And as always filter your user input (including these) with filter_input() or the like. 和往常一样,使用filter_input()之类的来过滤用户输入(包括这些输入filter_input()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM