Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 登录失败,无法重定向到其他页面_Php_Forms_Login - Fatal编程技术网

Php 登录失败,无法重定向到其他页面

Php 登录失败,无法重定向到其他页面,php,forms,login,Php,Forms,Login,我正在我的网站上工作,我将登录表单合并到主页中,而不是单独的页面。它工作得很好,除非你有一个错误“输入错误的信息”,然后显示的错误搞乱了表格的边距和格式。因此,如果用户输入了错误的详细信息,我希望将其重定向到单独的页面(login.php)。我的问题是如何做到这一点,以及是否有可能保持显示的错误?谢谢你,乔希 以下是我处理错误的代码: /** * login - The user has submitted his username and password * through

我正在我的网站上工作,我将登录表单合并到主页中,而不是单独的页面。它工作得很好,除非你有一个错误“输入错误的信息”,然后显示的错误搞乱了表格的边距和格式。因此,如果用户输入了错误的详细信息,我希望将其重定向到单独的页面(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;

}

您可以使用标题功能重定向

为错误创建一个数组,如:
$error=array();
如果($result==1){
$field=“用户”;
$form->setError($field,“*未找到用户名”);
$error[]=“找不到用户名”;
}
else if($result==2){
$field=“pass”;
$form->setError($field,“*无效密码”);
$error[]=“无效密码”;
}
/*如果存在表单错误,则返回*/
如果($form->num\u errors>0){
返回false;
}
在最后一个计数错误数组中,如下所示:
如果(计数($error)>0){
回波内爆(“
”,$error); } 如果没有错误,则如下所示: 如果(计数($error)==0){ 标题(“位置:redirectpage.php”); }
您可以在网站上多次使用重定向功能,我建议您为此创建一个类或函数:

如果使用url中的菜单变量,则此操作有效:(工作示例)

要向用户显示错误,并希望对其进行特殊查看,可以使用会话:

 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 
}
?>

请输入有效的用户名


如果这样做,您需要在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();
 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 
}
?>