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!

Jonaii
  • 67
  • 2
  • 12

2 Answers2

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'));
mpyw
  • 5,526
  • 4
  • 30
  • 36
1

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 "POST reqest".

This means, when you say http://www.example.com/tests/testtest.php?name=what, you send a GET request and not a POST 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 you use a POST request, you usually use the global $_POST array, instead of reading the request body manually through php://input.

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[]?

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Do you have any other method I can use to make it possible to send a data thru url parameter? – Jonaii Aug 20 '16 at 13:35