简体   繁体   English

PHP是否有比使用标头(位置)更好的方法来保护我的页面?

[英]PHP Is there a better way to protect my pages than using header(location)?

I am protecting my pages by checking the values of my sessions. 我通过检查会话的值来保护自己的页面。 Is there a more secure way of protecting my pages other than changing the Header Location if the sessions are not valid??? 如果会话无效,除了更改页眉位置之外,还有一种保护我的页面的更安全的方法吗??? Am I doing anything right??? 我做对了吗???

I have the following at the top of each page: 每个页面的顶部都有以下内容:

<?php
     session_start();

     //VERIFY LOGIN
     $validkey = 'br1ll1ant)=&';

     if ($_SESSION['valid'] != (hash('sha256',$validkey)) && $_SESSION['tokenconfirm'] != hash('sha256',$_SESSION['tokenID']))  {

            header("location:/login/");

         };

?>

using header() is fine, but don't forget to exit(); 使用header()很好,但不要忘记exit(); your script after calling header() . 调用header()之后的脚本。 User agents don't have to respect headers, so one could write a client which will simply read the part that comes after the header call. 用户代理不必尊重标头,因此可以编写一个客户端,该客户端将仅读取标头调用之后的部分。

if(!session_is_valid()) {
  header('Location: index.php');
  exit;
}

Are you using a templating system? 您正在使用模板系统吗? If you are, what you'd do is simply output the login form instead of the page content if the user isnt validated. 如果您是,那么您要做的就是简单地输出登录表单,而不输出页面内容(如果用户未通过验证)。 Even if you arent using one, you can change the output (different set of includes, for example), if the user isnt valid. 即使您不使用它,也可以在用户无效的情况下更改输出(例如,包括不同的一组)。 This way you arent relying upon the end user's browser to protect the content. 这样,您就不会依赖最终用户的浏览器来保护内容。

Headers should be fine, I haven't seen people use much anything else. 标头应该没问题,我还没有看到人们使用很多其他东西。

It is always best to authenticate to gain access to the page, and then check that authentication on every page. 始终最好进行身份验证以访问该页面,然后检查每个页面上的身份验证。 If it fails, redirect to the login. 如果失败,请重定向到登录名。

Using a MVC pattern, it is best to check the login status before they even get to a page, and either redirect if not logged in, or load the logged in view. 使用MVC模式,最好在登录页面之前检查登录状态,如果没有登录,则进行重定向,或者加载已登录的视图。

Using a front controller pattern you can put all your php files outside the web root. 使用前端控制器模式,您可以将所有php文件放在网络根目录之外。 That way they are not directly accessible from a URL. 这样,就不能直接通过URL访问它们。 This is fairly common practice in PHP frameworks include those built with Zend 'Framework'. 这在PHP框架(包括使用Zend'Framework'构建的框架)中是相当普遍的做法。

If your files are in the web root, another method that you might consider is to use constants. 如果文件位于Web根目录中,则可能考虑的另一种方法是使用常量。 This is how CodeIgniter does it. 这就是CodeIgniter的工作方式。 Define a constant in your front controller and if its not defined send them to the web root. 在前端控制器中定义一个常量,如果未定义,则将其发送到Web根目录。 Here is how to CI uses constants. 这是CI如何使用常量的方法。

The constant used everywhere 随处使用的常数

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

How it is defined. 如何定义。

define('BASEPATH', $system_folder.'/');

$system_folder being a few lines above. $ system_folder在上面几行。

$system_folder = realpath(dirname(__FILE__)).'/'.$system_folder;

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

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