加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 仅对登录页面使用https而不是整个网站

发布时间:2020-12-13 17:06:03 所属栏目:PHP教程 来源:网络整理
导读:我想打开我的网站的登录页面只有https,而不是comeplete网站.登录验证(成功)后,网站再次在http上运行. 目前我的主要登录页面是test_index.php,其中包括test_header.php 我在test_header.php上的基本代码是 if($_SERVER['SERVER_PORT'] != 443) { header("HTTP
我想打开我的网站的登录页面只有https,而不是comeplete网站.登录验证(成功)后,网站再次在http上运行.

目前我的主要登录页面是test_index.php,其中包括test_header.php

我在test_header.php上的基本代码是

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

但这使得完整的网站在https
?我也读了here,它可以通过.htaccess,所以我从test_header.php删除上面的代码片段并在.htaccess文件中添加以下行

<IfModule mod_rewrite.c>
  RewriteEngine on
  # 301 redirect to domain to 'www.'
  RewriteCond %{HTTP_HOST} ^testweb.com$[NC]
  RewriteRule ^(.*)$http://www.testweb.com/$1 [R=301,L]
</IfModule>

<FilesMatch test_index.php>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
</FilesMatch>

注意:testweb.com只是一个虚构的名称,而不是实际的网站

但仍然完整的网站运行https,请告诉我我在哪里做错了?

编辑

@webbiedave请检查我更新的代码,是正确的方法??

if ($_SERVER['REQUEST_URI'] == '/test_index.php') { // only check https on login
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
        exit();
    } else {
        die("Sorry,Your website is not secure");        
    }
} elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
        // header("HTTP/1.1 301 Moved Permanently");
        header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
        exit();
}

谢谢

解决方法

不要检查端口号以验证https,因为https不是不可能 – 尽管非常不可能 – 在非标准端口上.而是,检查$_SERVER [‘HTTPS’]变量:

if ($_SERVER['REQUEST_URI'] == '/login.php') { // only check https on login
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
        // do login stuff
    } else {
        // redirect to https or simply give an error
    }
} elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
    // redirect to http
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读