I am a beginner in php and am trying some very simple tests to get started.
I seem to be unable to get any values from $_GET.
This test.php
#!/usr/bin/php
<html>
<body><h1>GET test</h1><p>
<?php
print_r($_GET);
?>
</p></body></html>
produces the following when called with http://my.url/test.php?aValue=A&bValue=B
<html>
<body><h1>GET test</h1><p>
Array
(
)
</p></body></html>
I do not have write access to /etc/php.ini on the server, but check register_globals and it is off.
I have also tried using $_POST method, but this also doesn't work.
PHP version: PHP 5.1.6
The $_GET and $_POST variables are only available if track_vars is turned on.
As of PHP version 4.0.3 that is always automatically enabled.
Can you check your PHP version and also check the value of track_vars in php.ini?
It would also be helpful if you check phpinfo();
<?php
phpinfo();
?>
Check for
something called --enable-track-vars, which should be present.
_SERVER["argv"], should contain an array if you pass vars via a GET request.
also "Loaded Configuration File" should resolve to the file you think it is.
source: PHP: Description of core php.ini directives
Related
Is it possible to find out with php if the short start-flag <? does suffice in a script?
is there a ini-variable or do I need to program some function using output-buffering to see the results?
short_open_tag in php.ini turns <? on/off. However, <?=...?> always works in recent PHP versions.
You can retrieve its value using ini_get() - but you cannot change it using ini_set. You can set it using a .htaccess containing php_flag short_open_tag on though.
So you should never use <? for PHP blocks but <?php. For expressions <?= is fine if you don't need to support ancient PHP versions.
Yes, there is an ini variable, and it is called short_open_tags. So simply put ini_get with sort_open_tags as the parameter should return true if short tags is available on the server:
ini_get('short_open_tag')
I did some double check now:
<?php
check_configuration(); // on a blank linux install check for some config flags
function check_configuration(){
$test_short_open_tag=false;
?><? $test_short_open_tag=true; ?><?php
if(!ini_get('short_open_tag') or !$test_short_open_tag){
die( '<br>ERROR: please allow \'short_open_tag\' in php.ini or .htaccess to allow the use of "<?"<br><br>');
}
}
According to http://www.php.net/manual/en/reserved.variables.globals.php :
An associative array containing references to all variables which are
currently defined in the global scope of the script.
So, following code must display that $GLOBALS var has _SERVER, _ENV (if it is enabled in variables_order in php.ini) and _REQUEST keys:
var_dump($GLOBALS);
The result is:
Under nginx + php-fpm: missing _SERVER, _ENV, _REQUEST
Under cli: missing _ENV, _REQUEST
Hmm.. perhaps there is smth in docs about this behavior? I've looked through every page for each variable:
_SERVER: http://www.php.net/manual/en/reserved.variables.server.php
_ENV: http://www.php.net/manual/en/reserved.variables.request.php
_REQUEST: http://www.php.net/manual/en/reserved.variables.request.php
And i have found no mentions about such behaviour. Why it works like that?
I have installed php using debian package from http://www.dotdeb.org/ repo (nothing was compiled manually)... Currently running with nginx + php5-fpm.
Is that a php bug?
I've created a bug on php.net website, and php team answered: https://bugs.php.net/bug.php?id=65223
Summary:
This is not a bug. super-globals (aka. auto globals) are not added
to symbol tables by default for performance reasons unless the parser
sees need. i.e.
<?php $_SERVER; print_r($GLOBALS); ?>
will list it. You can also control this using auto_globals_jit in
php.ini:
http://www.php.net/manual/en/ini.core.php#ini.auto-globals-jit
Thanks php team so answer so fast!
I inherited an XP machine with xitami/pro server running on it and installed PHP 5.2.17 because I thought I might need the VC6 version.
PHP works and the phpinfo shows as it should. When I do www.domain.com/test.php?x=y&z=test the $_GET is not being populated.
The $_REQUEST variable is not being populated either. If I post it in a form and post it, the $_POST is empty as is the $_REQUEST.
If I loop through the $_SERVER variables and display them on a get, the QUERY_STRING is populated with the get variables.
When I do a print_r on any of the variables, it is empty. I get: Array ( ) 1
I then upgraded to PHP 5.4 and the same thing.
What is the problem? I am at a loss and don't know what else to try.
I would suspect this problem arises when the server is configured wrong. Especially when the wrong SAPI is used (for example, I'm pretty sure $_GET/$_POST are not available when using the PHP CLI.
To see if this causes your issue, create a new php file, and insert the following
<?php
echo php_sapi_name();
?>
In case this returns CLI I'm pretty sure that causes your issues. Solve it by configuring your server to use the correct SAPI.
TL;DR:
I assume you're using C:\php\php.exe as your PHP interpreter. Try C:\php\php-cgi.exe instead.
This is not your average session failed to start question, there is no whitespace, i have not called it in another file etc.
Im currently working on an application as I have started to build my session library, now when I call session_start I get the following error:
A session had already been started - ignoring session_start()
For those who wish to see the source: https://github.com/AdminSpot/ASFramework/blob/master/system/libraries/session.php
This usually means that the session.autostart directive is set to 1, but that's the thing.. it's not, it's set to 0 and I have verified this by doing the following:
Search my entire system for php.ini* files, checked them
Executed the following command php --ini amd validated the ini files
executed the following command php -i | grep session.auto_start. which responded with session.auto_start => Off => Off
Checked the PHPInfo page, see image below
Checked the php.ini files for cgi
There is no htaccess files on nginx
grep -lir "session_start" * only shows my library file
Restarting FastCGI, Nginx and the entire server
I have created a basic test script to test where i have just called session start on it's own.
The phpinfo() call stats the active php.ini is /etc/php5/cgi/php.ini so after running cat /etc/php5/cgi/php.ini | grep session.auto_start I get session.auto_start = 0, so it disabled, Could it be NGinx ?
Has anyone got any idea what's going on, some server information below:
PHP: PHP 5.3.5-1ubuntu7.2 with Suhosin-Patch
MySQL: Ver 14.14 Distrib 5.1.54, for debian-linux-gnu (i686) using readline 6.2
Nginx: Version: nginx/0.8.54
PHPInfo screen:
My first guess would be that you have an auto-prepend file or an .htaccess which is modifying the settings in the meanwhile.
You can use ini_get to retrieve the value of session.auto_start and auto_prepend_file to confirm. phpinfo() should work too.
Edit
Could it be that your session library is being instantiated twice? Since return $this->session_started is an instance variable, that could cause issues. What happens if you set that to a class-level variable?
Side note:
You also have this return $this->session_started = true; at the end of the start() method. It shouldn't matter, but it looks funny.
How about .htaccess containing a php_value session.auto_start 1? PHP on the command line would totally ignore settings overrides in .htaccess files. Remember that commandline PHP and web-based PHP have completely different .ini files in most standard configurations, so checking via command line is a waste of time.
I'd suggest having your script do a phpinfo() immediately before one of your session_start calls and check what the effective settings are there.
And anyway you can just verify if a session has already started or not.
if (!isset($_SESSION)) {
session_start();
}
http://php.net/manual/en/function.session-start.php#90007
Check and see if you are being passed a session cookie. May help you narrow it down.
I want to do the equivalent to the following line in file php.ini, but from PHP.
short_open_tag = On
Is it possible?
I tried this:
<?php
if (!ini_get('short_open_tag')) {
ini_set('short_open_tag', 'On');
}
$a = 1;
?>
<?=$a;?>
which outputs <?=$a;?>, so it's not working.
Yes, ini_set() is what you want.
An example:
if (!ini_get('short_open_tag')) {
ini_set('short_open_tag', 'on');
}
If you are using PHP 5.3, short_open_tag is no longer an option.
Description of core php.ini directives
Short tags have been deprecated as of PHP 5.3 and may be removed in PHP 6.0.
If you want to change it during a session and forget about it later, use ini_get() and ini_set(). If you want to actually modify php.ini programmatically, you can parse the ini file using parse_ini_file(), change your options and rewrite back to disk. See here for more.
Or you can write your own string replacement routine using the normal opening of a file, preg_replace(), etc.
Although you can use ini_set, be careful (quoted from the PHP documentation):
Not all the available options can be changed using ini_set(). There is a list of all available options in the appendix.
If you are changing options, like magic_quotes and short_open_tags, that's OK. But if you are going to change safe_mode, enable_dl, etc., the function will fail silently.
Many of the options specified above as examples are obsolete/removed security options in former versions of PHP. Consult the documentation if the behavior of ini_set is unexpected (e.g., does not work)
Please edit the php.ini file (just remove the ; and restart your Apache server):
Replace
;short_open_tag = On
with
short_open_tag = On
Now it will work.