Jump to content

injections


ohdang888

Recommended Posts

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
https://forums.phpfreaks.com/topic/83903-injections/#findComment-427048
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
https://forums.phpfreaks.com/topic/83903-injections/#findComment-427050
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
https://forums.phpfreaks.com/topic/83903-injections/#findComment-427053
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
https://forums.phpfreaks.com/topic/83903-injections/#findComment-427541
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.