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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 仅对登录页面使用https而不是整个网站脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想打开我的网站的登录页面只有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']) &amp;& $_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
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – 仅对登录页面使用https而不是整个网站全部内容,希望文章能够帮你解决php – 仅对登录页面使用https而不是整个网站所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。