php 如何检查用户名和密码区分大小写?

mwkjh3gx  于 2023-06-28  发布在  PHP
关注(0)|答案(4)|浏览(108)

我做了一个登录检查脚本,检查用户名和密码是否与数据库中设置的帐户匹配。所有工作正常,但它不区分大小写,它真的应该是!我该怎么做才能让这个脚本也检查大写/小写?例如:我有一个帐户用户名:AdMin密码:My 43 sGG。如果我在登录字段中输入admin和my 43 sgg,它也会起作用。
我的剧本:

<?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 {
?>
mhd8tkvw

mhd8tkvw1#

使用BINARY

WHERE BINARY username = BINARY '$myusername' AND
      BINARY password = BINARY '$mypassword'
  • SQLFiddle演示

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

xesrikrc

xesrikrc2#

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

lx0bsm1f

lx0bsm1f3#

您可以编写一个自定义函数来验证它,如下所示

function isPartUppercase($string) {
if(preg_match("/[A-Z]/", $string)===0) {
    return true;
}
return false;
}
b0zn9rqh

b0zn9rqh4#

试试这个

<?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'; 
    } 

} 
?>

相关问题