Jump to content

How would I protect......


MySQL_Narb

Recommended Posts

Wait, when I added it, everytime I try and post it doesn't work. It keeps saying illegal characters, test here: http://chataddict.netau.net/index.php

 

Username: demo Pass: demo

 

Code:

 

<?php require "global_settings.php"; ?>
<title><?php echo $sitetitle; ?></title>
<center><style type="text/css">

a:link {
color:#24374C;
text-decoration:bold;
}

a:visited {
color:#24374C;
text-decoration:bold;
}

a:active {
outline: none;
color:#24374C;
text-decoration:bold;
}

body {background-color:#b0c4de}

div.box {
width:250px;
padding:10px;
border:3px double #000000;
margin:10px;
background-color:#74AFF2;
}

p
{
border-top-style:dotted;
border-right-style:solid;
border-bottom-style:dotted;
border-left-style:solid;
}

div.menu-blue {
BORDER-RIGHT: #333366 1px solid;
BORDER-LEFT: #6699cc 1px solid;
BORDER-TOP: #6699cc 1px solid;
BORDER-BOTTOM: #333366 1px solid;

FONT-WEIGHT: normal;
FONT-SIZE: 2px;
COLOR: #ffffff;
FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;
BACKGROUND-COLOR: #23559C;
TEXT-DECORATION: none;
font-stretch : condensed;
}

.menu-top  {
BORDER-RIGHT: 1px solid #333366; BORDER-TOP: 1px solid #6699CC; FONT-WEIGHT: normal; FONT-SIZE: 2px; BORDER-LEFT: 1px solid #6699CC; COLOR: #FFFFFF; BORDER-BOTTOM: 1px solid #333366; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; BACKGROUND-COLOR: #23559C; TEXT-DECORATION: none;
font-stretch : condensed
}

</style>
<center>
<div class='menu-blue'>
<div align="center"> 
<table width="600" cellspacing="1" cellpadding="5" style="background-color:#23559C"> 
<tr> 
<td style="background-color:#FFFFFF"> 


    <div align="center"> 
    <table border="0"> 
    
    </form> 
    </table>
<?php

$con = mysql_connect("$dbhost", "$dbuser", "$dbpassword") 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']);

    if (!preg_match("/^[-_a-zA-Z0-9]+$/", $message))

   { 
      echo "<div class='box'>Contains illegal characters!</div>";
   }
else
   {

if (!$name) {
    echo "<div class='box'><b><span style='color:red'>You must be logged in to post!</span></b></div>";
}
else
{

//connect
$connect = mysql_connect("$dbhost","$dbuser","$dbpassword") or die("Connection failed!");
mysql_select_db("$db") or die("Database fail!");

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

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>";
}
   }
?>

Link to comment
Share on other sites

replace

if (!preg_match("/^[-_a-zA-Z0-9]+$/", $_POST['message']))
   { 
      echo "<div class='box'>Contains illegal characters!</div>";
   }
   exit

 

with

 

if (!preg_match("/^[-_a-zA-Z0-9]+$/", $_POST['message']))
   { 
      echo "<div class='box'>Contains illegal characters!</div>";
exit;
   }
   

Link to comment
Share on other sites

Sorry, i feel very dumb.. replace all you have with this:

remove any characters contained in the slashes you dont want out:

 

so you can remove any of this:

^[-_a-zA-Z0-9]+$

 

if (preg_match("/^[-_a-zA-Z0-9]+$/", $_POST['message']))

  {

      echo "<div class='box'>Contains illegal characters!</div>";

exit;

  }

Link to comment
Share on other sites

"/[^-_a-zA-Z0-9]+/" 

Will match one or more of any character other than those listed.

 

^[-_a-zA-Z0-9]+$

Will match if the string consists of only the allowed characters.

 

Are they not both equally viable options? The only difference is whether you consider the item as validated when preg_match returns true or false.

 

you have your preg_match wrong.. the ^ (carrot) should be inside the brackets.

Is that not a caret character as opposed to an orange vegetable?  :shrug:

Link to comment
Share on other sites

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.