PHP సెక్యూర్ ఇమెయిల్

ముందస్తు సెక్షన్లో ఉన్న PHP e-mail స్క్రిప్ట్ లో ఒక లోహితం ఉంది.

PHP E-mail ఇంజెక్షన్

మొదటగా, ముందస్తు సెక్షన్లో ఉన్న PHP కోడ్ ను చూడండి:

<html>
<body>
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send 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'>
  ఇమెయిల్: <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 "Thank you for using our mail form";
    }
  }
else
  {//if "email" is not filled out, display the form
  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 ఫిల్టర్ఈ సెక్షన్ లో ఫిల్టర్ గురించి మరింత చదవండి.