Case-sensitive query in sql server with Laravel 5.4

Asked

Viewed 301 times

2

I am making a query to the database (sql server) for user and password validation with Laravel 5.4 but, the query is running as case-insensitive.

In the database is:

pwd = Aa12a

Input:

password = aa12a

The result is:

pwd == password

Consultation:

DB::table('users')
    ->where('acc', $request->input('account'))
    ->where('pwd', $request->input('password'))
    ->first();

How to change that?

  • What is the coding/collation of the bank?

  • It’s Sql_latin1_general_cp1_ci_as. I tried to change it directly in the database, but it’s not accepting the collation exchange.

  • You know that Ci means case insensitive?

  • Yes. And this is exactly why I asked, to know if there is any way to determine this in the query of the proper Standard, since I could not change in the database, because there is a restriction.

1 answer

1


If it is complicated to compare passwords by SQL server, you can do it by PHP.

$result = DB::table('users')
->where('acc', $request->input('account'))
->first();

if ($result && $result->pwd === $request->input('password')) {
      // Senhas conferem
}

Still, I think it is possible to define in the database configuration. For example, I will define two identical settings, but with the collation different.

[
    'sqlserver' => [
        'driver'    => 'sqlsrv',
        'host'      => 'host',
        'database'  => '',
        'username'  => 'username',
        'password'  => 'password',
        'prefix'    => '',
        'collation' =>  'SQL_Latin1_General_CP1_CI_AS',
        'charset'   =>  'latin1'
    ],

    'sqlserver-case-sensitive' => [
        'driver'    => 'sqlsrv',
        'host'      => 'host',
        'database'  => '',
        'username'  => 'username',
        'password'  => 'password',
        'prefix'    => '',
        'collation' =>  'SQL_Latin1_General_CP1_CS_AS',
        'charset'   =>  'latin1'
    ]
] 

To use the collation of the second connection configuration, just do so:

DB::connection('sqlserver-case-sensitive')->table('users')
    ->where('acc', $request->input('account'))
    ->where('pwd', $request->input('password'))
   ->first();

Recommended readings:

  • I thought about that possibility, but I preferred to ask to see if there is any other means to "save" code. haha. But thank you! =)

  • @Getulio I am editing, there is yes... I don’t know about Sqlserver, but you can use the COLLATE option in the query

  • I also do not (kk), but I will search on. Thank you!

  • @Getulio now I think has improved, I think the last option is what you need!

  • I tried, but it didn’t work. I’ll have to do it by php even! Better than getting my head broken. xD Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.