我正在我的网站上工作,我将登录表单合并到主页,而不是单独的页面。除非您输入错误信息“输入错误信息”,否则它的效果很好然后显示的错误会影响表单边距和格式。因此,如果用户输入了错误的详细信息,我希望将其重定向到单独的页面(login.php)。我的问题是如何做到这一点,是否可以保持显示的错误?谢谢先进的Josh
以下是处理错误的代码:
/**
* 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 :(得分:1)
您可以使用标题功能重定向。 http://php.net/manual/en/function.header.php
header('Location: http://www.example.com/');
答案 1 :(得分: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");
}
答案 2 :(得分: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
}
?>