1

I know this question has been already asked in the forum for several times but neither any of the answer worked for me. I still get empty string every time I try to send a data via a URL parameter.

This is the PHP code

            <?php   
                    if (!file_get_contents("data:,ok")) {
                              die("Houston, we have a stream wrapper problem.");
                         }
                    print "CONTENT_TYPE: " . $_SERVER['CONTENT_TYPE'] . "<BR />";
                    $data = file_get_contents('php://input');
                    print "DATA: <pre>";
                    var_dump($data);
                    $tempArray = json_decode(file_get_contents('generated.json'), true);
                    array_push($tempArray, $data);
                    $jsonData = json_encode($tempArray);
                    file_put_contents('generated.json', $jsonData);
                    print "</pre>"; 
            ?>
            <form method="post">
                <input type="text" name="name" value="ok" />
                <input type="submit" name="submit" value="submit"/> 
            </form>

Example of passing a variable using url parameter

http://localhost/tests/testtest.php?name=what

Output:

            Notice: Undefined index: CONTENT_TYPE in C:\Apache24\htdocs\tests\testtest.php on line 5
            CONTENT_TYPE:
            DATA: 

I have already set allow_url_fopen = On, set the post_max_size = 8M and still no hope. However when I try to send a data by click the submit button it send a raw data to the php (string(21) "name=ok&submit=submit").

Anyone care to help? Thanks!

1

2 Answers 2

2

This is described in php://input

php://input

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives.

The important part is "request body".

This means, when you say http://www.example.com/tests/testtest.php?name=what, you send data in the URL parameters and not the body of the request. Therefore, there is no request body, and you cannot read anything through php://input.


To read form input passed as URL parameters, you may use the global $_GET variable.

When your request includes a body in form-url-encoded format, you usually use the global $_POST array, instead of reading the request body manually through php://input. php://input can be useful if you need to receive other data formats in the request body, such as JSON.

You may also consider $_REQUEST, if you don't care if it is a POST or GET request. Although, be aware of What's wrong with using $_REQUEST[]?

Sign up to request clarification or add additional context in comments.

1 Comment

Do you have any other method I can use to make it possible to send a data thru url parameter?
2

Are you looking for $_SERVER['QUERY_STRING']?

<?php

// Basic way, but has the risk of E_NOTICE when it is undefined.
var_dump($_SERVER['QUERY_STRING']);

// Safe way
var_dump((string)filter_input(INPUT_SERVER, 'QUERY_STRING'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.