I have problems with a function I wrote. I call it check_size
and it is supposed to check the message size in the
email script below. The rest of the script works fine, but
the check_size function returns Sorry. Message is to big
even thow I only write one letter. Im trying to make it accept
messages less that 10 letters, and reject messages over 10
letters.
Can anybody help me with this ?
Below is the script.
<?php
# Global variables
$from = escapeshellcmd($_POST["from"]);
$subject = escapeshellcmd($_POST["subject"]);
$message = escapeshellcmd($_POST["message"]);
# Function to check if email address is valid
function valid_email($from) {
if (ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $from))
return true;
else
return false;
}
# Function to check if $subject input is valid
function valid_input($subject) {
if (preg_match('/^[\w\s\.]+$/', $subject))
return true;
else
return false;
}
# Function to check message size
function check_size($size) {
if($size<10) return true;
else return false;
}
if(!$_POST) {
echo "<form method='post' action='$_SERVER[PHP_SELF]'>";
echo "Your emailaddress<br><input type='text' name='from' size=60><br>";
echo "Subject<br><input type='text' name='subject' size=60><br>";
echo "Message<br><textarea name='message' cols=52 rows=11></textarea><br>";
echo "<input type='submit' value='SendMail'>";
echo "</form>";
}
elseif(!($from and $subject and $message)) {
echo "All fields are required to send an email!<br>";
echo "<a href='$_SERVER[PHP_SELF]'>Try again</a>";
}
elseif(!valid_email($_POST['from'])) {
echo "Invalid email address.<br>";
echo "<a href='$_SERVER[PHP_SELF]'>Try again</a>";
}
elseif (!valid_input($_POST['subject']) or !valid_input($_POST['message'])) {
echo "Invalid input. Only letters, numbers and dots are allowed.<br>";
echo "<a href='$_SERVER[PHP_SELF]'>Try again</a>";
}
elseif($_POST['message']>check_size) {
echo "Sorry. Message to big<br>";
echo "<a href='$_SERVER[PHP_SELF]'>Try again</a>";
}
else {
mail("your_email_address@domain.com", $subject, $message, "From: $from");
echo "Your mail is sent!</p>";
echo "<a href='$_SERVER[PHP_SELF]'>Try again</a>";
}
?>