Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I POST and GET to the same PHP page

Tags:

post

php

xml

get

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"];
    }
//==========================================
like image 512
gringoLoco007 Avatar asked Jul 11 '11 14:07

gringoLoco007


1 Answers

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.

like image 160
Brad Avatar answered Sep 23 '22 05:09

Brad