PHP 보안 이메일

巴扎黑
풀어 주다: 2016-11-11 18:03:48
원래의
1422명이 탐색했습니다.


PHP 보안 이메일

PHP 이메일

PHP 오류

이전 섹션에 PHP 이메일이 있습니다. 메일 스크립트의 취약점.

PHP 이메일 삽입

먼저 이전 섹션의 PHP 코드를 살펴보세요.

<html>
<body>
<?php
if (isset($_REQUEST[&#39;email&#39;]))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST[&#39;email&#39;] ; 
  $subject = $_REQUEST[&#39;subject&#39;] ;
  $message = $_REQUEST[&#39;message&#39;] ;
  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=&#39;post&#39; action=&#39;mailform.php&#39;>
  Email: <input name=&#39;email&#39; type=&#39;text&#39; /><br />
  Subject: <input name=&#39;subject&#39; type=&#39;text&#39; /><br />
  Message:<br />
  <textarea name=&#39;message&#39; rows=&#39;15&#39; cols=&#39;40&#39;>
  </textarea><br />
  <input type=&#39;submit&#39; />
  </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() 함수는 위의 텍스트를 이메일 헤더에 넣으므로 이제 헤더가 추가되었습니다. 추가 참조:, 숨은 참조: 및 받는 사람: 필드. 사용자가 제출 버튼을 클릭하면 이 이메일이 위의 모든 주소로 전송됩니다!

PHP의 이메일 삽입 방지

이메일 삽입을 방지하는 가장 좋은 방법은 입력 내용을 검증하는 것입니다.

다음 코드는 이전 섹션과 유사하지만 양식에서 이메일 필드를 감지하기 위해 입력 유효성 검사기를 추가했습니다.

<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[&#39;email&#39;]))
  {//if "email" is filled out, proceed
  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST[&#39;email&#39;]);
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
  else
    {//send email
    $email = $_REQUEST[&#39;email&#39;] ; 
    $subject = $_REQUEST[&#39;subject&#39;] ;
    $message = $_REQUEST[&#39;message&#39;] ;
    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=&#39;post&#39; action=&#39;mailform.php&#39;>
  Email: <input name=&#39;email&#39; type=&#39;text&#39; /><br />
  Subject: <input name=&#39;subject&#39; type=&#39;text&#39; /><br />
  Message:<br />
  <textarea name=&#39;message&#39; rows=&#39;15&#39; cols=&#39;40&#39;>
  </textarea><br />
  <input type=&#39;submit&#39; />
  </form>";
  }
?>
</body>
</html>
로그인 후 복사

위 코드에서는 PHP 필터링을 사용합니다. 입력을 확인하려면:

FILTER_SANITIZE_EMAIL 문자열에서 이메일의 잘못된 문자를 제거합니다.

FILTER_VALIDATE_EMAIL 이메일 주소 확인



원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!