ایمیل امن PHP

در اسکریپت ایمیل PHP در بخش قبلی، یک لکه وجود دارد.

تزریق ایمیل PHP

ابتدا، به کد PHP در بخش قبلی نگاه کنید:

<html>
<body>
<?php
if (isset($_REQUEST['email']))
//اگر "ایمیل" پر شده باشد، ایمیل ارسال شود
  {
  //ایمیل ارسال شود
  $email = $_REQUEST['email'] ; 
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail("someone@example.com", "Subject: $subject",
  $message, "From: $email" );
  echo "ممنون از استفاده از فرم ایمیل ما";
  }
else
//اگر "ایمیل" پر نشده باشد، فرم نمایش داده شود
  {
  echo "<form method='post' action='mailform.php'>
  ایمیل: <input name='email' type='text' /><br />
  موضوع: <input name='subject' type='text' /><br />
  پیام:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>
</body>
</html>

مشکل موجود در این کد این است که کاربران غیرمجاز می‌توانند با ورود به فرم داده‌ها را در سراسر ایمیل وارد کنند.

اگر استفاردها در فیلد‌های ورودی فرم این متن‌ها را اضافه کنند، چه چیزی رخ خواهد داد؟

someone@example.com%0ACc:person2@example.com
%0ABcc:person3@example.com,person3@example.com,
anotherperson4@example.com,person5@example.com
%0ABTo:person6@example.com

علاوہ ازیں، mail() فنکشن اپنے متن کو ای میل کے سر میں لگا دیتا ہے، اور اب سر میں مزید میدان Cc:, Bcc: اور To: موجود ہیں۔ جب یوزر ریوایز بٹن پر کلک کرتا ہے تو اس ای میل کو مذکورہ تمام پتا پرمیلی کریگا!

PHP سے ای میل داخل کیجائی کا روکنا

ای میل داخل کیجائی کا بہترین طریقہ یہ ہے کہ وہ نیچل انپٹ ویلیڈیشن کا عمل کریں۔

مقابل کے کوئی کے ساتھ کا کیا جارہا ہے، جس میں ہم نے فرم میں email فیلڈ کی نیچل انپٹ ویلیڈیشن پروگرام شامل کیا ہے:

<html>
<body>
<?php
function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail 
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }
if (isset($_REQUEST['email']))
  {//if "email" is filled out, proceed
  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST['email']);
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
  else
    {//send email
    $email = $_REQUEST['email'] ; 
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    mail("someone@example.com", "Subject: $subject",
    $message, "From: $email" );
    echo "ممنون از استفاده از فرم ایمیل ما";
    }
  }
else
  {// اگر "ایمیل" پر نشده باشد، فرم نمایش داده شود
  echo "<form method='post' action='mailform.php'>
  ایمیل: <input name='email' type='text' /><br />
  موضوع: <input name='subject' type='text' /><br />
  پیام:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>
</body>
</html>

در کد بالا از فیلترهای PHP برای تأیید ورودی استفاده کردیم:

  • FILTER_SANITIZE_EMAIL برای حذف کاراکترهای غیرقانونی ایمیل از رشته استفاده کنید
  • FILTER_VALIDATE_EMAIL برای تأیید آدرس ایمیل استفاده کنید

شما می‌توانید در PHP فیلتردر این بخش می‌توانید بیشتر درباره فیلترها بخوانید.