hi, i'm new here and i'm pretty new to php, but basically i've been trying to sort this out all day - i'm making a guestbook script but i cant seem to get the security right.
this is my code -
<?php
$self = $_SERVER ['PHP_SELF'];
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$submit = $_POST['submit'];
#the html form
$form = "<form action\"$self\" method=\"post\" name=\"form\" onSubmit=\"return Check()\">";
$form.= "<input type=\"text\" value=\"Name\" name=\"name\" class=\"formbutton2\" ";
$form.= "size=\"50\" value=\"$name\"> <br>";
$form.= "<input type=\"text\" value=\"Website Address (without http://)\" name=\"email\" class=\"formbutton2\" ";
$form.= "size=\"50\" value=\"$email\"> <br>";
$form.= "<span class=\"content\">Comments:</span><br>";
$form.= "<textarea name=\"comments\" class=\"formbutton3\" ";
$form.= "rows=\"4\">$comments</textarea> <br>";
$form.= "<input type=\"submit\" name=\"submit\" class=\"formbutton\" ";
$form.= "value=\"Sign\"></form>";
#on first opening display the form
if ( !$submit) { $msg = $form; }
#or redisplay a message and the form if incomplete
else if ( !$name or !$email or !$comments)
{ $msg = "<b>Please complete all fields</b><br><br>";
$msg.= $form; }
#or add the form data to the guestbook database table
else #otherwise connect to mysql
{ $conn = @mysql_connect( "host.com", "database", "password" )
or die( "could not connect" );
$rs = @mysql_select_db( "database", $conn )
or die ( "could not select database" );
if( $name and $comments )
{
$sql ="insert into guestbook (name, email, comments)
values(\"$name\", \"$email\", \"$comments\")";
$rs = @mysql_query( $sql, $conn )
or die ( "could not execute sql query" ); }
if($rs)
{ $msg = "<h3>Thank you, your entry has been saved.";
$msg.= "<br><a href=\"view.php\">";
$msg.= "View guestbook</a></h3>"; }
}
echo( $msg );
?>
could somebody please show me how to make it secure from people trying to post malicious code on it? it'd be a massive help to me,
Solidgold