If you are seeing the Warning for 'Use of undefined REQUEST_URI' in your Apache server logs, don't worry. This can easily be fixed. Before jumping to the solution let's understand why PHP gives this warning.
Reason for undefined constant REQUEST_URI Warning:
This is more of a syntax warning, which PHP is allowing for now as PHP is an easy-going language when it comes to syntax. But with new versions of PHP, PHP is becoming a little more strict to deal with these silly coding mistakes.
You must be using the following code in your PHP file:
$_SERVER[REQUEST_URI]
This is generally used to get the URL for the current page, which includes just the directory structure and not the host name. For example, for the URL https://studytonight.com/forum the above code with return /forum as output.
You can use the following code in any PHP file to see the result:
echo $_SERVER[REQUEST_URI];
In the browser, you will get the path of the URI while in the logs, you will get the following log:
Warning: Use of undefined constant REQUEST_URI – assumed ‘REQUEST_URI’ (this will throw an Error in a future version of PHP)
Solution for undefined constant REQUEST_URI Warning:
The solution for this warning is very simple, all you have to do is include single or double quotes around the REQUEST_URI text, for example:
$_SERVER['REQUEST_URI']
That's it and it should solve your problem. Now, if you will open the PHP page and check the logs, you will not find any warning.
Why solve this undefined constant REQUEST_URI Warning?
You must be thinking if this is just a warning, why should you even bother solving it. Well, there are two reasons for solving this harmless warning:
-
As the log says, in the upcoming versions of PHP this may lead to errors. So you should solve this while you have time.
-
Your Webserver is printing unnecessary logs for these Warnings right now which time can be utilized in delivering your webpages. Hence if a large set of your web pages logs this error, then that is a lot of time and resource wastage for the webserver which can be saved.
Conclusion:
This is a trivial warning for now, but we recommend you to fix this before PHP release some new version in which this is reported as an error that may render your website unusable. We had this error in our website at a few places and we have started fixing it already.
You may also like: