表單驗證

表單驗證

<!DOCTYPE HTML> 
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 

<?php
// 定義變量並默認設置爲空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    if (empty($_POST["name"]))
    {
        $nameErr = "名字是必需的";
    }
    else
    {
        $name = test_input($_POST["name"]);
        // 檢測名字是否只包含字母跟空格
        if (!preg_match("/^[a-zA-Z ]*$/",$name))
        {
            $nameErr = "只容許字母和空格"; 
        }
    }
    
    if (empty($_POST["email"]))
    {
    $emailErr = "郵箱是必需的";
    }
    else
    {
        $email = test_input($_POST["email"]);
        // 檢測郵箱是否合法
        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
        {
            $emailErr = "非法郵箱格式"; 
        }
    }
    
    if (empty($_POST["website"]))
    {
        $website = "";
    }
    else
    {
        $website = test_input($_POST["website"]);
        // 檢測 URL 地址是否合法
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
        {
            $websiteErr = "非法的 URL 的地址"; 
        }
    }
    
    if (empty($_POST["comment"]))
    {
        $comment = "";
    }
    else
    {
        $comment = test_input($_POST["comment"]);
    }
    
    if (empty($_POST["gender"]))
    {
        $genderErr = "性別是必需的";
    }
    else
    {
        $gender = test_input($_POST["gender"]);
    }
}

function test_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>

<h2>PHP 表單驗證明例</h2>
<p><span class="error">* 必需字段。</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
名字: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
網址: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
備註: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
<br><br>
性別:
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?>  value="female">女
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?>  value="male">男
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit"> 
</form>

<?php
echo "<h2>您輸入的內容是:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>

</body>
</html>

首先讓咱們先看看純HTML的表單代碼分析:php

文本字段

"名字", "E-mail", 及"網址"字段爲文本輸入元素,"備註"字段是 textarea。html

單選按鈕

"性別"字段是單選按鈕web

表單元素

HTML 表單代碼以下所示:瀏覽器

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  • 該表單使用 method="post" 方法來提交數據。 那麼什麼是 $_SERVER["PHP_SELF"] 變量?

$_SERVER["PHP_SELF"]是超級全局變量,返回當前正在執行腳本的文件名,與 document root相關。 因此, $_SERVER["PHP_SELF"] 會發送表單數據到當前頁面,而不是跳轉到不一樣的頁面。服務器

  • 什麼是 htmlspecialchars()方法?

htmlspecialchars() 函數把一些預約義的字符轉換爲 HTML 實體。函數

預約義的字符是:
& (和號) 成爲 &amp;
" (雙引號) 成爲 &quot;
' (單引號) 成爲 &#039;
< (小於) 成爲 &lt;
> (大於) 成爲 &gt;

PHP表單中需引發注重的地方:

  • XSS又叫 CSS (Cross-Site Script) ,跨站腳本攻擊。惡意攻擊者往Web頁面裏插入惡意html代碼,當用戶瀏覽該頁之時,嵌入其中Web裏面的html代碼會被執行,從而達到惡意用戶的特殊目的。

考慮到用戶會在瀏覽器地址欄中輸入如下地址:http://www.runoob.com/test_form.php/"><script>alert('hacked')</script> 以上的 URL 中,將被解析爲以下代碼並執行:post

<form method="post" action="test_form.php/"><script>alert('hacked')</script>

請注意, 任何JavaScript代碼能夠添加在<script>標籤中! 黑客能夠利用這點重定向頁面到另一臺服務器的頁面上,頁面 代碼文件中能夠保護惡意代碼,代碼能夠修改全局變量或者獲取用戶的表單數據。spa

  • 能夠經過 htmlspecialchars() 函數來避免被利用。 htmlspecialchars() 把一些預約義的字符轉換爲 HTML 實體。如今若是用戶想利用 PHP_SELF 變量, 結果將輸出以下所示:
<form method="post" action="test_form.php/&quot;&gt;&lt;script&gt;alert('hacked')&lt;/script&gt;">

嘗試該漏洞失敗!code

使用 PHP 驗證表單數據

當用戶提交表單時,咱們將作如下兩件事情,:orm

  1. 使用 PHP trim() 函數去除用戶輸入數據中沒必要要的字符 (如:空格,tab,換行)。
  2. 使用PHP stripslashes()函數去除用戶輸入數據中的反斜槓 ( \ ).
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息