2013-07-24 9 views
0

나는 내 웹 사이트에서 일하고 있으며 별도의 페이지가 아니라 홈페이지에 로그인 양식을 포함 시켰습니다. "잘못된 정보 입력"오류가 표시되지 않으면 효과가 있습니다. 그런 다음 표시된 오류로 인해 양식 여백과 서식이 엉망입니다. 그래서 사용자가 잘못된 세부 정보를 입력하면 별도의 페이지 (login.php)로 리디렉션하도록하고 싶습니다. 내 질문은 그것을하는 방법이며 또한 그것은 표시 erros를 유지하는 것이 가능한가?로그인 실패 다른 페이지로 리디렉션

/** 
    * 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; 

}

답변

1

당신은 헤더 기능을 리디렉션 할 수 있습니다 : 고급 감사, 조쉬

여기에 처리 오류에 대한 내 코드입니다. 당신이 메뉴 변수로 작업하는 경우

이 작품 : http://php.net/manual/en/function.header.php

header('Location: http://www.example.com/'); 
+0

당신은 당신이 그런 식으로 할 경우 URL에 오류를 통과해야합니다. – shapeshifter

+0

감사합니다. 훌륭한 작품들입니다. – s28400

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

당신은 몇 번 더 귀하의 웹 사이트에 리디렉션 기능을 사용할 수 있습니다, 당신이 이것에 대한 클래스 또는 함수를 작성 제안 URL에 : (작업 예)

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(); 

는 사용자에게 오류를 표시하고 당신이 당신이 세션과 함께 작업 할 수있는 특별한 모습을 제공 할 :

다음 login.php에
if($result == 1) { 
$_SESSION['fail_user'] = 1; 
} 

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

:

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