php - How can i prevent SQL injection when using MySQLi? -


this question has answer here:

how prevent sql injections in sql query this?

<?php $mysqli = new mysqli("ip", "username", "pass", "database");  /* check connection */ if (mysqli_connect_errno()) {     printf("sorry, login server 'under maintainance'");     exit(); }  $username = $_post["username1"]; $username = strtolower($username); $password = $_post['password1']; $hash = sha1(strtolower($username) . $password);  $query = "select * accounts name='$username'";  if ($result = $mysqli->query($query)) {     /* determine number of rows result set */     $rownum = $result->num_rows;  if($rownum != 0) { while ($row = $result->fetch_assoc()) {   {   $acct = $row['acct'];   $pass = $row['pass']; } if($hash == $pass){ session_start(); $_session['name']=$username; $_session['acct']=$acct; header('location:index.php'); } else { echo 'there error when logging in. make sure password , username correct.'; } }     $result->close(); } else { echo 'account not exist. please <a href="register.php">register</a> account before logging in.'; }     $mysqli->close(); } ?> 

i have added encryption cannot seem find prevention method know how use yet. also, possible user use mysql injection without using input box? (page dissection???)

encryption , query sanitation not related.

you're using mysqli, nice, don't sanitize input query (namely $username, doesn't need strtolowered either).

you should use parameterized queries sanitation.

$query = "select * accounts name = ?"; $stmt = $mysqli->prepare($query); $stmt->bind_param("s", $username); $stmt->execute(); $stmt->bind_result($acct, $pass); $stmt->fetch(); //$act , $pass set 

the limits on sql injection have nothing user. it's possible accidentally inject in own code, , injection not have malicious. reason, should always parameterize queries if don't think there's risk of malicious injection.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -