0

Every question which I asked on stackoverflow I received a question that It was easy to do a php injection into my script.

I've now a example and checked some tutorials on youtube. Am I doing this right now? This is an example how I'm working now

if($user->isLoggedIn()) {
    $pakuser = $user->data()->username;
    $sql = $db->query("SELECT * FROM users
        INNER JOIN post ON users.username = post.add
        WHERE post.id = $id AND post.add = '$pakuser'")
        or die(mysql_error());         

    if ($sql === FALSE) {

    }
    if($row = $sql->fetch_object())
    if($row->add)
    {               
?>     
    <p><a href="editpost.php?id=<?php echo htmlspecialchars($gooo->id);?>">edit this post</a><br><br>BEWARE OF DELETING YOUR CONTENT THERE IS NO GO-BACK<BR><a href="delete.php?id=<?php echo htmlspecialchars($gooo->id); ?>">Delete this post</a> </p>
<?php
    }
}
lighter
  • 2,808
  • 3
  • 40
  • 59

2 Answers2

0

Everytime the user can manipulate your sql-query without any restriction, there is a security-issue. Here is an example:

$query_string = "SELECT * FROM user WHERE (name='$username' AND password='$password')";

if the user sends a password like:

"something') OR ('1' = '1"

the query will change to:

$query_string = "SELECT * FROM user WHERE (name='Name' AND password='something') OR ('1' = '1')";

Because '1'='1' is always true, this will return each user in your database.

Instead you can change the example above to:

$query = mysqli->prepare('SELECT * FROM user WHERE (name=? AND password=?)');
$query->bind_param('ss', $username, $password);
$query->execute();

This will filter all strings that could break your sql-query.

Nino
  • 402
  • 3
  • 8
-1

It seems like you are still just passing variables straight through into the query. Yes, this may work, but is not necessary secure.

You could have a look at using PDO instead, which has means of being able to verify the data type that you are wanting to pass through into your query rather than just passing a variable into the query string.

In terms of using mysqli, have a look at mysqli_real_escape_string if you have not already. It is well documented.

Harry Lawrence
  • 783
  • 1
  • 7
  • 17