简体   繁体   English

从未登录的访问者保护我的框架的最佳技术是什么?

[英]What's the best technique to protect my framework from visitors who are not logged in?

First of all, I would like to say that I have used the search box looking for a similar question and was unsuccessful, maybe because of my poor english skills. 首先,我想说我使用搜索框寻找类似的问题并且没有成功,可能是因为我的英语能力差。

I have aa 'homemade' framework. 我有一个'自制的'框架。 I have certain PHP files that must only be visible for the admin. 我有一些必须仅对管理员可见的PHP文件。 The way I currently do this is check within every single page to see if a session has been opened. 我目前这样做的方法是在每个页面内检查以查看会话是否已打开。 If not, the user gets redirected to a 404 page, to seem like the file which has been requested doesn't exist. 如果没有,则用户被重定向到404页面,看起来好像已经请求的文件不存在。

I really don't know if this is guaranteed to work or if there's a better and more safe way because I'm currently working with kind of confidential data that should never become public. 我真的不知道这是否可以保证工作,或者是否有更好更安全的方式,因为我目前正处理一些永远不会公开的机密数据。

Could you give me some tips? 你能给我一些提示吗? Or leave a link where I could find some? 或者留下我能找到的链接?

Thank you very much, and again excuse me for kicking the dictionary. 非常感谢,再次请原谅我踢字典。

EDIT 编辑

What I usually write in the top of each file is something like this 我通常在每个文件的顶部写的是这样的

<?php
include("sesion.php");
$rs=comprueba(); //'check'

if ($rs) { 
?> 

And then, at the end 然后,最后

<?php 
}
else { header("Location: err404.html"); }
?>

Is it such a butched job, isn't it? 这是一个如此划职的工作,不是吗?

EDIT 编辑

Let's say I have a customers list in a file named customers.php 假设我在名为customers.php的文件中有一个客户列表

That file may be currently on http://www.mydomain.com/admin/customers.php and it must only be visible for the admin user. 该文件当前可能位于http://www.mydomain.com/admin/customers.php ,并且只有admin用户才能看到该文件。 Once the admin user has been logged in, I create a session variable. 管理员用户登录后,我创建了一个会话变量。 That variable is what I check on the top of each page, and if it exists, the customers list is shown. 该变量是我在每个页面顶部检查的变量,如果存在,则显示客户列表。 If not, the user gets redirected to the 404 page. 如果没有,则用户被重定向到404页面。

Thank you for your patience. 感谢您的耐心等待。 I really appreciate. 我真的很感激。

Apologies if I'm incorrect in interpreting your question but I think you're asking the best way to protect individual PHP pages used in the framework from people typing in the URL to view them? 抱歉,如果我在解释你的问题时不正确,但我认为你要问最好的方法来保护框架中使用的个人PHP页面,以及输入URL的人们查看它们?

If so, the best route I've found is to declare a constant in your master file (usually index.php). 如果是这样,我发现的最佳路径是在主文件中声明一个常量(通常是index.php)。

<?php
define( '_MYAPP', 1 );

Then, at the top of each PHP file ( before you define your classes) put - 然后,在每个PHP文件的顶部( 您定义类之前 )put -

<?php
defined( '_MYAPP' ) or die( 'No access.' );

I strongly recommend you use sessions. 我强烈建议你使用会话。

Now, i think there's two ways to do this. 现在,我认为有两种方法可以做到这一点。

Easiest way I can think of is: make a session.php file and include/require it in every file in your application. 我能想到的最简单的方法是:创建一个session.php文件,并在应用程序的每个文件中包含/要求它。

In this session.php do a session check for security tokens you can define when the user succesfully logs in (preferably an encrypted salted string). 在此session.php中,对用户成功登录时可以定义的安全令牌进行会话检查(最好是加密的盐渍字符串)。

Edit: What I do in session.php file is die(); 编辑:我在session.php文件中做的是die(); or redirect with header(); 或使用header()重定向; if no correct session is detected. 如果没有检测到正确的会话。

If you want, you can add an array of "public" files so that session check is skipped if one of those files is currently being executed. 如果需要,可以添加一组“公共”文件,以便在当前正在执行其中一个文件时跳过会话检查。

The other harder way to do this (still using sessions and token verification) would be creating a dispatcher file that checked sessions and then redirected requests to a view that rendered the requested action. 另一种更难的方法(仍使用会话和令牌验证)将创建一个调度程序文件,该文件检查会话,然后将请求重定向到呈现所请求操作的视图。

If security is vital in your app, You should read this guide: PHP Security Guide: Overview by the php security consortioum. 如果安全性在您的应用程序中至关重要,您应该阅读本指南: PHP安全指南: php安全联盟概述

<?php
$logged_in = 'no';
include("session.php"); // changes $logged_in to yes if logged in

if($logged_in == 'no'){
header("Location: login.php?error=notloggedin");
exit;
}
?>

you can either put this at the top of all of your pages, or simply put this in your session.php file, or make a header.php file to include in all pages. 您可以将它放在所有页面的顶部,或者只是将它放在session.php文件中,或者将header.php文件包含在所有页面中。

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

相关问题 在我的网页上计算唯一身份访问者的最佳方法是什么? - What's the best way to count unique visitors on my webpage? 在where子句中进行长时间比较的选择记录的最佳技术是什么 - what's the best technique for selecting records with long comparison in where clause 从登录的社交网络获取访问者姓名 - Get Visitors Name from Logged In Social Networks 在PHP中保护登录cookie数据的最佳方法是什么? - What's the best method to protect login cookie data in PHP? 检查登录用户的帐户是否仍与数据库匹配或存在的最佳解决方案是什么? - What is the best solution for checking if a logged-in user's account still matches or exists from the database? Laravel,捕获访客IP的最佳方法是什么? - Laravel, What is best approach to capture Visitors Ip? 如何跟踪我的访客? [最佳表现] - How to track my visitors ? [best perfomance] 如何保护public_html中的文件夹免受访问者的攻击 - How to protect folders in public_html from visitors 记录谁使用我的PHP网站从S3下载了什么文件 - Record who has downloaded what file from S3 using my PHP website 构建可扩展(可扩展),可维护和松散耦合的软件的最佳技术是什么? - What's the best technique to build scalable (extensible), maintainable, and loosely coupled software?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM