1

我已经创建了一个用户现在可以成功登录的网站,但是根据用户所在的组,我想在登录后将它们重定向到不同的页面。我有一个带有“training_group”行的数据库,例如,他们在组2013_1,他们将在登录后被引导到homepage_20131.php。

我一直在网上寻找教程,并找到了具有开关功能的可能解决方案?但我不确定如何/在哪里实现这一点。我刚开始学习 php,如果有任何建议,我将不胜感激!

现在,我的登录页面如下所示:

<?php
include 'core/init.php'; 
if (empty($_POST) === false) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    if (empty($username) === true || empty($password) === true) {
        $errors[] = 'please input a username and password first! ';
    } else if (user_exists($username) === false)    {
        $errors[] = 'We could not locate you in our database.';
    }    
    $login = login($username, $password);
    if ($login === false) {
        $errors [] = 'That username/password combination is incorrect';
    }    
    else {
        $_SESSION['user_id'] = $login;
        header('Location:logged_in/templates/logged_in_home.php');
        exit ();
    }
}
else {
    $errors [] = 'No data received';
}

include 'includes/overall/header.php';

if (empty ($errors) === false) {
    ?>
    <h2>We tried to log you in, but...</h2>
    <?php
    echo output_errors($errors);
}
include 'includes/overall/footer.php'; 
?>
4

1 回答 1

0

这里有几个片段可能会让你朝着正确的方向前进。

function login($username, $password){
    //... your login code .. database call
    if($validLogin){
        $user_id = id from database;
        $group_id = id from database;
        $return = array('user_id' => $user_id, 'group_id' => $group_id);
    }
    else{
        $return = false;
    }

    return $return;
}

$userinfo = login($username, $password);
if ($userinfo === false) {
    $errors [] = 'That username/password combination is incorrect';
}    
else {
    $_SESSION['user_id'] = $userinfo['user_id'];
    $_SESSION['group_id'] = $userinfo['group_id'];

    $page = 'homepage_' . str_replace('_', '', $userinfo['group_id'] . '.php';
    header('Location:' . $page);
    exit ();
}
于 2012-09-14T20:34:25.817 回答