$_GET and $_POST variables do not exist when using lighttpd web server
Asked Answered
T

3

7

I installed lighttpd web server on my Windows computer and I have a problem: in PHP files no $_GET and $_POST variables are defined.

For example I have this simple script (tmp.php):

<?php

  echo "x: '" . $_GET ['x'] . "'<br />";

?>

When I go to the address: http://localhost/tmp.php?x=123

I get this error message:

Notice: Undefined index: x in /srv/www/htdocs/tmp.php on line 3 x: ''

Whilst when I put the same file on public hosting I get:

x: '123'

Also the php command:

empty ($_GET)

returns true.

The same is for all $_POST variables.

Is there any misconfiguration in my php.ini file?

The command:

print_r($_SERVER); 

gives the following result:

Array (
 [SERVER_SOFTWARE] => lighttpd/1.4.20
 [SERVER_NAME] => localhost
 [GATEWAY_INTERFACE] => CGI/1.1
 [SERVER_PROTOCOL] => HTTP/1.1
 [SERVER_PORT] => 80
 [SERVER_ADDR] => 0.0.0.0
 [REQUEST_METHOD] => GET
 [REDIRECT_STATUS] => 200
 [QUERY_STRING] => x=123
 [REQUEST_URI] => /tmp.php?x=123
 [REMOTE_ADDR] => 127.0.0.1
 [REMOTE_PORT] => 3150
 [CONTENT_LENGTH] => 0
 [SCRIPT_FILENAME] => /srv/www/htdocs/tmp.php
 [SCRIPT_NAME] => /srv/www/htdocs/tmp.php
 [DOCUMENT_ROOT] =>
 [SYSTEMROOT] => C:\WINNT
 [HTTP_ACCEPT] => */*
 [HTTP_ACCEPT_LANGUAGE] => en-gb
 [HTTP_USER_AGENT] => Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.1)
 [HTTP_ACCEPT_ENCODING] => gzip, deflate
 [HTTP_HOST] => localhost
 [HTTP_CONNECTION] => Keep-Alive
 [WINDIR] => C:\WINNT
 [PHP_SELF] => /srv/www/htdocs/tmp.php
 [PATH_TRANSLATED] => /srv/www/htdocs/tmp.php
 [REQUEST_TIME] => 1328287189
 [argv] => Array (
 [0] => /srv/www/htdocs/tmp.php )
 [argc] => 1 
)

So the value x=123 exists in $_SERVER['QUERY_STRING'] and in $_SERVER['REQUEST_URI'], but I don't know how to get it.

Tracy answered 3/2, 2012 at 16:23 Comment(13)
The notice says the undefined index is 'a', not 'x'. Are you sure the code is the same?Expurgatory
Yes, it is x or any other variable that you define. $_GET and $_POST arrays do not exist at all.Tracy
Is it correctly set up? What does print_r($_SERVER); say for GET requests?Gaynell
Maybe try $_REQUEST, $_P, $_G .. Also try a var_dump($_SERVER); to see if the query string appears among the variablesMeilhac
Sorry, I couldn't post it here, so please see my original post for print_r($_SERVER) output. It does show x=123, but I don't know how to get it.Tracy
When I put in the code: echo $_SERVER ['QUERY_STRING']; I get the output: x=123. Should I now change all my PHP scripts or there is a method to somehow translate $_SERVER ['QUERY_STRING'] to $_GET?Tracy
@Gaynell No one's provided an answer yet...Schizophrenia
@AndreyRubliov Are you using CGI or FastCGI between lighttpd and PHP? What does your lighttpd.conf file look like?Schizophrenia
In my opinion this should be on serverfault.Banneret
@mario: I hope this wasn't serious: this can be a server misconfiguration problem, so this should be solved first, the OP shouldn't use such "hacks".Obel
@AndreyRubliov: you should share more information about your server's configuration, PHP version, etc... David Souther's previous question is also good. This must be a server misconfiguration.Obel
What is in var_dump($_REQUEST)?Dimissory
in case nothing else helps: getluky.net/2009/02/24/…Obel
W
2

Firstly lets make sure we're not missing something.

Try this

<?php
    // test1.php
    print('GET: '); // First missing semicolon
    print_r($_GET);
    print('HTTP_GET_VARS: '); // Second missing semicolon
    print_r($HTTP_GET_VARS);
?>

Secondly try $_POST

Make a simple html form and try to post it.

<?php
    // test2.php
    print_r($_POST);
?>
<form method="POST">
    <input type="text" name="myvar" />
    <input type="submit""/>
</form>

I know these sound silly, but please try them so we can rule them out.

Finally are you using rewrite rules?

Take a look in lighttpd.conf for any mention of 'url.rewrite'. Lighttpd doesn't try to guess the URL like apache, you have to specifically tell it to include the query string if your using rewrites. For example (The important bits are in bold - add them to your rewrites).

url.rewrite = ( "^/([a-z]+)/([a-z]+)(?*)(.*)" => "/index.php?controller=$1&action=$2&**$4**" )

Waggoner answered 13/6, 2012 at 10:3 Comment(2)
I'm not using rewrite and I've got empty $_GET. The first test you suggest give me no output.Tiddlywinks
Once I add the missing semi colons I get: GET: Array ( ) HTTP_GET_VARS:Tiddlywinks
E
1

According to the docs, this could be caused by the php.ini setting variables_order.

The default value is EGPCS, but if it was changed somehow and did not contain the letter G, GET variables would be completely ignored and $_GET would be empty.

Expurgatory answered 3/2, 2012 at 17:36 Comment(2)
Thanks, but in my php.ini it is set to: variables_order = "EGPFCS" so seems OK?Tracy
I can't find any documentation for the letter F, but in my test environment EGPFCS still populates $_GET, so I imagine that's not your problem.Expurgatory
W
0

Well as far I see content length is zero, it shouldn't be "zero". Install fiddler and check for the header send during request and check for response header. It seems that there should be header messing around..

Regards

Workable answered 18/6, 2012 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.