4

我制作了一个登录检查脚本,检查用户名和密码是否与数据库中设置的帐户匹配。一切正常,但它不区分大小写,它真的应该是!我该怎么做才能让这个脚本也检查大写/小写?例如:我有一个用户名:Admin 密码:My43sGG 的帐户。如果我在登录字段中输入 admin 和 my43sgg 也可以。

我的脚本:

<?php

// connect to the database
include("config.php");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM " .$members. " WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:index.php");
}
else {
?>
4

4 回答 4

6

利用BINARY

WHERE BINARY username = BINARY '$myusername' AND
      BINARY password = BINARY '$mypassword'

作为旁注,SQL Injection如果变量的值(s)来自外部,则查询很容易受到攻击。请看下面的文章,了解如何预防。通过使用PreparedStatements,您可以摆脱在值周围使用单引号。

于 2013-05-16T07:39:24.497 回答
3

与问题无关,但如果您使用 AES 将密码本身加密为共享机密并在数据库中查找 AES 加密字符串,那会更好......我想这也可以解决问题,尽管如果用不同的情况加密,它们会有所不同

于 2013-05-16T07:44:26.600 回答
0

您可以编写一个自定义函数来像这样验证它

function isPartUppercase($string) {
if(preg_match("/[A-Z]/", $string)===0) {
    return true;
}
return false;
}
于 2013-05-16T07:40:19.680 回答
-2

试试这个

<?php 
session_start(); 
$errorMessage = ''; 
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { 

    $userId   = $_POST['txtUserId']; 
    $password = ($_POST['txtPassword']); 
     require_once("db_fns.php");
    db_connect();
    $sql = "SELECT *  
            FROM adminuser
            WHERE username = '$userId' AND password = '$password'"; 

    $result = mysql_query($sql) or die('Query failed. ' . mysql_error());  

    $row=mysql_fetch_array($result);
    $admin=$row['username'];
   // print $admin;
    if (mysql_num_rows($result) == 1) { 

        $_SESSION['db_is_logged_in'] = true; 

        $_SESSION['admin']=$admin;

        header('Location: main.php'); 
        exit; 
    } else { 
        $errorMessage = 'Sorry, wrong user id / password'; 
    } 

} 
?>
于 2013-05-16T07:51:36.593 回答