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
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

 

Thankyou very much (:

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.