PHP 有没有比使用 header(location) 更好的方法来保护我的页面?

发布于 2024-09-16 13:37:50 字数 406 浏览 3 评论 0原文

我通过检查会话值来保护我的页面。如果会话无效,除了更改标题位置之外,是否有更安全的方法来保护我的页面???我做的对吗???

我在每页的顶部都有以下内容:

<?php
     session_start();

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

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

            header("location:/login/");

         };

?>

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/");

         };

?>

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

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

发布评论

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

评论(4

成熟的代价 2024-09-23 13:37:50

使用 header() 没问题,但不要忘记在调用 header() 后执行 exit(); 脚本。用户代理不必尊重标头,因此可以编写一个客户端,它只需读取标头调用之后的部分。

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

using header() is fine, but don't forget to exit(); your script after calling 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;
}
拿命拼未来 2024-09-23 13:37:50

您使用模板系统吗?如果是的话,如果用户未经验证,您要做的只是输出登录表单而不是页面内容。即使您没有使用,如果用户无效,您也可以更改输出(例如,不同的包含集)。这样您就不会依赖最终用户的浏览器来保护内容。

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.

不可一世的女人 2024-09-23 13:37:50

标题应该没问题,我还没有看到人们使用太多其他东西。

最好先进行身份验证才能访问该页面,然后在每个页面上检查该身份验证。如果失败,则重定向到登录。

使用 MVC 模式,最好在到达页面之前检查登录状态,如果未登录则进行重定向,或者加载登录视图。

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.

勿忘心安 2024-09-23 13:37:50

使用前端控制器模式,您可以将所有 php 文件放在 Web 根目录之外。这样就无法通过 URL 直接访问它们。这是 PHP 框架中相当常见的做法,包括使用 Zend“框架”构建的框架。

如果您的文件位于 Web 根目录中,您可能会考虑的另一种方法是使用常量。 CodeIgniter 就是这样做的。在前端控制器中定义一个常量,如果未定义,则将它们发送到 Web 根目录。以下是 CI 使用常量的方法。

随处使用的常量

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

如何定义。

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

$system_folder 是上面几行。

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

Using a front controller pattern you can put all your php files outside the web root. That way they are not directly accessible from a URL. This is fairly common practice in PHP frameworks include those built with Zend 'Framework'.

If your files are in the web root, another method that you might consider is to use constants. This is how CodeIgniter does it. Define a constant in your front controller and if its not defined send them to the web root. Here is how to CI uses constants.

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 = realpath(dirname(__FILE__)).'/'.$system_folder;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文