[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 11-23-2007, 11:28 AM   #1 (permalink)
NamePros Member
 
Join Date: Jan 2007
Posts: 68
0.00 NP$ (Donate)

baris22 is an unknown quantity at this point


Injection (php-mysql)

I have got a problem with a character. I do not know which one it is. I am inserting long text files into database and sometime i can not insert. Any idea which character can it be?

I am using this code

PHP Code:
$links.=$_POST['links'][$i];
                
$description=$_POST['description'][$i];
                
$type=$_POST['type'];
                if (!
get_magic_quotes_gpc()) {
                    
$title=addslashes($title);
                    
$links=addslashes($links);
                    
$description=addslashes($description);
                }
                
$to_replace = array("\r\n","|","\n","\\r\\n","\\n");
              
$title = str_replace($to_replace, " ", $title);
              
$description = str_replace($to_replace, " ", $description);
                
//echo $title . '<br/>';
                
$query="INSERT INTO `filedetails` VALUES ('', '".$type."', '".$title."', '".$description."', '".$links."','0000000000')";
                
mysql_query($query);
baris22 is offline  
Old 11-23-2007, 11:50 AM   #2 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
The easy way to fix this problem is to use mysql_real_escape_string() on your input this will ensure that any relevant characters are escaped.

Oh and by the way stop using addslashes it is far from ideal, if you use the above mentioned function then the addslashes function is not needed (and can cause problems) anyway.

I would also check and see if you have magic_quotes_gpc enabled (most hosts do). If that is the case reverse what this feature does or disable it using a .htaccess file if you are able too. In fact while you are at it disable register_globals.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 11-23-2007, 11:58 AM   #3 (permalink)
NamePros Member
 
Join Date: Jan 2007
Posts: 68
0.00 NP$ (Donate)

baris22 is an unknown quantity at this point


PHP Code:
 $links.=$_POST['links'][$i];
                
$description=$_POST['description'][$i];
                
$type=$_POST['type'];
                if (!
get_magic_quotes_gpc()) {
                    
$title=mysql_real_escape_string($title);
                    
$links=mysql_real_escape_string($links);
                    
$description=mysql_real_escape_string($description);
                }
                
$to_replace = array("\r\n","|","\n","\\r\\n","\\n");
              
$title = str_replace($to_replace, " ", $title);
              
$description = str_replace($to_replace, " ", $description);
                
//echo $title . '<br/>';
                
$query="INSERT INTO `filedetails` VALUES ('', '".$type."', '".$title."', '".$description."', '".$links."','0000000000')";
                
mysql_query($query);
is it how to use mysql_real_escape_string?

thanks
baris22 is offline  
Old 11-23-2007, 12:08 PM   #4 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
yes thats fine, just a note I do not know what the content of $type should be but you should run the function on that as well.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 11-23-2007, 12:32 PM   #5 (permalink)
NamePros Member
 
Join Date: Jan 2007
Posts: 68
0.00 NP$ (Donate)

baris22 is an unknown quantity at this point


it did not work. Same problem.

then i used this code but same problem again. I do not know if this code is ok

PHP Code:
$links.=$_POST['links'][$i];
                
$description=$_POST['description'][$i];
                
$type=$_POST['type'];
                
                
$title=stripslashes($title);
                
$links=stripslashes($links);
                
$description=stripslashes($description);
                
                
$title=mysql_real_escape_string($title);
                
$links=mysql_real_escape_string($links);
                
$description=mysql_real_escape_string($description);
                
                
$to_replace = array("\r\n","|","\n","\\r\\n","\\n");
              
$title = str_replace($to_replace, " ", $title);
              
$description = str_replace($to_replace, " ", $description);
                
//echo $title . '<br/>';
                
$query="INSERT INTO `filedetails` VALUES ('', '".$type."', '".$title."', '".$description."', '".$links."','0000000000')";
                
mysql_query($query);
                echo
mysql_error();
baris22 is offline  
Old 11-23-2007, 12:54 PM   #6 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
echo out the query and post it here, also a copy of the error message you receive.

Ahh and just noticed. Do the mysql_real_escape_string lines after the following not before, i never noticed it before:-

PHP Code:
                $to_replace = array("\r\n","|","\n","\\r\\n","\\n");
              
$title = str_replace($to_replace, " ", $title);
              
$description = str_replace($to_replace, " ", $description);
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 11-23-2007, 01:30 PM   #7 (permalink)
NamePros Member
 
Join Date: Jan 2007
Posts: 68
0.00 NP$ (Donate)

baris22 is an unknown quantity at this point


This is the code i used

PHP Code:
$links.=$_POST['links'][$i];
                
$description=$_POST['description'][$i];
                
$type=$_POST['type'];
                if (!
get_magic_quotes_gpc()) {
                    
$title=addslashes($title);
                    
$links=addslashes($links);
                    
$description=addslashes($description);
                }
              
$to_replace = array("\r\n","|","\n","\\r\\n","\\n");
              
$title = str_replace($to_replace, " ", $title);
              
$description = str_replace($to_replace, " ", $description);
              
              
$title=stripslashes($title);
              
$description=stripslashes($description);
                
              
$title=mysql_real_escape_string($title);
              
$description=mysql_real_escape_string($description);
                
//echo $title . '<br/>';
                
$query="INSERT INTO `filedetails` VALUES ('', '".$type."', '".$title."', '".$description."', '".$links."','0000000000')";
                
mysql_query($query) OR die(mysql_error());
                echo
mysql_error();
I do not get an error. I get this

Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
baris22 is offline  
Old 11-23-2007, 01:33 PM   #8 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
That seems completely unrelated.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 11-23-2007, 01:39 PM   #9 (permalink)
NamePros Member
 
Join Date: Jan 2007
Posts: 68
0.00 NP$ (Donate)

baris22 is an unknown quantity at this point


i think the problem is on one character. I have to add 50 entries at once. It works 90%.

I added over 100 pages. 100x50. it worked about 90 pages. I am just going to try htmlspecialchars
baris22 is offline  
Old 11-23-2007, 01:53 PM   #10 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
invalid characters should not cause a Service Temporarily Unavailable if it does then there is something seriously wrong.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 11-23-2007, 01:55 PM   #11 (permalink)
NamePros Member
 
Join Date: Jan 2007
Posts: 68
0.00 NP$ (Donate)

baris22 is an unknown quantity at this point


I know. I need to check every single entry to see what is wrong.

Is there anything i can add to

$to_replace = array("\r\n","|","\n","\\r\\n","\\n");
baris22 is offline  
Old 11-23-2007, 02:02 PM   #12 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Why are you replacing them with a space? They will not be causing you any problem and in fact you are losing formatting by doing this.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 11-23-2007, 02:32 PM   #13 (permalink)
NamePros Member
 
Join Date: Jan 2007
Posts: 68
0.00 NP$ (Donate)

baris22 is an unknown quantity at this point


No, i have to do that. My code graps content from other web sites.

Do you think this can happen because of the charset. If there is a character which is not ok. with mysql settings?
baris22 is offline  
Old 11-23-2007, 03:25 PM   #14 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
it shouldn't do, mysql_real_escape_string takes into account the charset that is in use. Anyway if the charset was the problem you should see a mySQL error being generated.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft

Last edited by peter@flexiwebhost; 11-23-2007 at 03:31 PM.
Peter is offline  
Old 11-24-2007, 04:00 PM   #15 (permalink)
NamePros Member
 
Join Date: Jan 2007
Posts: 68
0.00 NP$ (Donate)

baris22 is an unknown quantity at this point


Oh my god. I found the answer after 2 long days.

When i try to add the word "wget " to database it does not work.

I am so serious. When i try to add "wget " (there is a space after) it does not work.
baris22 is offline  
Old 11-24-2007, 04:20 PM   #16 (permalink)
New Member
 
Join Date: Nov 2007
Posts: 24
68.15 NP$ (Donate)

the_internet is an unknown quantity at this point


If you'd rather not have the space at the end, you could try using rtrim() (and other related functions) to remove spaces from the end of a string.
the_internet is offline  
Old 11-24-2007, 04:24 PM   #17 (permalink)
NamePros Member
 
Join Date: Jan 2007
Posts: 68
0.00 NP$ (Donate)

baris22 is an unknown quantity at this point


I think i am going to use

PHP Code:
$to_replace = array("\r\n","|","wget ","\n","\\r\\n","\\n");
              
$title = str_replace($to_replace, " ", $title);
              
$description = str_replace($to_replace, " ", $description);

I do not think it will be a problem.
baris22 is offline  
Old 11-24-2007, 04:33 PM   #18 (permalink)
New Member
 
Join Date: Nov 2007
Posts: 24
68.15 NP$ (Donate)

the_internet is an unknown quantity at this point


OK, good luck with the code.
the_internet is offline  
Old 11-24-2007, 04:46 PM   #19 (permalink)
NamePros Member
 
Join Date: Jan 2007
Posts: 68
0.00 NP$ (Donate)

baris22 is an unknown quantity at this point


Quote:
Originally Posted by the_internet
OK, good luck with the code.
it did not work

PHP Code:
 $to_replace = array("\r\n","|","wget ","\n","\\r\\n","\\n");
              
$title = str_replace($to_replace, " ", $title);
              
$description = str_replace($to_replace, " ", $description);
baris22 is offline  
Old 11-24-2007, 04:54 PM   #20 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
i cant see why having the string wget would cause problems unless you were running it through exec or something like that.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 11-24-2007, 04:59 PM   #21 (permalink)
NamePros Member
 
Join Date: Jan 2007
Posts: 68
0.00 NP$ (Donate)

baris22 is an unknown quantity at this point


Quote:
Originally Posted by peter@flexiwebhost
i cant see why having the string wget would cause problems unless you were running it through exec or something like that.
I am million times sure. the problem is "wget "

I deleted that word from the post and it worked fine. I tried to enter onlt the word "wget " and it fails.
baris22 is offline  
Old 11-24-2007, 06:39 PM   #22 (permalink)
New Member
 
Join Date: Nov 2007
Posts: 24
68.15 NP$ (Donate)

the_internet is an unknown quantity at this point


Could the problem not be with the word "wget ", but with the space at the end?
Have you tried using "wget" without a space?
the_internet is offline  
Old 11-24-2007, 08:10 PM   #23 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future

Autism Autism Autism Autism Autism Autism Autism
Can you paste the actual error message from phpMyAdmin? It will say exactly what is wrong and should make this really easy.
Dan is offline  
Old 11-25-2007, 02:14 AM   #24 (permalink)
NamePros Member
 
Join Date: Jan 2007
Posts: 68
0.00 NP$ (Donate)

baris22 is an unknown quantity at this point


Quote:
Originally Posted by Dan
Can you paste the actual error message from phpMyAdmin? It will say exactly what is wrong and should make this really easy.
It works perfect if i add from phpmyadmin.

I just made a simple script to try one by one if the problem is "wget "

I can insert "wget" but not "wget "

here is the simple code i am using.

PHP Code:
<?php
$con
= mysql_connect("localhost","user","pass");
if (!
$con)
  {
  die(
'Could not connect: ' . mysql_error());
  }
mysql_select_db("database name", $con);
  
  
$title=$_POST['title'];
  
$description=$_POST['description'];
  
  
$title = str_replace("wget", "ddd", $title);
  
$description = str_replace("wget", "ddd", $description);
  
  
$query="INSERT INTO `filedetails` VALUES ('', '', '".$title."', '".$description."', '','0000000000')";
mysql_query($query);
echo
'MYSQL ERROR IS: '.mysql_error();
mysql_close($con)
?>
<html>
<body>
<form action="insert.php" method="post">
title:<input name="title" type="text" size="100" />
<br>
desc:<textarea name="description" cols="150" rows="10"></textarea>
<br>
<input type="submit" />
</form>
</body>
</html>
When i try to insert "wget " i get this

Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
baris22 is offline  
Old 11-25-2007, 01:49 PM   #25 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future

Autism Autism Autism Autism Autism Autism Autism
Maybe the problem is a bad host?

Are you making it echo the exact query you are trying to run then using that in phpMyAdmin? If you can do the query in phpMyAdmin, then there is no problem with the query...
Dan is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 07:26 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85