Home
Main Page

PHP Basic
Introduction
Syntax
Variables
Operators
If...Else
Switch
Arrays
Looping
Functions
Forms
GET
POST

PHP Advanced
Date
Include
File
File Upload
Cookies
Sessions
Mail
Secure Mail

MySQL Database
Introduction
Connect
Create
Insert
Select
Where
Order By
Update
Delete

PHP Database
ODBC

PHP Reference
PHP Manual

PHP Secure Mail



การอัดส่งอีเมล์ (Email Injections)

มาดูที่โค้ดส่งเมล์จากบทก่อน:

<html>
<body>
<?php
if (isset($_REQUEST['email']))
  {
  $email = $_REQUEST['email'] ; 
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail( "webmaster@domain.com", $subject,
  $message, "From: $email" );

  echo "ขอบคุณที่ใช้ฟอร์มเมล์ของเรา";
  }
else
  {
  echo "<form method='post' action=''>
  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>
ปัญหาจากโค้ดข้างบนคือ ผู้ใช้ที่ไม่มีสิทธิ์สามารถใส่ชื่ออีเมล์ไปไว้ที่ส่วนของ headers ผ่านแบบฟอร์มนี้ได้

จะเกิดอะไรขึ้น ถ้าผู้ใช้ใส่ข้อมูลรายชื่ออีเมล์เข้าไปในฟอร์ม

someone@example.com%0ACc:person2@example.com
%0ABcc:person3@example.com,person3@example.com,
anotherperson4@example.com,person5@example.com
%0ABTo:person6@example.com
เมื่อผู้ใช้ใส่ข้อมูลเหล่านี้ลงไปแล้วคลิ้ก submit ล่ะ อีเมล์จะถูกส่งไปตามอีเมล์เหล่านี้ทั้งหมด

การหยุดอัดส่งอีเมล์

ทางที่ดีที่สุดในการหยุดอัดส่งอีเมล์ คือ พิสูจน์ค่าที่ส่งมาจากฟอร์ม

โค้ดข้างล่างนี้ เหมือนกับโค้ดก่อน แต่จะเพิ่มส่วนที่พิสูจน์ค่าที่ส่งมาจากฟอร์ม ในฟิลด์อีเมล์

<html>
<body>
<?php
function spamcheck($field)
  {
//eregi() จะถือว่าตัวอักษรเล็กหรือใหญ่เหมือนกัน
  if(eregi("to:",$field) || eregi("cc:",$field)) 
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }
//if "email" is filled out, send email
if (isset($_REQUEST['email']))
  {
  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST['email']);
  if ($mailcheck==TRUE)
    {
    echo "Invalid input";
    }
  else
    { 
    //send email
    $email = $_REQUEST['email'] ; 
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
	
    mail("webmaster@domain.com", "Subject: $subject",
    $message, "From: $email" );
	
    echo "ขอบคุณที่ใช้ฟอร์มเมล์ของเรา";
    }
  }
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>



Related Sites
php.net
mysql
phpMyAdmin
apache
thainuke
microsoft
linux
PostgreSQL
Zend Technologies
CentOS

Special Thanks
ohohost.com
w3schools.com

Advertise




Power By



 
2279209







Copyright 2006 by Phpstreet. All Rights Reserved.