Contact Form v2

Status
Not open for further replies.

o0slowpaul0o

New Member
Messages
254
Reaction score
0
Points
0
I recoded another contact form, I made it more secure and now you have multiple choices of people to e-mail. Things you need edit be in read.

Open up a text edit and put this code in.

Code:
<?
$program = "Contact Us";
$GLOBALS['template'] = "[COLOR=Red]Change to page (Such as template.html or something where ever you want your contact form to blend into.)[/COLOR]";

$thisprog = $_SERVER['SCRIPT_NAME'];
$requireemail = "n"; 
$requirerefer = "n"; 
$requireicode = "y"; 
$validservers = "[COLOR=Red]Your URL[/COLOR]";

$defaultrecip = "[COLOR=Red]Main E-Mail[/COLOR]";
$defaultsubject = "Unknown";

$defaultpage = "$thisprog?thanks";

$validemail = "/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/";

// DISPLAY THE IMAGE CODE IMAGE
if ($_SERVER['QUERY_STRING'] == 'imagecode')
	{
	header("Content-type: image/png");
	$im = imagecreatetruecolor(60,20);
	$background = imagecolorallocate($im, 0, 0, 0);
	imagefill($im, 0, 0, $background);
	$fontcolor = imagecolorallocate($im, 255, 255, 255);
	imagestring ($im, 5, 7, 1, strtoupper(substr(md5(date("d",time())),0,5)), $fontcolor);
	imagepng($im);
	imagedestroy($im);
	exit;
	}

// IF YOU DON'T DEFINE A RECIPIENT IN THE FORM THEN USE DEFAULT
if ($_POST['recip'] == '')
	{
	$recipient = $defaultrecip;
	}
// IF A RECIPIENT WAS DEFINED IN THE FORM USE THAT INSTEAD
else
	{
	$recipient = $_POST['recip'];
	}

// IF YOU DON'T DEFINE A SUBJECT IN THE FORM THEN USE DEFAULT
if ($_POST['subject'] == '')
	{
	$subject = $defaultsubject;
	}
// IF A RECIPIENT WAS DEFINED IN THE FORM USE THAT INSTEAD
else
	{
	$subject = $_POST['subject'];
	}

// IF A NAME WAS NOT PROVIDED THEN SET IT TO UNKNOWN
if ($_POST['name'] == '')
	{
	$fromname = 'Unknown';
	}
// IF A RECIPIENT WAS DEFINED IN THE FORM USE THAT INSTEAD
else
	{
	$fromname = $_POST['name'];
	}

$footer = get_footer();

if ($_POST['message'] == '' && !$_SERVER['QUERY_STRING'])
	{
	$data .= "<FORM ACTION=contactform.php METHOD=POST><CENTER><TABLE>";
	$data .= "<TR><TD WIDTH=150><FONT SIZE=2 FACE=verdana>Your Name:</TD><TD WIDTH=400>";
	$data .= "<INPUT TYPE=TEXT NAME=name SIZE=30></TD></TR>";
	$data .= "<TR><TD><FONT SIZE=2 FACE=verdana>Your email:</TD><TD><INPUT TYPE=TEXT NAME=email SIZE=30></TD></TR>";
	$data .= "<TR><TD><FONT SIZE=2 FACE=verdana>Recipient:</TD>";
	$data .= "<TD><SELECT NAME=recip id='recip'><option value=' ' selected> </option><option value='[COLOR=Red]example@example.com[/COLOR]'>[COLOR=Red]Example[/COLOR]</option><option value='[COLOR=Red]example2@example2.com[/COLOR]'>[COLOR=Red]Example2[/COLOR]</option><option value='[COLOR=Red]example3@example3.com[/COLOR]'>[COLOR=Red]Example3[/COLOR]</option></SELECT></TD></TR>";
	$data .= "<TR><TD><FONT SIZE=2 FACE=verdana>Subject:</TD><TD><INPUT TYPE=TEXT NAME=subject SIZE=30></TD></TR>";
	if ($requireicode == 'y')
		{
		$data .= "<TR><TD><FONT SIZE=2 FACE=verdana>code ( <IMG SRC=$thisprog?imagecode ALIGN=MIDDLE> ):</TD><TD><INPUT TYPE=TEXT NAME=icode SIZE=30></TD></TR>";
		}
	$data .= "<TR><TD><FONT SIZE=2 FACE=verdana>message:</TD><TD><TEXTAREA COLS=30 ROWS=6 NAME=message>";
	$data .= "</TEXTAREA></TD></TR>";
	$data .= "<TR><TD>&nbsp;</TD><TD><INPUT TYPE=SUBMIT VALUE=\"Submit\"></TD></TR></TABLE><P>$footer</CENTER></FORM>";
	printpage($title,$data,"");
	}

// IF A RECIPIENT, SUBJECT AND MESSAGE FOUND THEN ATTEMPT TO SEND IT
if ($recipient != '' && $subject != '' && $_POST['message'] != '')
	{
	if ($requireicode == 'y' && $_POST['icode'] != strtoupper(substr(md5(date("d",time())),0,5)))
		{
		header("Location: $thisprog?invalidcode");
		exit;
		}
	if ($_POST['email'] != '' && preg_match($validemail,$_POST['email']))
		{
		$headers .= "From: $fromname <" . $_POST['email'] . ">\r\n";
		$headers .= "Reply-to: $fromname <" . $_POST['email'] . ">\r\n";
		}
	else
		{
		if ($requireemail == 'y')
			{
			header("Location: $thisprog?emailreq");
			exit;
			}
		$headers = "";
		}

// IF SERVER VALIDATION IS REQUIRED CHECK REFERING SERVER AND REDIRECT TO ERROR PAGE IF INVALID
if ($requirerefer == 'y')
	{
	$validarray = explode(",",$validservers);
	foreach ($validarray as $valid)
		{
		if (ereg($valid,$_SERVER['HTTP_REFERER']))
			{
			$serverisvalid = 'y';
			}
		}
	if ($serverisvalid != 'y') {header("Location: $thisprog?invalidserver"); exit;}
	}

	$_POST['message'] = stripslashes($_POST['message']);
	$subject = stripslashes($subject);
	if ($_POST['email'] == '') {$_POST['email'] = "bounce@" . $_SERVER['SERVER_NAME'];}
	foreach ($_POST as $fname => $fvalue)
		{
		if ($fname != '' && $fname != 'email' && $fname != 'name' && $fname != 'recip' && $fname != 'subject' && $fname != 'message' && $fname != 'thankspage' && $fname != 'icode' && $fvalue != '')
			{
			$formelements .= strtoupper($fname) . ": $fvalue\n";
			}
		}
	if ($formelements != '') {$formelements = "\n" . $formelements;}
	$newmessage = $formelements . $_POST['message'];
	mail ($recipient,$subject,$newmessage,$headers);
	// IF A THANKS PAGE WAS PROVIDED IN THE FORM USE IT
	if ($_POST['thankspage'])
		{
		$_POST['thankspage'] = "http://" . str_replace("http://","",$_POST['thankspage']);
		$thankspage = $_POST['thankspage'];
		header("Location: $thankspage");
		exit;
		}
	// IF NO THANKS PAGE WAS PROVIDED IN THE FORM USE THE DEFAULT THANKS PAGE
	else
		{
		header("Location: $defaultpage");
		exit;
		}
	}
// DISPLAY THE DEFAULT THANKS PAGE - YOU SHOULDN'T NEED TO USE THIS!
elseif ($_SERVER['QUERY_STRING'] == 'thanks')
	{
	$data .= "<CENTER>\n\n";
	$data .= "<BIG><B>Thanks for contacting me! I will get back to you shortly!</B></BIG>";
	$data .= $footer . "\n\n</CENTER>";
	printpage($title,$data,"");
	}
// DISPLAY THE ERROR IF AN EMAIL ADDRESS IS MISSING AND REQUIRED!
elseif ($_SERVER['QUERY_STRING'] == 'emailreq')
	{
	$data = "";
	$data .= "<CENTER>\n\n";
	$data .= "<BIG><B>You need to enter <I>a valid</I> email address!</B></BIG>";
	$data .= $footer . "\n\n</CENTER>";
	printpage($title,$data,"");
	}
// DISPLAY THE INVALID SERVER REFERER ERROR
elseif ($_SERVER['QUERY_STRING'] == 'invalidserver')
	{
	$data .= "<CENTER>\n\n";
	$data .= "<BIG><B>Sorry, this server is not permitted to execute this script!</B></BIG>";
	$data .= $footer . "\n\n</CENTER>";
	printpage($title,$data,"");
	}
// DISPLAY THE ERROR FOR PEOPLE ENTER THE WRONG IMAGE-CODE (TO PREVENT SPAM!!!)
elseif ($_SERVER['QUERY_STRING'] == 'invalidcode')
	{
	$data .= "<CENTER>\n\n";
	$data .= "<BIG><B>Please make sure to enter the valid image code in order to prevent spam!</B></BIG><P><IMG SRC=\"$thisprog?imagecode\" ALT=\"**** SPAM\">";
	$data .= $footer . "\n\n</CENTER>";
	printpage($title,$data,"");
	}
// RETURN THE DEFAULT FORM IF NOTHING ELSE WAS DEFINED
else
	{
	header("Location: $thisprog");
	exit;
	}

function get_footer ()
	{
	$data = " ";
	$data .= " ";
	return $data;
	}

function printpage ($title,$data,$meta)
	{
	$data = "<BR>\n" . $data;
	$newtitle = $GLOBALS['program'] . " -> " . $title;
	$template = file_get_contents($GLOBALS['template']);
	$newdata = str_replace('$title',$newtitle,$template);
	$newdata = str_replace('$meta',$meta,$newdata);
	$newdata = str_replace('$data',$data,$newdata);
	echo $newdata;
	exit;
	}

?>

Save it as contactform.php.

To display it in your webpage you will have to us an Iframe.

You can add more option values just before.

Code:
</SELECT></TD></TR>";
	$data .= "<TR><TD><FONT SIZE=2
 

Iceman

New Member
Messages
308
Reaction score
0
Points
0
The Demo is down....

I'll have to do some chopping of the template codes and stuff (I don't use a lot of variables and have my templates referred to as includes), but it looks great, much better than a lot of free contact forms I've seen on script websites. Awesome work.
 

Tyler

Retired Staff
Messages
8,564
Reaction score
0
Points
0
Dont bump 4 month old topics please :)

Closed
 
Status
Not open for further replies.
Top