Php 如何根据用户将用户重定向到特定的子文件夹

Php 如何根据用户将用户重定向到特定的子文件夹,php,html,.htaccess,redirect,directory,Php,Html,.htaccess,Redirect,Directory,我正在制作一个显示文件夹中文件的网页。我从开源网站上得到了这个网页。我想创建一种方式,根据用户的不同,签名将被重新定向到仅为其指定的文件夹。我能够创建一个使用户登录的.htaccess和一个具有登录凭据的.htpasswd 比如说, 每个文件夹中有3个用户(user1、user2、user3)和3个带有index.php的文件夹(folder1、folder2、folder3) 当“user1”登录时,它们会被重定向到“folder1”, 如果“user2”登录,它们会被重定向到“folder2

我正在制作一个显示文件夹中文件的网页。我从开源网站上得到了这个网页。我想创建一种方式,根据用户的不同,签名将被重新定向到仅为其指定的文件夹。我能够创建一个使用户登录的.htaccess和一个具有登录凭据的.htpasswd

比如说,

每个文件夹中有3个用户(user1、user2、user3)和3个带有index.php的文件夹(folder1、folder2、folder3)

当“user1”登录时,它们会被重定向到“folder1”, 如果“user2”登录,它们会被重定向到“folder2”或“folder3”,我希望它们也被重定向

这可能与.htaccess文件或类似于php文件有关吗

注意:PHP、.htaccess编码知识有限公司!:(

我希望有人能帮我,或者给我指出正确的方向,如果你需要任何额外的信息,请告诉我!谢谢

下面是我的index.php


发票
目录内容
文件名
类型
大小
修改日期

好的,我用PHP手册中的代码为您做了一个快速的模拟。我还测试了它,并完全按照您的要求执行

请确保“username”文件夹是在创建之前创建的,在我的示例中,我有:'Norbert1','Norbert2','Norbert3',这意味着必须有一个名为'Norbert1','Norbert2','Norbert3'的文件夹

<?php
ob_start();

$realm = 'Restricted area';

$users = [
        'Norbert1' => '123456', 
        'Norbert2' => '123456',
        'Norbert3' => '123456'
];


if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
    header('HTTP/1.1 401 Unauthorized');
    header('WWW-Authenticate: Digest realm="'.$realm. '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
    die('This website requires authorization');
}


// analyze the PHP_AUTH_DIGEST variable
if (!($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) || !isset($users[$data['username']]))
{
    header('HTTP/1.1 401 Unauthorized');
    die('Invalid Credentials or no such user exists!');
}

    // generate the valid response
    $A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]);
    $A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
    $valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);

    if ($data['response'] != $valid_response)
    {
        header('HTTP/1.1 401 Unauthorized');
        die('Invalid Credentials or no such user exists!');
    }
        // ok, valid username & password
        echo 'You are logged in as: ' . $data['username'] . PHP_EOL;

        header("Location: /". $data['username']."/");

        // function to parse the http auth header
        function http_digest_parse($txt)
        {
            // protect against missing data
            $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
            $data = array();
            $keys = implode('|', array_keys($needed_parts));

            preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);

            foreach ($matches as $m) {
                $data[$m[1]] = $m[3] ? $m[3] : $m[4];
                unset($needed_parts[$m[1]]);
            }

            return $needed_parts ? false : $data;
    }
?>

发布你的代码,否则没人能猜到你有什么……我知道那是什么代码?我的index.php?还是htaccess?@NorbertBorosPost你所有的东西,这样我们就可以帮助你了……好的,我已经添加了代码。@NorbertBorosOk,先别管通过.htacess做了。其次,这些是“文件夹”吗为每个用户预先创建?如果是这样,您可以轻松重定向它们,因为基本上是一个“文件夹”将变成:example.co/folder/index.php。第三,您没有为用户登录编写任何东西,因此如果您想保留.htaccess方法,您需要手动创建文件夹,并始终更新htpasswd。它工作得非常好!正是我需要的。非常感谢!我不小心跳过了一个文件夹,而不必登录,所以我没有做example.com/indexpage/i,而是做了example.com/indexpage/Norbert1/,这样我就可以不用登录就能看到页面上的所有PDF,因此,出于安全原因,我如何创建if语句或其他东西来检查我或用户是否登录,这就是我问的原因,谢谢。这是另一种情况。。。我目前没有时间,但我将尝试为您制作另一个。是否有办法获取登录用户的用户名?是的,$data['username']包含该用户名。