Jump to content

injections


ohdang888

Recommended Posts

Anything that is going into the database use

 

mysql_real_escape_string

 

Other than that, create checks when submitting data.

 

i.e. if it is a username check for a-zA-Z0-9 only, make sure things like " ; ' etc are not allowed.

Link to comment
Share on other sites

You can try things like:

 

<?php
function escape_text($connection, $text) {
// Stripslashes
if (get_magic_quotes_gpc()) {
  $text = stripslashes($text);
}

// Escape if not a number
if (!is_numeric($text)) {
  $text = mysqli_real_escape_string($connection, $text);
}

return $text;
}

function escape_forbidden($text) {
// Forbidden characters
$forbidden = "/!\@#$%^&*():{}?£¬`\/.,;[]-_+=~<>";
$text = stripslashes($text);

// Escape if $text contains forbidden
if (strlen($text) != strcspn($text, $forbidden)) {
	return "invalid";
}
else {
	return "valid";
}
}
?>

Link to comment
Share on other sites

Typecast everything. If your input expects a string, typecast it to a string, and so on. If you have forms where input is boolean, or dropdown boxes / radio buttons specify choices, make sure that the choices are the only choices accepted by the processing script.

Link to comment
Share on other sites

Typecast everything. If your input expects a string, typecast it to a string, and so on. If you have forms where input is boolean, or dropdown boxes / radio buttons specify choices, make sure that the choices are the only choices accepted by the processing script.

 

This is also always a good idea.

Link to comment
Share on other sites

does nay1 have any really really good links or advice about preventing injections?

 

Prepared statements.

 

<?php
try {
$db = new PDO('mysql:dbname=database_name;host=localhost', 'username', 'password');
}
catch(PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}

$statement = $db->prepare('SELECT * FROM users WHERE username = ? LIMIT 1');
$statement->execute(array($_GET['username']));

$user_info = $statement->fetch(PDO::FETCH_ASSOC);
print_r($user_info);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.