"Fantastic support, quick and effective hosting"


Ben Steward
BSD Design
XDnet Live Help

Posts Tagged ‘injections’

2008 Sep 24

It seems like everyone is concerned about MySQL injections when it comes to programming, however their is an easy solution out there! Here is the functions I wrote and have been using for a good few months and I’ve never had any problems.

<?php
// The SQL Input Function This validates whats 
inputted and 'cleans' it.
function SQL_Input($input, $is_numeric=FALSE){
if(
is_numeric($input) == TRUE && $is_numeric == TRUE){ // If it's a number and it's ment to be a number.
return $input;
}else{
// Anything else
return serialize($input); // You can replace this for a base64,
 but I like serialize's.
}
}

// The SQL output function. This convets the stuff pulled from the 
SQL back to normal text.
function SQL_Output($input, $is_numeric=FALSE){
if(
is_numeric($input) == TRUE && $is_numeric == TRUE){ // If it’s a number and it’s ment to be a number.
return $input;
}else{
// Anything else
return unserialize($input);
}
}

### Example ###
/* In the next example I’m going to pull some data from a table called ’table’. 
Here is some info about the table:
ID - Should only be numbers.
Text - Is some plain text.

And the input is:
$_POST['ID'] - should be a number.
$_POST['Text'] - is some general text.
*/
$result = mysql_query(“SELECT * FROM ’table’ WHERE ID=’”.SQL_Input($_POST['ID'], TRUE).“‘ AND Text=’”.SQL_Input($_POST['Text'], FALSE).“‘”);
while (
$row = mysql_fetch_assoc($result)) {
echo
‘<strong>ID:</strong>’.SQL_Output($row['ID'], TRUE).“\n”;
echo
‘<strong>Text:</strong>’.SQL_Output($row['Text'], TRUE).“\n”;
}
?>




XDnet Web Hosting Blog is proudly powered by WordPress
Entries (RSS) and Comments (RSS).