Undefined index problem

So, I’ve been using the well known “Jem’s form to mail” script on my website. All has been fine until suddenly I noticed errors. Looking through the error logs I can see lots of “undefined index” errors. Trouble is, I don’t know how to fix them, or why they are suddenly a problem!

Here is the full php script in question:
[php]

<?php // OPTIONS - PLEASE CONFIGURE THESE BEFORE USE! $yourEmail = "MY EMAIL"; // the email address you wish to receive these mails through $yourWebsite = "WEBSITE NAME"; // the name of your website $thanksPage = 'thankyou.html'; // URL to 'thanks for sending mail' page; leave empty to keep message on the same page $maxPoints = 4; // max points a person can hit before it refuses to submit - recommend 4 $requiredFields = "name,email,comments"; // names of the fields you'd like to be required as a minimum, separate each field with a comma // DO NOT EDIT BELOW HERE $error_msg = array(); $result = null; $requiredFields = explode(",", $requiredFields); function clean($data) { $data = trim(stripslashes(strip_tags($data))); return $data; } function isBot() { $bots = array("Indy", "Blaiz", "Java", "libwww-perl", "Python", "OutfoxBot", "User-Agent", "PycURL", "AlphaServer", "T8Abot", "Syntryx", "WinHttp", "WebBandit", "nicebot", "Teoma", "alexa", "froogle", "inktomi", "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory", "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot", "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz"); foreach ($bots as $bot) if (stripos($_SERVER['HTTP_USER_AGENT'], $bot) !== false) return true; if (empty($_SERVER['HTTP_USER_AGENT']) || $_SERVER['HTTP_USER_AGENT'] == " ") return true; return false; } if ($_SERVER['REQUEST_METHOD'] == "POST") { if (isBot() !== false) $error_msg[] = "No bots please! UA reported as: ".$_SERVER['HTTP_USER_AGENT']; // lets check a few things - not enough to trigger an error on their own, but worth assigning a spam score.. // score quickly adds up therefore allowing genuine users with 'accidental' score through but cutting out real spam :) $points = (int)0; $badwords = array("adult", "javascript"); REMOVED FOR SAKE OF POST foreach ($badwords as $word) [b]ERRORS ARE BEING CAUSED FROM HERE [/b] if ( strpos(strtolower($_POST['comments']), $word) !== false || strpos(strtolower($_POST['name']), $word) !== false ) $points += 2; if (strpos($_POST['comments'], "http://") !== false || strpos($_POST['comments'], "www.") !== false) $points += 2; if (isset($_POST['nojs'])) $points += 1; if (preg_match("/(<.*>)/i", $_POST['comments'])) $points += 2; if (strlen($_POST['name']) < 3) $points += 1; if (strlen($_POST['comments']) < 15 || strlen($_POST['comments'] > 1500)) $points += 2; if (preg_match("/[bcdfghjklmnpqrstvwxyz]{7,}/i", $_POST['comments'])) $points += 1; // end score assignments foreach($requiredFields as $field) { trim($_POST[$field]); if (!isset($_POST[$field]) || empty($_POST[$field]) && array_pop($error_msg) != "We are experiencing some technical difficulties.\r\n") $error_msg[] = "We are experiencing technical difficulties - PLEASE FILL IN FORM AGAIN - IT WILL BE SENT! Sorry for the inconvenience."; } if (!empty($_POST['name']) && !preg_match("/^[a-zA-Z-'\s]*$/", stripslashes($_POST['name']))) $error_msg[] = "The name field must not contain special characters.\r\n"; if (!empty($_POST['email']) && !preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i', strtolower($_POST['email']))) $error_msg[] = "That is not a valid e-mail address.\r\n"; if (!empty($_POST['url']) && !preg_match('/^(http|https):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i', $_POST['url'])) $error_msg[] = "Invalid website url.\r\n"; if ($error_msg == NULL && $points <= $maxPoints) { $subject = "Automatic Form Email"; $message = "You received this e-mail message through your website: \n\n"; foreach ($_POST as $key => $val) { if (is_array($val)) { foreach ($val as $subval) { $message .= ucwords($key) . ": " . clean($subval) . "\r\n"; } } else { $message .= ucwords($key) . ": " . clean($val) . "\r\n"; } } $message .= "\r\n"; $message .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n"; $message .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n"; $message .= 'Points: '.$points; if (strstr($_SERVER['SERVER_SOFTWARE'], "Win")) { $headers = "From: $yourEmail\r\n"; } else { $headers = "From: $yourWebsite <$yourEmail>\r\n"; } $headers .= "Reply-To: {$_POST['email']}\r\n"; if (mail($yourEmail,$subject,$message,$headers)) { if (!empty($thanksPage)) { header("Location: $thanksPage"); exit; } else { $result = 'Your mail was successfully sent.'; $disable = true; } } else { $error_msg[] = 'Your mail could not be sent this time. ['.$points.']'; } } else { if (empty($error_msg)) $error_msg[] = 'Your mail looks too much like spam, and could not be sent this time. ['.$points.']'; } } function get_data($var) { if (isset($_POST[$var])) echo htmlspecialchars($_POST[$var]); } ?> My Email Form
<style type="text/css">
	p.error, p.success {
		font-weight: bold;
		padding: 10px;
		border: 1px solid;
	}
	p.error {
		background: #ffc0c0;
		color: #900;
	}
	p.success {
		background: #b3ff69;
		color: #4fa000;
	}
</style>
<?php if (!empty($error_msg)) { echo '

ERROR: '. implode("
", $error_msg) . "

"; } if ($result != NULL) { echo '

'. $result . "

"; } ?>

Name: * " />

<label for="email">Email: *</label> 
	<input type="text" name="email" id="email" value="<?php get_data("email"); ?>" /><br />


<label for="comments">Comments: *</label>
	<textarea name="comments" id="comments" rows="5" cols="20"><?php get_data("comments"); ?></textarea><br />

/>

Powered by Jem's PHP Mail Form

[/php]

Someone please help!

To get better help you should only post the part of the script that you think is going to get you the help. You can get a good hint where the script is bugging out on you by the error code, it usually a line number that is either it or a few lines before or after.

There are a few area’s I could foresee a warning being thrown. So, more info would be needed.

Undefined indexes are usually because you used a variable before it was defined. A quick review of your
code did not show any of these. What line of code does it fail at? How did you debug it so far?

To debug this type of code, place some code it inside your routines to cause it to fail and to show what is
inside the variables. Test-run the code and see if the results are accurate. In that way you can track down
where the error is.

For instance, just before mailing out the email ( " if (mail($yourEmail,$subject,$message,$headers)) { " ),
place something like this:
[php]
echo "
email: " . $yourEmail . “
”;
echo "subject: " . $subject . “
”;
echo "message: " . $message . “
”;
die ("headers: " . $headers . “
”);
if (mail($yourEmail,$subject,$message,$headers)) {
[/php]
This will show all of the variables that are sent into the mail function. You can verify that the data inside of
them are accurate. If they are, then move back up or further down your code checking other variables.
Then, you can locate the bad area of code and fix it.

You can also, just add one line here and there in your code such as "die(“got here…”); which would let you
find out how far the code gets before it dies. All of these are simple ways to debug code. Of course, do it
on a test page, not a live site or others sending messages will see odd results.

Not sure if this helps, but, should point you at the problems area. Let us know if you solve it or need any
further help…

Sponsor our Newsletter | Privacy Policy | Terms of Service