Page 1 of 1

Contact form problem with IP checking

Posted: Tue Oct 07, 2008 5:17 am
by Sindarin
I am currently having problems with my contact form. The script works in one site of mine while it doesn't for another. I can't figure it out as the script seems to be error free. The problem is with the IP checking. It doesn't work while it should.

Code: Select all

<?php
 
//Contact Form
 
//the sending address [From]
$from = "contact_us@xxxxx.gr";
 
//the destination address [To]
//$to = "contact_us@xxxxx.gr";
$to = "info@xxxxxx.gr";
 
 
//the message subject
$subject = "New Contact [xxxxxx]";
 
//ip list filename
$ip_file="../ip-list.txt";
 
//function email_validate
function email_validate ($email) 
{
    if (strlen (trim ($email)))
        return (eregi("^[a-z0-9]([_\\.\\-]?[a-z0-9]+)*@((([a-z0-9]+[a-z0-9\\-]*[a\-z0-9]+)|[a-z0-9])+\\.)+[a-z]{2,10}$", $email));
    return false;
}   
 
//get variables from form
 
$first = $_POST['first'];
$last = $_POST['last'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$address = $_POST['address'];
$number = $_POST['arithm'];
$city = $_POST['city'];
$tk = $_POST['tk'];
$email = $_POST['email'];
$url = $_POST['url'];
$message = $_POST['text'];
 
//get IP address
$ip=$_SERVER['REMOTE_ADDR'];
 
//read file and check the IPs from the array
 
$ip_id=fopen($ip_file,"r");
$ip_array=fread($ip_id,9999);
fclose($ip_id);
$ip_array = explode("\n", $ip_array);
 
//check the IP
if(in_array($_SERVER['REMOTE_ADDR'], $ip_array)) 
{
//blocked IP returned, display error and stop submission
echo "<center><font family='Verdana'><font >We are sorry but your IP address <font color='red'><b>$ip</b></font> has been blocked from using the contact form.<br /></font></center>";
}
else
{
 
//IP not in blocked IP list, allow contact
 
//check the required fields and validate email
$email_is_valid=email_validate($email);
if ($first=="" || $last=="" || $message=="" || $email=="" || $email_is_valid==false)
{
//some of the required fields are not filled
echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/><center>?? ???????? ?????????? ?????????????? ???? ??? ??????? ??? ??????:<br /><br />";
if ($first==""){echo "<font color='red'><br/>?? ????? ????? ??????????? ?????.<br />";}
if ($last==""){echo "?? ??????? ????? ??????????? ?????<br />";}
if ($message==""){echo "?? ?????? ??? ????? ????????????.<br />";}
if ($email_is_valid==false){echo "? ????????? e-mail "; if ($email==""){echo "??? ????? ???????????? ";} echo "??? ??? ????? ??????.<br /></font color>";}
echo "<br /><br /><a href='javascript&#058;history.back(-1);'><b>[ OK ]</b></a></center>";
}
else
{
//all required fields are completed
 
//wrap characters so the message shows shorter
$wrappedmessage =wordwrap($message, 50, "<br />\n");
    
//set the message content
$form_message = "
 
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-7' />
<title>Contact Form Message</title>
<style type='text/css'>
<!--
.style2 {font-size: 10px}
body,td,th {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    color: #999999;
}
body {
    background-color: #FFFFFF;
    margin-left: 32px;
    margin-top: 32px;
    margin-right: 32px;
    margin-bottom: 32px;
}
.style3 {font-size: 12px; }
.style4 {
    color: #000000;
    font-weight: bold;
}
.style5 {font-size: 12px; color: #000000; }
-->
</style>
</head>
 
<body>
<table width='349' height='508' border='1' bordercolor='#666666'>
  <tr>
    <td height='26' colspan='2'><div align='center'><strong>xxxxx | ??? ?????? ??? ?? ????? ???????????? (??? ????????)</strong></div></td>
  </tr>
  <tr>
    <td height='19' colspan='2'>&nbsp;</td>
  </tr>
  <tr>
    <td height='28' align='left' valign='middle'>IP Address</td>
    <td align='left' valign='middle'><span class='style4'>$ip</span></td>
  </tr>
  <tr>
    <td width='145' height='30' align='left' valign='middle'>?????</td>
    <td width='188' align='left' valign='middle'><span class='style4'>$first</span></td>
  </tr>
  <tr>
    <td height='30' align='left' valign='middle'>???????</td>
    <td align='left' valign='middle'><span class='style4'>$last</span></td>
  </tr>
  <tr>
    <td height='31' align='left' valign='middle'>????. ????????</td>
    <td align='left' valign='middle'><span class='style4'>$day $month $year</span></td>
  </tr>
  <tr>
    <td height='30' align='left' valign='middle'>?????????</td>
    <td align='left' valign='middle'><span class='style4'>$address $number $city</span></td>
  </tr>
  <tr>
    <td height='31' align='left' valign='middle'>??</td>
    <td align='left' valign='middle'><span class='style4'> $tk</span></td>
  </tr>
  <tr>
    <td height='31' align='left' valign='middle'>e-mail</td>
    <td align='left' valign='middle'><span class='style4'>$email</span></td>
  </tr>
  <tr>
    <td height='31' align='left' valign='middle'>??? ??? ?????? ??? ????: </td>
    <td align='left' valign='middle'><span class='style4'>$url</span></td>
  </tr>
  <tr>
    <td height='29' align='left' valign='middle' class='style3'>??????</td>
    <td align='left' valign='middle' class='style2'>&nbsp;</td>
  </tr>
  <tr>
    <td height='111' colspan='2' align='left' valign='top' class='style5'>$message</td>
  </tr>
  <tr>
    <td height='51' colspan='2' class='style2'><div align='justify'>To minima auto einai kodikopoiimeno se ansi characters</div></td>
  </tr>
</table>
</body>
</html>
 
 
";
    
//set the mail headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-7\r\n";
$headers .= "To: <$from> \r\n";
$headers .= "From: <$to>\r\n";
    
//send the message
mail($to, $subject, $form_message, $headers);
 
    //output success html to the user
echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/><p style='a:link {color: #840E64;text-decoration: none;}'><font color='black'><center><br/>?? ?????? ??? ?????????? ?? ????????!</font><br /><a href='javascript&#058;history.back(-1);'><b>[ OK ]</b></a></p></center>";
 
}
}
 
?>
 

Re: Contact form problem with IP checking

Posted: Tue Oct 07, 2008 5:29 am
by VladSun

Code: Select all

<?php
 
//read file and check the IPs from the array
 
$ip_id=fopen($ip_file,"r");
$ip_array=fread($ip_id,9999);
fclose($ip_id);
$ip_array = explode("\n", $ip_array);
 
//check the IP
if(in_array($_SERVER['REMOTE_ADDR'], $ip_array)) 
{
.....
 
You have to rewrite this part of your code.
1. To read the file use file_get_contents();
2. To search for a specific IP in the file contents use strpos();
3. Or you may replace the whole part with a single code line by using grep (Linux):

Code: Select all

$exists = system('grep ^'.$_SERVER['REMOTE_ADDR'].'$ '.$ip_file) != '';

Re: Contact form problem with IP checking

Posted: Tue Oct 07, 2008 8:50 am
by Sindarin
file_get_contents();
The server doesn't have PHP5. :/