简体   繁体   中英

PHP SQL login case sensitive

I am learning out to do website log-in and create creation. I am facing the probably where my script does not check if the two strings are the same case. Example, Foo and foO return both true.

My current PHP script is:

//Create query
$qry="SELECT * FROM member WHERE username='$username' AND password='$password'";
$result=mysql_query($qry);

//Check whether the query was successful or not
if($result) {
    if(mysql_num_rows($result) > 0) {
        //Login Successful
        session_regenerate_id();
        $member = mysql_fetch_assoc($result);
        $_SESSION['SESS_MEMBER_ID'] = $member['mem_id'];
        $_SESSION['SESS_FIRST_NAME'] = $member['username'];
        $_SESSION['SESS_LAST_NAME'] = $member['password'];
        session_write_close();

you can use "COLLATE latin1_general_cs" character set to differentiate to upper case or lower case.

SELECT * FROM `table` WHERE `your condiction` COLLATE latin1_general_cs;

or you can use

SELECT *  FROM `table` WHERE BINARY `column` = 'value'

If you are in the WINDOWS: you can find the my.ini and change the lower_case_table_names=0. If you are in the LINUX: you can find the my.cnf and change the lower_case_table_names=0.

The default collation for character set latin1, which is latin1_swedish_ci, happens to be case-insensitive.

You can choose a case-sensitive collation, for example latin1_general_cs while creating a database schema . Have look in this link

https://dba.stackexchange.com/questions/15250/how-to-do-a-case-sensitive-search-in-where-clause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM