Jump to content

Comment protection against sql injection help need to be secure!


Minimeallolla

Recommended Posts

<?php

include ("database.php");

// show comments

$result = mysql_query("SELECT * FROM gamecomments");

while($row = mysql_fetch_array($result))

  {

  echo $row['username'] . ": <Br> " . $row['comment'];

  echo "<p>";

}

ini_set ("display_errors", "1");

error_reporting(E_ALL);

 

 

 

if (isset($_POST['submit'])) {

 

 

 

        // now we insert it into the database

$insert = "INSERT INTO gamecomments (username, comment)

VALUES ('[$username]', '$_POST[comment]')";

 

$add_comment = mysql_query($insert);

{

echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=games.php\">";

  }

}

Would this work or something like this? need help still \=

<?php
include ("database.php");
// show comments
$result = mysql_query("SELECT * FROM gamecomments");
while($row = mysql_fetch_array($result))
  {
  echo addslashes($row['username']) . ": <Br> " . addslashes($row['comment']);
  echo "<p>";
}
      ini_set ("display_errors", "1");
      error_reporting(E_ALL);
      

      
      if (isset($_POST['submit'])) {
         
      	$username = real_escape_string($username);
		$_POST['comment'] = real_escape_String($comment);
         
           // now we insert it into the database
   $insert = "INSERT INTO gamecomments (username, comment)
   VALUES ('[$username]', '$_POST[comment]')";

   $add_comment = mysql_query($insert);
{
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=games.php\">";
  }
}
[code]

sorry, im really tired \=  so just that like? will prevent sql injection? and protect everything?

<?php
include ("database.php");
// show comments
$result = mysql_query("SELECT * FROM gamecomments");
while($row = mysql_fetch_array($result))
  {
  echo ($row['username']) . ": <Br> " . ($row['comment']);
  echo "<p>";
}
      ini_set ("display_errors", "1");
      error_reporting(E_ALL);
      

      
      if (isset($_POST['submit'])) {
         
         $username = mysql_real_escape_string($username);
		$_POST['comment'] = mysql_real_escape_string($comment);

           // now we insert it into the database
   $insert = "INSERT INTO gamecomments (username, comment)
   VALUES ('[$username]', '$_POST[comment]')";

   $add_comment = mysql_query($insert);
{
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=games.php\">";
  }
}

Where does $username come from?  Echoing row data isn't the same as constructing a variable.  Also, your assignment statement dealing with the comments is backwards.

 

The escape function will protect you from injection, but you should validate all incoming data.  When you display your comments, you'll need to combat against unwanted HTML and XSS attacks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.