Jump to content

Recommended Posts

Ok, so I want to be able to use the (') character in my forms (for more flex in user names etc...).  Would this be adequate to keep out the injection attack?

 

<?php

$str = "sdfljkdddddddddddddg ' gjkl '\\\\\\\\\\' dgffg\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\'\'dfgdgh";
echo "$str\n";

$str = preg_replace('/\\\\*/','',$str);
$str = preg_replace('/\'(\')+/','\'',$str);
$str = htmlspecialchars($str, ENT_QUOTES);
echo "$str";

?>

 

The echo output looks like (source):

sdfljkdddddddddddddg ' gjkl '\\\\\' dgffg\\\\\\\\\\\\\\\\\\\\'\'\'dfgdgh
sdfljkdddddddddddddg &#039; gjkl &#039; dgffg&#039;dfgdgh

 

Will this adequately reject an injection attack?

Link to comment
https://forums.phpfreaks.com/topic/192112-sql-injection-sanitization/
Share on other sites

To stop SQL injection I would advise you to use a function such as mysql_real_escape_string().

 

$username = mysql_real_escape_string($_POST['username']);
$result = mysql_query("INSERT INTO user(username) VALUES('$username')") or trigger_error(mysql_error());

Wow, the function "mysql_real_escape_string()" does not function for me on my system.  Any usage of it results in a blank output to variable.  I used it as described at http://php.net/manual/en/function.mysql-real-escape-string.php but no matter what I put through it, no output was generated.

 

$str = mysql_real_escape_string('sdfljkdddddddddddddg \' gjkl \'\\\"\"\"\"\\\\\' dgffg\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\'\'dfgdgh');
if(isset($str)){
    echo "str = ".$str."\n<br>\n";
}

output: "str = "

 

$str = "sdfljkdddddddddddddg ' gjkl '\\\"\"\"\"\\\\' dgffg\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\'\'dfgdgh";
$str = mysql_real_escape_string("$str");
if(isset($str)){
    echo "str = ".$str."\n<br>\n";
}

output: "str = "

 

$str = mysql_real_escape_string($_POST['str']);
if(isset($str)){
    echo "str = ".$str."\n<br>\n";
}

output: "str = "

 

:wtf: What's my malfunction? LOL

 

(PHP version: 5.3.1)

  • 2 weeks later...

I used mysqli_stmt_ functions to get it done...

 

if ($func=="login"){
if(!$username || !$passwd) { die('ERROR: missing info'); }
dbr_(); /* connect to the DB */
$stmt = mysqli_stmt_init($link);						/* create a prepared statement, $link is from dbr_(); */
if (mysqli_stmt_prepare($stmt, 'SELECT username, upass FROM users WHERE username=?')) {
	mysqli_stmt_bind_param($stmt, "s", $username);				/* bind parameters for markers */
	mysqli_stmt_execute($stmt);						/* execute query */
	mysqli_stmt_bind_result($stmt, $uname, $upass);				/* bind result variables */
	if (mysqli_stmt_fetch($stmt)) {						/* fetch value */
		if($upass == "$passwd" ){
			sess('login',TRUE);

 

Etc... (I don't yet understand classes, so I used procedural code.)

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.