我正在为我的网站构建一个登录页面。我已经使用Dreamweaver创建了登录代码。我对php不太了解,,但当我使用错误的密码(但正确的用户名)登录时,它会允许我进入网站的受限部分
例如:假设正确的用户名是test,而正确的用户名则是test123。当用户键入用户名的"test"并键入任何字母(尽管如果你键入一个数字,它不允许你通过)作为密码时,它会将他们登录并允许他们通过。这会很糟糕。。。
有没有办法确保网站检查密码和用户名是否正确
我对php几乎一无所知。因此,任何帮助都将不胜感激。
这是我的登录代码:
<?php
require_once('Connections/--------_userdatabase.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;
}
// *** 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 = "http://www.-----------.org";
$MM_redirectLoginFailed = "http://www.-----------.org/login.php";
$MM_redirecttoReferrer = false;
mysql_select_db($database_-------_userdatabase, $--------_userdatabase);
$LoginRS__query=sprintf("SELECT Username, Password FROM userdatabase WHERE Username=%s AND Password=%s",
GetSQLValueString($loginUsername, "int"), GetSQLValueString($password, "int"));
$LoginRS = mysql_query($LoginRS__query, $--------_userdatabase) 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']) && false) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
if ($loginUsername === 'Admin') {
header("Location: http://www.------------.org/user/-------/");
} else if ($loginUsername === 'User') {
header("Location: http://www.------------.org/user/-------/");
} else if ($loginUsername === 'User2') {
header("Location: http://www.-----------.org/user/--------/");
} else {
header("Location: " . $MM_redirectLoginSuccess );
}
} else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?>
您必须在header()
重定向后添加die();
,因为header()
仅向浏览器发送标头,而不知道它的含义。然后,如果之后没有die()
,它将继续像往常一样提供内容
部分示例
<?php
if ($loginFoundUser) {
// Put the cut out code back in
if (isset($_SESSION['PrevUrl']) && false) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
if ($loginUsername === 'Admin') {
header("Location: http://www.------------.org/user/-------/");
die;
} else if ($loginUsername === 'User') {
header("Location: http://www.------------.org/user/-------/");
die;
} else if ($loginUsername === 'User2') {
header("Location: http://www.-----------.org/user/--------/");
die;
} else {
header("Location: " . $MM_redirectLoginSuccess );
die;
}
} else {
header("Location: ". $MM_redirectLoginFailed );
die;
}
?>
此外,在检查数据库中的值之前,您似乎正在将值转换为INT,这是不应该做的(考虑到有效的$loginUsername
是"Admin")。这可以通过以下代码进行更改:
GetSQLValueString($loginUsername, "string"), GetSQLValueString($password, "string"));
下面的链接是禁区吗?
http://www.------------.org/user/-------/