PHP知识点——2

来源:互联网 发布:范思哲男装高仿淘宝 编辑:程序博客网 时间:2024/05/20 21:43

表单处理:

写法:
<html>
<body>

<form action="welcome.php"method="post">
Name: <input type="text"name="fname">
Age: <input type="text"name="age">
<input type="submit">
</form>
</body>
</html>

 

Welcome.php文件:

<html>
<body>

Welcome <?php echo$_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"];?> years old.

</body>
</html>

 

注意:加了entype=”text/plain”后会收不到数据。

表单验证:
应该在任何可能的时候对用户输入进行验证(通过客户端脚本)。浏览器验证速度更快,并且可以减轻服务器的负载。

如果用户输入需要插入数据库,您应该考虑使用服务器验证。在服务器验证表单的一种好的方式是,把表单传给它自己,而不是跳转到不同的页面。这样用户就可以在同一张表单页面得到错误信息。用户也就更容易发现错误了。

<form method="post" action="<?php echohtmlspecialchars($_SERVER["PHP_SELF"]);?>">

$_SERVER["PHP_SELF"]是超级全局变量,返回当前正在执行脚本的文件名,会发送表单数据到当前页面,而不是跳转到不同的页面。

htmlspecialchars() 函数把一些预定义的字符转换为 HTML实体。

预定义的字符是:

·        &(和号)成为 &amp;

·        "(双引号)成为 &quot;

·        '(单引号)成为 &#039;

·        <(小于)成为 &lt;

·        >(大于)成为 &gt;

通过 htmlspecialchars()函数来避免被利用$_SERVER["PHP_SELF"]变量有可能会被黑客使用!当黑客使用跨网站脚本的HTTP链接来攻击时,$_SERVER["PHP_SELF"]服务器变量也会被植入脚本。原因就是跨网站脚本是附在执行文件的路径后面的,因此$_SERVER["PHP_SELF"]的字符串就会包含HTTP链接后面的JavaScript程序代码。 任何JavaScript代码可以添加在<script>标签中! 黑客可以利用这点重定向页面到另外一台服务器的页面上等。

htmlspecialchars() 把一些预定义的字符转换为 HTML实体。现在如果用户想利用 PHP_SELF变量, 结果将输出如下所示:

<formmethod="post" action="test_form.php/&quot;&gt;&lt;script&gt;alert('hacked')&lt;/script&gt;">

又如:

当我们使用htmlspecialchars()函数时,在用户尝试提交以下文本域:

<script>location.href('http://www.runoob.com')</script>

- 该代码将不会被执行,因为它会被保存为HTML转义代码,如下所示:

&lt;script&gt;location.href('http://www.runoob.com')&lt;/script&gt;

以上代码是安全的,可以正常在页面显示或者插入邮件中。

 

当用户提交表单时,我们将做以下两件事情,:

1.     使用 PHP trim()函数去除用户输入数据中不必要的字符 (如:空格,tab,换行)

2.     使用PHP stripslashes()函数去除用户输入数据中的反斜杠 (\)

<?php
// 定义变量并默认设置为空值
$name = $email = $gender = $comment = $website ="";

if ($_SERVER["REQUEST_METHOD"] =="POST")
{
  $name =test_input($_POST["name"]);
  $email =test_input($_POST["email"]);
  $website =test_input($_POST["website"]);
  $comment =test_input($_POST["comment"]);
  $gender =test_input($_POST["gender"]);
}

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

 

必需字段写法:

if (empty($_POST["name"]))  //使用php empty() 函数是否为空
    {$nameErr = "Name isrequired";}
  else
    {$name = test_input($_POST["name"]);}

 

Name:<input type="text" name="name">
<span class="error">* <?phpecho $nameErr;?></span>

结合正则表达式进行格式判定:

preg_match — 进行正则表达式匹配

<?php
// 定义变量并设为空值
$nameErr = $emailErr = $genderErr = $websiteErr= "";
$name = $email = $gender = $comment = $website ="";

if ($_SERVER["REQUEST_METHOD"] =="POST")
{
  if (empty($_POST["name"]))
    {$nameErr = "Name isrequired";}
  else
    {
    $name =test_input($_POST["name"]);
    // check if name onlycontains letters and whitespace
    if(!preg_match("/^[a-zA-Z ]*$/",$name))
      {
      $nameErr ="Only letters and white space allowed"; 
      }
    }

  if (empty($_POST["email"]))
    {$emailErr = "Email isrequired";}
  else
    {
    $email =test_input($_POST["email"]);
    // check if e-mail addresssyntax is valid
    if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
      {
      $emailErr ="Invalid email format"; 
      }
    }

  if (empty($_POST["website"]))
    {$website = "";}
  else
    {
    $website = test_input($_POST["website"]);
    // check if URL addresssyntax is valid (this regular expression also allows dashes in the URL)
    if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
      {
      $websiteErr ="Invalid URL"; 
      }
    }

  if (empty($_POST["comment"]))
    {$comment = "";}
  else
    {$comment =test_input($_POST["comment"]);}

  if (empty($_POST["gender"]))
    {$genderErr = "Gender isrequired";}
  else
    {$gender = test_input($_POST["gender"]);}
}
?>

 

综合实例,html加php写在同一个php文件即可。

 

$_GET:

预定义的 $_GET变量用于收集来自 method="get"的表单中的值。

从带有 GET方法的表单发送的信息,对任何人都是可见的(会显示在浏览器的地址栏),并且对发送信息的量也有限制。

HTML表单中使用 method="get"时,所有的变量名和值都会显示在 URL中。

http://www.w3cschool.cc/welcome.php?fname=Peter&age=37

注释:所以在发送密码或其他敏感信息时,不应该使用这个方法!

然而,正因为变量显示在 URL中,因此可以在收藏夹中收藏该页面。在某些情况下,这是很有用的。

注释:HTTP GET方法不适合大型的变量值。它的值是不能超过 2000个字符的。

 

$_POST:

预定义的 $_POST变量用于收集来自 method="post"的表单中的值。

从带有 POST方法的表单发送的信息,对任何人都是不可见的(不会显示在浏览器的地址栏),并且对发送信息的量也没有限制。然而,由于变量不显示在 URL中,所以无法把页面加入书签。

注释:然而,默认情况下,POST方法的发送信息的量最大值为 8 MB(可通过设置 php.ini文件中的 post_max_size进行更改)。

 

预定义的 $_REQUEST变量包含了 $_GET$_POST $_COOKIE 的内容。

$_REQUEST 变量可用来收集通过 GET POST 方法发送的表单数据。

 

0 0
原创粉丝点击