Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-13-2009, 07:00 PM   PM User | #1
SRBuckey5266
New Coder

 
Join Date: Jan 2009
Posts: 45
Thanks: 3
Thanked 0 Times in 0 Posts
SRBuckey5266 is an unknown quantity at this point
Pllleeeaasseee help!

I have this code:

PHP Code:
<?php

$name 
$_POST['name'];
$message $_POST['message'];

if(!
eregi("^([0-9a-z])*$"$message)){
  echo 
"<div class='box'>Please use letters only.</div>"
}
else
{
 
//connect
$connect mysql_connect("","","") or die("Connection failed!");
mysql_select_db("") or die("Database fail!");

//write
$write mysql_query("INSERT INTO posts VALUES ('','$name','$message')") or die(mysql_eror());

echo 
"<div class='box'><font face='arial'><b><span style='color:green'>Posted! Your name was:</span> $name</b> - Your message was....<br><br><b>$message - <a href='index.php'>View it!</a></b>";
}

?>
Now if you go here: http://chataddict.netau.net/ - and type your message, it keeps displaying the error box. Why??!

Last edited by SRBuckey5266; 10-13-2009 at 07:30 PM..
SRBuckey5266 is offline   Reply With Quote
Old 10-13-2009, 07:09 PM   PM User | #2
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
In the future, please use a more descriptive subject when posting a question. See posting guidelines.

I went to that link and was able to post without getting an error. I just couldn't use a newline (aka press enter) but that is because your regular expression doesn't allow it. It doesn't allow punctuation either.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Old 10-13-2009, 07:12 PM   PM User | #3
SRBuckey5266
New Coder

 
Join Date: Jan 2009
Posts: 45
Thanks: 3
Thanked 0 Times in 0 Posts
SRBuckey5266 is an unknown quantity at this point
What can I do to improve it?

And it's still not letting me post.
SRBuckey5266 is offline   Reply With Quote
Old 10-13-2009, 07:16 PM   PM User | #4
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
Well you shouldn't use the eregi function anyways since it is deprecated.

But what things are you trying to block from being in messages?

Edit: The page isn't working for me now though that second post on the page did work but no longer does now.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Old 10-13-2009, 07:17 PM   PM User | #5
SRBuckey5266
New Coder

 
Join Date: Jan 2009
Posts: 45
Thanks: 3
Thanked 0 Times in 0 Posts
SRBuckey5266 is an unknown quantity at this point
Quote:
Originally Posted by oracleguy View Post
Well you shouldn't use the eregi function anyways since it is deprecated.

But what things are you trying to block from being in messages?
The simple things to protect from SQL Injections, I just want the following blocked out: ;$'^#@

I guess I'll remove the code. :/

Thank you for the help.
SRBuckey5266 is offline   Reply With Quote
Old 10-13-2009, 07:19 PM   PM User | #6
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
Then just use mysql_real_escape_string and you should use it on the name and the message. That will auto escape any special characters that could be used for SQL injection.

See:

PHP Code:
<?php
 
//connect
$connect mysql_connect("","","") or die("Connection failed!");
mysql_select_db("") or die("Database fail!");

$name mysql_real_escape_string($_POST['name']);
$message mysql_real_escape_string($_POST['message']);

//write
$write mysql_query("INSERT INTO posts VALUES ('','$name','$message')") or die(mysql_eror());

echo 
"<div class='box'><font face='arial'><b><span style='color:green'>Posted! Your name was:</span> $name</b> - Your message was....<br><br><b>$message - <a href='index.php'>View it!</a></b>";


?>
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Old 10-13-2009, 07:21 PM   PM User | #7
SRBuckey5266
New Coder

 
Join Date: Jan 2009
Posts: 45
Thanks: 3
Thanked 0 Times in 0 Posts
SRBuckey5266 is an unknown quantity at this point
Thank you Oracle! I'll make a credits list, and I'll add you, and a link to your profile. I really appreciate it!
SRBuckey5266 is offline   Reply With Quote
Old 10-13-2009, 07:26 PM   PM User | #8
SRBuckey5266
New Coder

 
Join Date: Jan 2009
Posts: 45
Thanks: 3
Thanked 0 Times in 0 Posts
SRBuckey5266 is an unknown quantity at this point
Wait, now I get this error:


Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home/a5488351/public_html/post.php on line 33

Free Web Hosting

PHP Error Message

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/a5488351/public_html/post.php on line 33

Free Web Hosting

PHP Error Message

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home/a5488351/public_html/post.php on line 34

Free Web Hosting

PHP Error Message

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/a5488351/public_html/post.php on line 34
SRBuckey5266 is offline   Reply With Quote
Old 10-13-2009, 07:30 PM   PM User | #9
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
Did you connect to the database before you called mysql_real_escape_string like I did revised version of your code that I posted?
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Old 10-13-2009, 07:32 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
mysql_real_escape_string requires you're connection to the database is established. Ensure that you're using you're mysql_connect prior to the use of mysql_real_escape_string.
Also, until PHP6, there is a possibility of magic_quotes_gpc being enabled on you're server. The idea behind it was to prevent sql injections, but they are not compatible with 'real' (ie: from the database) sanitation. So, you'll need to code to handle that as well:
PHP Code:
$con mysql_connect('''''') or die(mysql_errno());
if (
function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
    
$_POST array_map('stripslashes'$_POST);
}

$name mysql_real_escape_string($_POST['name']);
$message mysql_real_escape_string($_POST['message']);
.... 
Of course, if its not a string you're intending to handle, cast it to the specific type (like an int), and ignore the mysql_real_escape_string. Any input data in PHP is considered a string, so its up to you to control what is really what.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 10-13-2009, 07:56 PM   PM User | #11
SRBuckey5266
New Coder

 
Join Date: Jan 2009
Posts: 45
Thanks: 3
Thanked 0 Times in 0 Posts
SRBuckey5266 is an unknown quantity at this point
I don't think that protects from codes. I want a code that stops you and says: "Please use letters only." if they type in stuff like: $[];'{}

Can anyone do this?
SRBuckey5266 is offline   Reply With Quote
Old 10-13-2009, 08:04 PM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by SRBuckey5266 View Post
I don't think that protects from codes. I want a code that stops you and says: "Please use letters only." if they type in stuff like: $[];'{}

Can anyone do this?
Well yes actually it will protect from code, at least PHP code. The purpose of mysql_real_escape_string is to convert escapable data into non-escapable data string allowing you to store it in a database properly. Should you want to remove tags to prevent html and xss injection, you can look at using strip_tags and htmlentities to take care of those conversions.
To match just letters you can pattern match with if (preg_match('/^[a-z]*$/i', $input)), but thats letters only, no spaces or numbers.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 10-13-2009, 08:23 PM   PM User | #13
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
Quote:
Originally Posted by Fou-Lu View Post
he purpose of mysql_real_escape_string is to convert escapable data into non-escapable data string allowing you to store it in a database properly.
Aka meaning it prevents SQL injection. So it should do what you want. There is no need to block $[];'{}. It isn't like if someone were to write $foo = 8; in the message that the code would get executed.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 09:21 AM.


Advertisement
Log in to turn off these ads.