我可以POST和GET到相同的PHP页面 [英] Can I POST and GET to the same PHP page

查看:103
本文介绍了我可以POST和GET到相同的PHP页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在同一个php页面上进行GET和POST操作,例如

我想发送数据到:

  http://www.example.com/my.php 

所以首先GET

  http://www.example.com/my .php?task = dosomething 

并POST一些 $ thexml = XML

  http://www.example.com/my.php?task=dosomething 

,然后就可以像一些代码一样访问这些代码(例如)

  //示例代码============================ 

if($ _ GET [task] ==dosomething){
$ mynewxml = $ _POST [$ thexml];
}
// ======================================= ===


解决方案

技术上不行,你不能 POST GET 。他们是两种不同的动词,你只能在你的请求中创建一个动词。



然而,你会发现如果你做一个 POST 并在URL中包含参数,例如 yourscript.php?param1 = somevalue& param2 = somevalue ,然后都是 $ _ GET 和 $ _ POST 将填入各自的数据。



这将是明智的阅读HTTP实际工作方式。 http://www.jmarshall.com/easy/http/



您应该考虑这是否是您的好系统设计。一个 GET 应该用于不改变服务器数据的请求。一个 POST 可以改变数据。尽管两者都可以实施,但最好遵循这一惯例。否则你永远不知道代理或其他程序将如何处理。


I wanted to know if it is possible to GET and POST on the same php page, for example

I want to send data to:

http://www.example.com/my.php

So first the GET

http://www.example.com/my.php?task=dosomething

and POST some $thexml = XML to

http://www.example.com/my.php?task=dosomething

and then be able to access both in some code like (example)

// Example Code ============================

    if($_GET["task"] == "dosomething"){
      $mynewxml = $_POST["$thexml"];
    }
//==========================================

解决方案

Technically no, you cannot POST and GET at the same time. They are two different verbs, and you only get to make one during your request.

However, you will find that if you do a POST and include parameters in the URL, such as yourscript.php?param1=somevalue&param2=somevalue, then both $_GET and $_POST will be populated with their respective data.

It would be wise to read up on how HTTP actually works. http://www.jmarshall.com/easy/http/

You should consider whether or not this is good system design on your part. A GET is supposed to be for requests that don't change data on the server. A POST can change data. Even though both can be implemented to do either, it is best to follow this common practice. You never know what some proxy or other program up the line will do with it otherwise.

这篇关于我可以POST和GET到相同的PHP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆