dtx63505 2013-07-24 06:27
浏览 41
已采纳

登录失败重定向到其他页面

I am working on my website, and I incorporated the login form in the homepage, not a separate page. It works great unless you have an error "enter wrong info" Then the displayed errors mess up the form margins and formatting. So I would like to have it redirect to a separate page (login.php) if the user entered the wrong details. My question is how to do that and also is it possible to keep the displayed erros? Thanks in advanced, Josh

Here is my code for handling errors:

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

}

  • 写回答

3条回答 默认 最新

  • doushen1026 2013-07-24 06:32
    关注

    You can redirect with the header function. http://php.net/manual/en/function.header.php

    header('Location: http://www.example.com/');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决