3

I want to know how to use POST request in PHP. I used $_REQUEST['text'] for getting data from url like http://localhost/data.php?text=ABCDEFGH but If i pass very long text than ERROR : Request-URI Too Long.

if(isset($_REQUEST['text'])){
     $parsetext=$_REQUEST['text'];    //get data here data > ABCDEFGH
}else{
     echo "not valid";
}

Please any one tell me how to support long TEXT using POST request. I know that $_REQUEST is for both request GET & POST.

Gunjan Patel
  • 2,342
  • 4
  • 24
  • 45

2 Answers2

3

Regarding the error, you can check these links (I assume you've already seen this):

How do I resolve a HTTP 414 “Request URI too long” error?

Request-URI Too Large

And for your question: I want to know how to use POST request in PHP.

  1. Create a form.

(I assume that the textbox from this form will get the long data that you want to POST).

<form method="POST" action="http://localhost/data.php">
  <input type="text" name="input_text" />
  <button type="submit">Submit</button>
</form>
  1. Receive the data from the from input using the defined method on your form. In this case the method is POST and the url/file that will receive the submitted data is http://localhost/data.php.

    if (isset($_POST['input_text'])) {
        // Where input_text is the name of your textbox.
        $text = $_POST['input_text'];
        echo $text;
    }
    

ERROR : Request-URI Too Long.

$_REQUEST, as you say, handles $_POST and $_GET methods is correct. Regarding your question, even though you use $_REQUEST to get the data, in the background it use the $_GET method to catch the query string you pass with the url.

$_GET method has limit on size and this is the main reason why you encounter that error. Whereas $_POST method don't have limit: Is there a maximum size for content of an HTTP POST?.

Conclusion: Better not use $_REQUEST, use $_GET or $_POST specifically :D

Community
  • 1
  • 1
Vainglory07
  • 5,073
  • 10
  • 43
  • 77
1

First of all, read this Question/Answer, this will probably clear some things for you on the differences between POST and GET and what method you should use for your project.

Then, you should forget about the $_REQUEST and use either $_GET or $_POST. This will prevent some security issues that you'll probably run into if you keep using $_REQUEST. More on that in the PHP Manual

Next up, you should definitely switch to POST, instead of GET if you're passing large sets of data. Otherwise you have to modify your apache config and that is not recommended if you plan on releasing you code to the public.

-EDIT START-

You can even use POST within AJAX, if everything is on the same server.

-EDIT END-

Community
  • 1
  • 1
BrainInBlack
  • 139
  • 10