PHP secure email

Zài shàng yī jié zhōng de PHP yóu jiàn shù bēn zhōng, cún zài yī gè kòu lù.

PHP yóu jiàn zhù rù

Shǒu zhǐ, qǐng kàn shàng yī jié zhōng de PHP dāo má

<html>
<body>
<?php
if (isset($_REQUEST['email']))
//ràng "yóu jiàn" zhèng jiàn, shū fā yóu jiàn
  {
  //shū fā yóu jiàn
  $email = $_REQUEST['email'] ; 
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail("someone@example.com", "Subject: $subject",
  $message, "From: $email" );
  echo "Thank you for using our mail form";
  }
else
//ràng "yóu jiàn" bù zhèng jiàn, zhǎn shì biǎo tíng
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>
</body>
</html>

Shàng miàn dāo shù de mài cóng zhèn wèn shì, wú shēn quán zhǔ de yòng hù kě yǐ tōng guò shū rù biǎo tíng zài yóu jiàn tóu bù chā rù shù jù.

Kwai bai zhe xie wén bēn zài biǎo tíng zhōng de shū rù kuāng nèi jiā rù zhè xiē wén bēn, huì chū xiàn shén me qíng kuàng ne?

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

Dahala kamar yadda ake gani, mail() function kaiwa tekun da ake gudanarwa zuwa kofin mai shirin, kuma yanzu akwai sassa na kofin da aka yi sabon, kamar Cc:, Bcc: da To:. Kamar yadda wani ya ci karo sabon bata, e-mail ake gudanarwa zuwa kowane wuri da aka shirya!

PHP a kare e-mail

Kusanin harshe e-mail a kuskure ce sabon harshe.

Kodin da yake da sannan yana da kewayawa, amma amfana da tsarin nazarin input form email:

<html>
<body>
<?php
function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail 
  
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  //filter_var() validates the e-mail
  
  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
    {//rannar email
    $email = $_REQUEST['email'] ; 
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    mail("someone@example.com", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form";
    }
  }
else
  {//if "email" is not filled out, display the form
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>
</body>
</html>

In the above code, we used PHP filters to validate the input:

  • FILTER_SANITIZE_EMAIL to remove illegal characters from email strings
  • FILTER_VALIDATE_EMAIL to verify email addresses

You can find more about our PHP FilterRead more about filters in this section.