国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

PHP高級教程(8)-PHP安全的電子郵件

2020-03-24 18:00:15
字體:
來源:轉載
供稿:網友
首先,請看上一節中的 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'
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 以上代碼存在的問題是,未經授權的用戶可通過輸入表單在郵件頭部插入數據。假如用戶在表單中的輸入框內加入這些文本,會出現什么情況呢?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: 字段。當用戶點擊提交按鈕時,這封 e-mail 會被發送到上面所有的地址!PHP 防止 E-mail 注入防止 e-mail 注入的最好方法是對輸入進行驗證。下面的代碼與上一節類似,不過我們已經增加了檢測表單中 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'
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 在上面的代碼中,我們使用了 PHP 過濾器來對輸入進行驗證:FILTER_SANITIZE_EMAIL 從字符串中刪除電子郵件的非法字符
FILTER_VALIDATE_EMAIL 驗證電子郵件地址html教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大渡口区| 丹江口市| 定边县| 滦平县| 嘉义市| 凯里市| 河西区| 陕西省| 禄劝| 沁源县| 奎屯市| 沙洋县| 隆子县| 唐海县| 晋江市| 化州市| 渭南市| 夏河县| 利川市| 稷山县| 陆丰市| 巨鹿县| 英德市| 依兰县| 东兴市| 扎赉特旗| 石屏县| 马龙县| 龙陵县| 封开县| 修水县| 永嘉县| 临西县| 周口市| 萝北县| 千阳县| 天气| 稻城县| 博湖县| 游戏| 博野县|