ITPub博客

首页 > 应用开发 > IT综合 > PHP的GET和POST方法

PHP的GET和POST方法

原创 IT综合 作者:PHP大师 时间:2019-12-23 13:45:00 0 删除 编辑

GET方法

GET方法发送附加到页面请求的编码后的用户信息。页面和编码信息由  ? 字符。

http://www.test.com/index.htm?name1=value1&name2=value2
  • GET方法会在浏览器的“位置”框中生成一个长字符串,该字符串出现在您的服务器日志中。

  • GET方法仅限于发送多达1024个字符。

  • 如果您将密码或其他敏感信息发送到服务器,切勿使用GET方法。

  • GET不能用于将二进制数据(如图像或Word文档)发送到服务器。

  • GET方法发送的数据可以使用QUERY_STRING环境变量访问。

  • PHP提供  $ _GET 关联数组来使用GET方法访问所有发送的信息。

通过将源代码放入test.php脚本来尝试以下示例。

<?php   if( $_GET["name"] || $_GET["age"] ) {      echo "Welcome ". $_GET['name']. "<br />";      echo "You are ". $_GET['age']. " years old.";      exit();   }?><html>
   <body>
      <form action = "<?php **$_PHP_SELF** ? >" method = "GET">
         Name: <input type = "text" name = "name" />
         Age: <input type = "text" name = "age" />
         <input type = "submit" />
      </form>
   </body></html>

它会产生以下结果 :

POST方法

POST方法通过HTTP标头传输信息。该信息按照GET方法中的描述进行编码,并放入名为QUERY_STRING的标头中。

  • POST方法对要发送的数据大小没有任何限制。

  • POST方法可用于发送ASCII以及二进制数据。

  • 通过POST方法发送的数据通过HTTP标头,因此安全性取决于HTTP协议。通过使用安全HTTP,您可以确保您的信息安全。

  • PHP提供  $ _POST 关联数组来使用POST方法访问所有发送的信息。

通过将源代码放入test.php脚本来尝试以下示例。

<?php
 if( $_POST["name"] || $_POST["age"] ) {
    if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
       die ("invalid name and name should be alpha");
    }
    echo "Welcome ". $_POST['name']. "<br />";
    echo "You are ". $_POST['age']. " years old.";
    exit();
 }?><html> <body>    <form action = "<?php **$_PHP_SELF** ? >" method = "POST">       Name: <input type = "text" name = "name" />       Age: <input type = "text" name = "age" />       <input type = "submit" />    </form> </body></html>

它会产生以下结果:

形式

$ _REQUEST变量

PHP $ _REQUEST变量包含$ _GET,$ _POST和$ _COOKIE的内容。我们将在讨论cookie时讨论$ _COOKIE变量。

PHP $ _REQUEST变量可用于从GET和POST方法发送的表单数据中获取结果。

通过将源代码放入test.php脚本来尝试以下示例。

<?php
   if( $_REQUEST["name"] || $_REQUEST["age"] ) {
      echo "Welcome ". $_REQUEST['name']. "<br />";
      echo "You are ". $_REQUEST['age']. " years old.";
      exit();
   }?><html>   <body>      <form action = "<?php **$_PHP_SELF** ? >" method = "POST">         Name: <input type = "text" name = "name" />         Age: <input type = "text" name = "age" />         <input type = "submit" />      </form>   </body></html>


这里$ _PHP_SELF变量包含了被调用的自脚本的名字。

它会产生以下结果 :


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69957770/viewspace-2669965/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-12-19

  • 博文量
    8
  • 访问量
    4966