Redirect 如何在使用特定用户名登录后在特定页面上重定向

Redirect 如何在使用特定用户名登录后在特定页面上重定向,redirect,login,Redirect,Login,我创建了一个登录表单,我想如果(例如)帐户包含“doru”名称,doru将被重定向到“/admin”链接。我该怎么做?我试着这样做: $_SESSION['login'] = 'Yes'; if("username='".htmlentities($_POST['user'])."'"=='doru') echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL=/admin">'; if("usern

我创建了一个登录表单,我想如果(例如)帐户包含“doru”名称,doru将被重定向到“/admin”链接。我该怎么做?我试着这样做:

$_SESSION['login'] = 'Yes';
        if("username='".htmlentities($_POST['user'])."'"=='doru')
            echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL=/admin">';
    if("username='".htmlentities($_POST['user'])."'"<>'doru')
        echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL=profile.php">';
$\u会话['login']='Yes';
如果(“username=”.htmlentities($\u POST['user'])。“”“=='doru')
回声';
如果(“username=”.htmlentities($\u POST['user'])。“”“'doru')
回声';
完整的代码如下:(用罗马尼亚语,我为您修改了上面的几个单词)


是的,这是可能的。我认为问题在于你的比较代码

$_SESSION['logat'] = 'Da';
if($_POST['user']=='doru')
    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL=/admin">';
if($_POST['user']<>'doru')  // Note: this 'if' is not necessary since it will always be true
    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL=profil.php">';
您的代码中有一个
while($rand=mysql\u fetch\u array($rezultat))
,但从不使用
$rand
结果。您可以将其全部删除,也可以将其用于目标位置

但是,首先,您已经知道只有一行(您已经测试过了),所以您可以去掉
while()
,只需要
$rand=mysql\u fetch\u数组($rezultat)
(我通常使用mysql\u fetch\u assoc()按名称获取字段)。然后重定向到数据库中存储的位置

$rand = mysql_fetch_assoc($rezultat);
$_SESSION['logat'] = 'Da';
if( !empty( $rand['startDir'] ) )
    header( 'Location: http://' . $_SERVER['HTTP_HOST'] . $rand['startDir'] );
header( 'Location: http://' . $_SERVER['HTTP_HOST'] . "/profil.php" );
您可以创建一个函数(一个良好的实践)并在使用时使用会话

// Function
function redirect($url, $statusCode = 0)
{
   header('Location: ' . $url, true, $statusCode);
   die();
}
实际上,您也可以在会话中保存用户名和登录名(作为布尔值)(找到一种安全的方法来保存会话)

然后用作

if($_SESSION['login'] == TRUE){
  if($_SESSION['username'] == "aaaa"){
   redirect('http://www.xxxx.com/', false);
  }else{
   redirect('http://www.yyyy.com/', false);
  }
}
做这样的事

if(!headers_sent()) {
//If headers not sent yet... then do php redirect
    header('Location: '.$redirect_url);
    exit;
} else {
//If headers are sent... do javascript redirect... if javascript disabled, do html redirect.
    echo '<script type="text/javascript">';
    echo 'window.location.href="'.$redirect_url.'";';
    echo '</script>';
    echo '<noscript>';
    echo '<meta http-equiv="refresh" content="0;url='.$redirect_url.'" />';
    echo '</noscript>';
    exit;
}
if(!headers\u sent()){
//如果标题还没有发送…那么做php重定向
标题('Location:'.$redirect\u url);
出口
}否则{
//如果发送了标题…执行javascript重定向…如果禁用javascript,执行html重定向。
回声';
echo'window.location.href=“”.$redirect_url.”“;”;
回声';
回声';
回声';
回声';
出口
}
if($_SESSION['login'] == TRUE){
  if($_SESSION['username'] == "aaaa"){
   redirect('http://www.xxxx.com/', false);
  }else{
   redirect('http://www.yyyy.com/', false);
  }
}
if(!headers_sent()) {
//If headers not sent yet... then do php redirect
    header('Location: '.$redirect_url);
    exit;
} else {
//If headers are sent... do javascript redirect... if javascript disabled, do html redirect.
    echo '<script type="text/javascript">';
    echo 'window.location.href="'.$redirect_url.'";';
    echo '</script>';
    echo '<noscript>';
    echo '<meta http-equiv="refresh" content="0;url='.$redirect_url.'" />';
    echo '</noscript>';
    exit;
}