如果用户名和密码正确,则在同一页面上显示消息

时间:2015-11-05 11:38:03

标签: php html

我正在开发一个在登录前需要用户名和密码的应用程序。我使用Dreamweaver进行开发。我想要实现的是当用户输入正确的用户名和密码时,他应该收到一条消息,说明"用户名和密码正确" ,如果错误的凭据"用户名或密码错误"。 但是,使用Dreamweaver登录服务器行为时,它无法执行此操作,它的作用是在凭据正确或错误时重定向到页面。有人能帮助我吗?

HTML

  <form id="form2" name="form2" method="post" action="">
      <table width="100%" border="0" cellpadding="4" cellspacing="4">
        <tr>
          <td><label for="username"></label>
            <input name="username" type="text" class="register-input" id="username" data-role="none" placeholder="Enter your username" /></td>
        </tr>
        <tr>
          <td><label for="password"></label>
            <input name="password" type="text" class="register-input" id="password" data-role="none" placeholder="Enter your password" /></td>
        </tr>
        <tr>
          <td><input type="submit" name="button" id="button" value="Sign In" /></td>
    </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
      </table>
      <table width="100%" border="0" cellpadding="4" cellspacing="4">
        <tr>
          <td><div align="center"> </div></td>
        </tr>
      </table>
    </form>

PHP

<?php require_once('Connections/db.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "test.php";
  $MM_redirectLoginFailed = "error.php";
  $MM_redirecttoReferrer = true;
  mysql_select_db($database_catchapp, $catchapp);

  $LoginRS__query=sprintf("SELECT username, password FROM users WHERE username=%s AND password=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 

  $LoginRS = mysql_query($LoginRS__query, $catchapp) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";

    if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;       

    if (isset($_SESSION['PrevUrl']) && true) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];  
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>

0 个答案:

没有答案