Jump to content

[SOLVED] Need help protecting this please!


adamjones

Recommended Posts

Hi (:

 

I have a really simple code, which I'm using on a basic forum. When a user submits a new thread, or a reply to one, their message is sent to this file, where it is then posted;

 

<?php
session_start();
$host="localhost";
$username=""; 
$password="";
$db_name="";
$tbl_name="questions"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_SESSION['user'];
$forum=$_GET['forum'];
$datetime=date("d/m/y h:i:s"); 

if($topic == '') {
	$errmsg_arr[] = 'You need to give your post a name!';
	$errflag = true;
}

if($detail == '') {
	$errmsg_arr[] = 'You cant submit a blank message!';
	$errflag = true;
}

if($errflag) {
	$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
	session_write_close();
	header("location: create_topic.php");
	exit();
}

$sql="INSERT INTO $tbl_name(title, message, author, datetime, forum)VALUES('$topic', '$detail', '$name', '$datetime', '$forum')";
$result=mysql_query($sql);

if($result){

$sql2="UPDATE categories SET last='$topic' WHERE shortname='$forum'";
$result2=mysql_query($sql2);

if($result2){

header("location: forums.php?forum=".$forum."");
		exit();
	}else {
		header("location: error.php");
		exit();
	}
	}
	?>

 

What I'm wondering is could a user not submit a hack, or something, which could destroy my database? If anyone knows how, please can you help me protect this, as I have no idea! Also, is it possible to remove HTML submitted? So if a user posts some coding, it is just removed?

 

Thank's for your help!

Link to comment
https://forums.phpfreaks.com/topic/174743-solved-need-help-protecting-this-please/
Share on other sites

http://www.denhamcoote.com/php-howto-sanitize-database-inputs

seems like this is what you want.

 

Always sanitize user input.. any GET POST input for that matter

 

Also good idea to typecast when possible and add 'LIMIT 1' to your SQL Querys when possible

 

on that note:

http://lmgtfy.com/?q=php+sanitizing+your+data

:D

http://www.denhamcoote.com/php-howto-sanitize-database-inputs

seems like this is what you want.

 

Always sanitize user input.. any GET POST input for that matter

 

Also good idea to typecast when possible and add 'LIMIT 1' to your SQL Querys when possible

 

on that note:

http://lmgtfy.com/?q=php+sanitizing+your+data

:D

 

Thankyou very much (:

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.