3

我在我的 PHP 页面中遵循了 MVC 模式。我为每个视图页面创建了一个控制器页面,它将与模型页面进行交互。我已经检查了每个视图页面顶部的用户登录。但我从未检查过控制器页面。因为任何人都可以直接进入控制器页面并可能更改数据库内容。

所以有人请告诉我,是否可以通过控制器页面进入数据库。以及我是否需要在控制器页面中检查用户的登录。

4

1 回答 1

4

如果我理解正确,您将在控制器页面的每个链接页面的顶部放置一个登录检查(假设您的 index.php)。

如果是这样,那么您不需要在每个链接页面的顶部进行登录检查,只需从 index.php 进行。如果您在 index.php 的顶部登录成功,那么您将继续包含您的目标视图页面。

例如(在您的 index.php 中)

<?php

if ( !$user->checkSession() )
    header('Location: login.php');

if ( $_GET['p'] == 'viewPageName' )
    include('modules/viewPageName.php' )
elseif and so on

?>

编辑

现在我对你的问题有了更好的理解。

解决方案 1
在您的index.php中(放在顶部)

<?php
define('DIRECT', true);

your login check, etc...
?>

在您的其他文件中,放在顶部

<?php
if (!defined('DIRECT')) die('No direct access is allowed');

other code, etc...
?>

解决方案 2:
将 .htaccess 文件放在所有其他文件所在的文件夹中,并拒绝直接访问这些文件。

在 .htaccess 中放入以下内容:

deny from all

Solution 3:
Well, assuming you've defined the $db file at the index.php and have set the class up in your index.php, your other files will return errors because you didn't define your DB class in them.

In other words, if you defined $db = new Database(); in your index.php, your other files will get an error if you try to access directly because $db has yet to be defined in them.

于 2013-01-25T06:08:32.150 回答