XAMPP+PHP7.0.9虚拟环境下Superglobals全部为NULL的问题求助
Hey there, let's work through why all your PHP superglobals are returning NULL—this almost always ties back to a configuration misstep in your Apache virtual host or PHP settings, so let's dive into the fixes step by step:
1. Fix the Critical Syntax Error in Your Virtual Host Config
First off, I spotted a glaring typo in your VirtualHost block: the DocumentRoot line is missing a closing double quote. Right now it looks like this:
DocumentRoot "d:/php/test
This breaks Apache's configuration parsing entirely, which prevents PHP from picking up any server environment variables at all. Correct it to:
DocumentRoot "d:/php/test"
After fixing this, restart your Apache server through the XAMPP control panel—this is non-negotiable for config changes to take effect.
2. Correct the Directory Block in Your Virtual Host
Your current <Directory /> targets the root filesystem, which is unnecessary and can cause permission-related issues that mess with superglobal population. Instead, point it directly to your site's document root:
<Directory "d:/php/test"> Order allow,deny Allow from all Require all granted </Directory>
This ensures Apache properly handles requests to your test site, which is a prerequisite for PHP to populate superglobals correctly.
3. Verify PHP's variables_order Setting
PHP uses the variables_order directive in php.ini to decide which superglobals to populate. If this is misconfigured, variables like $_SERVER won't get filled at all.
- Open your
php.inifile (you can find its exact location by running a simplephpinfo()test script) - Look for the line:
variables_order = "GPCS"- Make sure it includes
S(for Server variables) and isn't set to an empty string or a value missing key letters
- Make sure it includes
- Save the file and restart Apache again
4. Confirm Your Virtual Host & Hosts File Are Working
Double-check that your request is actually routing through the virtual host you set up:
- Run
ping test.comin Command Prompt—you should see it resolve to127.0.0.1 - Clear your browser cache and visit
http://test.com(notlocalhostor the raw IP address) to ensure you're hitting the correct virtual host
5. Test with a Basic PHP Info Script
Create a file named phpinfo.php in your d:/php/test directory with this code:
<?php phpinfo(); ?>
Visit http://test.com/phpinfo.php and scroll down to the $_SERVER section. If these variables are still NULL here, the issue is definitely server-side. If they show up correctly, then your original code might be accidentally unsetting or overwriting the superglobals (check for lines like unset($_SERVER); or accidental variable name collisions).
Start with the first fix—the missing quote in DocumentRoot is the most likely culprit here. Let me know if any of these steps get your superglobals working again!
内容的提问来源于stack exchange,提问作者Ivaylo Cheresharov

