禁止直接访问

发布于 2024-12-17 02:30:17 字数 1084 浏览 0 评论 0原文

基本上,我有两个文件,位于两个不同的目录中:index.php(在/login/中)和index.php(在/login/buyer/中)。

/login/buyer/index.php 文件有:

<?php
session_start();

function isLoggedIn()
{
    if(isset($_SESSION['valid']) && $_SESSION['valid'])
        return true;
    return false;
}

//if the user has not logged in
if(!isLoggedIn())
{
    header('Location: index.php');
    die();
}
?>

<?php 
    if($_SESSION['valid'] == 1){
        echo "<a href='../logout.php'>Logout</a>";
    }
    else{
        echo "<a href='../index.php'>Login</a>";
    }
?>

/login/index.php 文件有:

<!--

an form to login

-->

<?php 
    if($_SESSION['valid'] == 1){ #user has logged in by creating a session var
        echo "<a href='logout.php'>Logout</a>";
    }
    else{
        return true;
    }
?>

这两个文件似乎工作正常,但比如说恶意用户想要尝试在不提供凭据的情况下直接访问 /login/buyer/,并且只想在 URL 末尾进行尝试,例如 mysite.com/login/buyer >,而不是登录。如何我在那里添加另一个条件来阻止这个?

Basically, I have two files, in two different directories: index.php (in /login/) and index.php(in /login/buyer/) .

The /login/buyer/ index.php file has:

<?php
session_start();

function isLoggedIn()
{
    if(isset($_SESSION['valid']) && $_SESSION['valid'])
        return true;
    return false;
}

//if the user has not logged in
if(!isLoggedIn())
{
    header('Location: index.php');
    die();
}
?>

<?php 
    if($_SESSION['valid'] == 1){
        echo "<a href='../logout.php'>Logout</a>";
    }
    else{
        echo "<a href='../index.php'>Login</a>";
    }
?>

The /login/ index.php file has:

<!--

an form to login

-->

<?php 
    if($_SESSION['valid'] == 1){ #user has logged in by creating a session var
        echo "<a href='logout.php'>Logout</a>";
    }
    else{
        return true;
    }
?>

These two files seem to be working fine, but say for instance a malicious user wants to try to directly access /login/buyer/ without providing an credentials and wants to just try that at the end of the url like mysite.com/login/buyer, instead of logging in. How can I add another condition in there to stop this?

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

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

发布评论

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

评论(3

帝王念 2024-12-24 02:30:17

我建议对所有 Web 访问使用单个 /index.php 文件(即“控制器”)。

这样,您必须对所有文件检查一次会话是否有效。

使用参数来告知您要继续执行哪个“模块”和“操作”(Symfony 1.x 词汇)。

例如 yoursite.com/index.php?module=user&action=login&type=buyer,或者使用 .htaccess 和路由引擎更好:yousite .com/user/login/buyer

正如我建议的在另一个问题中,甚至最好将 index.php 和其他源文件(不应直接通过网络服务器访问)放在单独的目录中。

其他建议

function isLoggedIn()
{
    if(isset($_SESSION['valid']) && $_SESSION['valid'])
        return true;
    return false;
}

可以简单地重写为:

function isLoggedIn()
{
    return (isset($_SESSION['valid']) && $_SESSION['valid']);
}

I suggest to use a single /index.php file (that is, a "controller") for all web accesses.

This way, you have to check once for all files if the session is valid or not.

Use parameters to tell which "module" and "action" (Symfony 1.x vocabulary) you want to proceed.

E.g. yoursite.com/index.php?module=user&action=login&type=buyer, or better with a .htaccess and a routing engine: yousite.com/user/login/buyer

As I suggest in this other question, it is even better to put the index.php and the other source files (which should not be accessed directly through the webserver), in separate directories.

Additional advice:

function isLoggedIn()
{
    if(isset($_SESSION['valid']) && $_SESSION['valid'])
        return true;
    return false;
}

could be simply rewritten to:

function isLoggedIn()
{
    return (isset($_SESSION['valid']) && $_SESSION['valid']);
}
终陌 2024-12-24 02:30:17

您应该始终检查每个页面上的有效会话。

看起来你的代码就是这样做的(尽管我的 PHP 很生锈)。

如果不是买家的人登录然后使用买家页面,您需要将权限/角色与每种类型的帐户相关联,并在需要时检查是否有适当的权限。

例如,在买家页面上,您可能

function isLoggedIn()
{
    if(isset($_SESSION['valid']) && $_SESSION['valid'] && isset('isBuyer') && $_SESSION['isBuyer'])
        return true;
    return false;
}

需要根据提供的用户 ID 在登录时填充 $_SESSION['isBuyer'] 之类的内容。

You should always check for a valid session on every page.

It looks like your code does that (though my PHP is rusty).

If it's matter of someone who is not a buyer logging in and then using the buyer page, you need to associate permissions / roles with each type of account and check for appropriate permissions where needed.

For example, on the buyer page, you could have something like

function isLoggedIn()
{
    if(isset($_SESSION['valid']) && $_SESSION['valid'] && isset('isBuyer') && $_SESSION['isBuyer'])
        return true;
    return false;
}

You would have to populate $_SESSION['isBuyer'] at the time of logon, based on the provided user ID.

丑丑阿 2024-12-24 02:30:17

在到达实际支付页面或买家索引之前强制用户再次登录被认为是最终的安全措施。

这是因为真正的授权信息在会话变量中并不安全,因为 PHP 用来识别每个用户的会话 ID 是通过 GET 变量(在 URL 中)或作为 cookie 传递的。两者都很容易通过不安全的网络或 XSS 漏洞被窃取。

除了会话检查之外,您还应该有一个 SSL 加密的登录页面,以确保用户在进入网站的关键区域之前已登录。

It's considered ultimate security to force the user to log-in AGAIN before reaching the actual payment page or buyer index.

That's because true authorization information is not safe in session variables, as the session ID, which PHP uses to identify each user, is passed by either the GET variables (in the URL) or as a cookie. Both are very easy to steal over an unsecured network or XSS vulnerabilities.

You should have an SSL encrypted login page IN ADDITION to the session check to make sure the user is logged in before entering the critical area in your website.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文