1

希望我能很好地解释我的问题。我尝试搜索类似的问题,但找不到任何东西,我也可能只是在搜索错误的术语。

我正在开发一个新网站,用户可以在其中登录并修改他们的个人资料、提交食谱等。

当我第一次开始使用该网站时,我做了它,以便用户必须登录才能开始。如果他们未登录并转到个人资料页面,他们将被定向到主页。

但是现在我正在考虑更多关于该站点的内容,并且大部分内容都是隐藏的,因为您需要登录才能查看它。这不允许人们分享他们的页面,这对搜索引擎优化也不是很好。

如何创建可以查看基本内容的页面。但如果登录,额外的功能就在那里(更新他们的个人资料,提交食谱等)。

这是我的配置文件,它检查他们是否已登录,如果没有则重定向到主页。我尝试只删除标题,但是在未登录时我只是得到空白页。

    //Function to check if user is logged in, and if so, return user data as an object
function check_user($secret_key, &$db) {
    if (!isset($_SESSION['userid']) || !isset($_SESSION['hash'])) {
        header("Location: index.php");
        exit;
    } else {
        $check = sha1($_SESSION['userid'] . $_SERVER['REMOTE_ADDR'] . $secret_key);
        if ($check != $_SESSION['hash']) {
            session_unset();
            session_destroy();
           header("Location: index.php");
            exit;
        } else {
            $query = $db->execute("select `id`,`nickname`, `joindate`, `last_active` from `Profile`  where `id`=?", array($_SESSION['userid']));
            $userarray = $query->fetchrow();
            if ($query->recordcount() == 0) {
                session_unset();
                session_destroy();
               header("Location: index.php");
                exit;
            }
            foreach ($userarray as $key => $value) {
                $user->$key = $value;
            }
            $query = $db->execute("update `Profile` set `last_active`=? where `id`=?", array(time(), $user->id));
            return $user;
        }
    }
}

然后在当前受保护的页面上。我会添加这个。

$userprofile = check_user($secret_key, $db);

我仍然希望能够使用 $userprofile 对象来验证用户。但我仍然希望“不安全”的内容可以查看,而不是将它们重定向到主页。

有没有办法修改我所拥有的,希望其中一些是可以保存的。我正在使用 ADODB。

4

1 回答 1

0

您将需要以不再重定向自身的方式重写该函数。这可以通过重写它来完成,使其返回用户对象或NULL. 如果它返回NULL,调用者可以决定是否可以发生重定向。

如果您不想一次重写所有代码,您可以在函数中添加一个新参数,该参数指示是否在您的登录检查函数中进行重定向。该参数应该有一个默认值,让函数像往常一样运行。

function check_user($secret_key, &$db, $redicrect = TRUE) {
    if (!isset($_SESSION['userid']) || !isset($_SESSION['hash'])) {
        if ($redirect) {
            header("Location: index.php");
            exit;
        }
        return NULL;
    }
    $check = sha1($_SESSION['userid'] . $_SERVER['REMOTE_ADDR'] . $secret_key);
    if ($check != $_SESSION['hash']) {
        if ($redirect) {
            session_unset();
            session_destroy();
            header("Location: index.php");
            exit;
        }
        return NULL;
    }
    //...
}
于 2018-01-22T21:52:05.713 回答