2013-07-24 58 views
0

我在我的网站上工作,并将登录表单并入主页,而不是单独的页面。它工作得很好,除非你有错误“输入错误的信息”然后显示的错误混乱了窗体边距和格式。因此,如果用户输入了错误的详细信息,我希望将其重定向到单独的页面(login.php)。我的问题是如何做到这一点,也有可能保持显示的错误?在先进的感谢,乔希登录失败重定向到不同的页面

这是我的错误处理代码:

/** 
    * login - The user has submitted his username and password 
    * through the login form, this function checks the authenticity 
    * of that information in the database and creates the session. 
    * Effectively logging in the user if all goes well. 
    */ 
    function login($subuser, $subpass, $subremember){ 
     global $database, $form; //The database and form object 
    /* Username error checking */ 
    $field = "user"; //Use field name for username 
    if(!$subuser || strlen($subuser = trim($subuser)) == 0){ 
    $form->setError($field, "* Username not entered"); 
    } 
    else{ 
    /* Check if username is not alphanumeric */ 
    if(!preg_match("/^([0-9a-z])*$/i", $subuser)){ 
     $form->setError($field, "* Username not alphanumeric"); 
    } 
    } 

    /* Password error checking */ 
    $field = "pass"; //Use field name for password 
    if(!$subpass){ 
    $form->setError($field, "* Password not entered"); 
    } 

    /* Return if form errors exist */ 
    if($form->num_errors > 0){ 
    return false; 
    } 



    /* Checks that username is in database and password is correct */ 
    $subuser = stripslashes($subuser); 
    $result = $database->confirmUserPass($subuser, md5($subpass)); 

    /* Check error codes */ 
    if($result == 1){ 
    $field = "user"; 
    $form->setError($field, "* Username not found"); 
    } 
    else if($result == 2){ 
    $field = "pass"; 
    $form->setError($field, "* Invalid password"); 
    } 

    /* Return if form errors exist */ 
    if($form->num_errors > 0){ 
    return false; 
    } 

    /* Username and password correct, register session variables */ 
    $this->userinfo = $database->getUserInfo($subuser); 
    $this->username = $_SESSION['username'] = $this->userinfo['username']; 
    $this->userid = $_SESSION['userid'] = $this->generateRandID(); 
    $this->userlevel = $this->userinfo['userlevel']; 

    /* Insert userid into database and update active users table */ 
    $database->updateUserField($this->username, "userid", $this->userid); 
    $database->addActiveUser($this->username, $this->time); 
    $database->removeActiveGuest($_SERVER['REMOTE_ADDR']); 

    /** 
    * This is the cool part: the user has requested that we remember that 
    * he's logged in, so we set two cookies. One to hold his username, 
    * and one to hold his random value userid. It expires by the time 
    * specified in constants.php. Now, next time he comes to our site, we will 
    * log him in automatically, but only if he didn't log out before he left. 
    */ 
    if($subremember){ 
    setcookie("cookname", $this->username, time()+COOKIE_EXPIRE, COOKIE_PATH); 
    setcookie("cookid", $this->userid, time()+COOKIE_EXPIRE, COOKIE_PATH); 
    } 

    /* Login completed successfully */ 
    return true; 

}

回答

0
Make an array for error like: 
$error = array(); 

if($result == 1){ 
    $field = "user"; 
    $form->setError($field, "* Username not found"); 
    $error[] = "Username not found"; 
    } 
    else if($result == 2){ 
    $field = "pass"; 
    $form->setError($field, "* Invalid password"); 
    $error[] = "Invalid password"; 
    } 

    /* Return if form errors exist */ 
    if($form->num_errors > 0){ 
    return false; 
    } 


And in the last count error array like: 

if(count($error) > 0){ 
echo implode("<br />",$error); 
} 


if there is no error then like: 

if(count($error)==0){ 
header("location:redirectpage.php"); 
} 
0

您可以使用重定向功能,您的网站几次,我建议你创建这个类或函数:

这个,如果你有一个菜单变量时才起作用在网址:(工作示例)

class myHeader { 

    function redirect($menu) { 
    $this->host= $_SERVER['HTTP_HOST']; 
    $this->uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); 
    $extract="http://$this->host$this->uri/?menu=$menu"; 
    $this->head($extract); 
    } 

    function head($extract) { header("Location: $extract"); exit; } 

    } 
    $header = new myHeader(); 

显示错误给用户,并且你想给它一个特殊的外观可以用会话工作:

if($result == 1) { 
$_SESSION['fail_user'] = 1; 
} 

if($result == 2) { 
$_SESSION['fail_password'] = 1; 
} 

那么您的login.php:

<?php 
if($_SESSION['fail_user'] == 1) { 
?> 
<p style="color:red; font-weight:bold;">Please enter a valid Username</p> 
<?php 
} 
?> 
相关问题