PHP安全的電子郵件-3.11PHP高級(jí)教程

在上一節(jié)中的 PHP e-mail 腳本中,存在著一個(gè)漏洞。

創(chuàng)新互聯(lián)-云計(jì)算及IDC服務(wù)提供商,涵蓋公有云、IDC機(jī)房租用、成都溫江機(jī)房、等保安全、私有云建設(shè)等企業(yè)級(jí)互聯(lián)網(wǎng)基礎(chǔ)服務(wù),咨詢熱線:18980820575

PHP E-mail 注入

首先,請(qǐng)看上一節(jié)中的 PHP 代碼:

<html><body><?phpif (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>

以上代碼存在的問題是,未經(jīng)授權(quán)的用戶可通過輸入表單在郵件頭部插入數(shù)據(jù)。

假如用戶在表單中的輸入框內(nèi)加入這些文本,會(huì)出現(xiàn)什么情況呢?

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() 函數(shù)把上面的文本放入郵件頭部,那么現(xiàn)在頭部有了額外的 Cc:, Bcc: 以及 To: 字段。當(dāng)用戶點(diǎn)擊提交按鈕時(shí),這封 e-mail 會(huì)被發(fā)送到上面所有的地址!

PHP 防止 E-mail 注入

防止 e-mail 注入的最好方法是對(duì)輸入進(jìn)行驗(yàn)證。

下面的代碼與上一節(jié)類似,不過我們已經(jīng)增加了檢測(cè)表單中 email 字段的輸入驗(yàn)證程序:

<html><body><?phpfunction 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 過濾器來對(duì)輸入進(jìn)行驗(yàn)證:

FILTER_SANITIZE_EMAIL 從字符串中刪除電子郵件的非法字符FILTER_VALIDATE_EMAIL 驗(yàn)證電子郵件地址

您可以在我們的 PHP 過濾器這一節(jié)中閱讀更多有關(guān)過濾器的內(nèi)容。

網(wǎng)站題目:PHP安全的電子郵件-3.11PHP高級(jí)教程
網(wǎng)頁地址:http://www.muchs.cn/article6/dghhoig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)建站ChatGPT、營(yíng)銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化