Illegal use of $_SERVER. You must use the request class to access input data.

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
jimc_hjones
Registered User
Posts: 9
Joined: Tue Dec 10, 2019 3:22 pm

Illegal use of $_SERVER. You must use the request class to access input data.

Post by jimc_hjones »

I have a script, I believe borrowed from hereabouts, on my site's home page which queries the phpbb for latest posts. I've just upgraded the forum from 3.2.x to 3.3.8 and it works fine except that my home page, not part of phpbb fails. It doesn't fail on the script, but further down where I query $_server for script name: $server_script = @$_SERVER['SCRIPT_NAME']; Presumably something in the forum code is deactivating the super globals because the error message as title also includes "This error message was generated by deactivated_super_global". Now I could just force the super globals active again, but presumably it would be better practice to "use the request class". So how do I do that? I've spent a day knocking round forums and php documentation, and I don't know if my brain cells have gone or something, but I'm damned if I can find anything that explains what the request class is in this context and how I set about using it to get the script name. Any pointers please?

thanks, Jim C
Last edited by HiFiKabin on Fri Nov 25, 2022 5:09 pm, edited 1 time in total.
Reason: Moved to Custom Coding
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Illegal use of $_SERVER. You must use the request class to access input data.

Post by Kailey »

jimc_hjones wrote: Fri Nov 25, 2022 5:07 pm $server_script = @$_SERVER['SCRIPT_NAME'];
Documentation for the request class is here. Try doing

Code: Select all

$server_script = htmlspecialchars_decode($request->server('SCRIPT_NAME'));
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
jimc_hjones
Registered User
Posts: 9
Joined: Tue Dec 10, 2019 3:22 pm

Re: Illegal use of $_SERVER. You must use the request class to access input data.

Post by jimc_hjones »

Kailey wrote: Fri Nov 25, 2022 5:28 pm
Thank you
User avatar
axe70
Registered User
Posts: 752
Joined: Sun Nov 17, 2002 10:55 am
Location: Italy
Name: Alessio
Contact:

Re: Illegal use of $_SERVER. You must use the request class to access input data.

Post by axe70 »

Can i do a question?
SInce $_SERVER['SCRIPT_NAME'] is server-side
why you use htmlspecialchars_decode?
which result can be expected?
Do not take me too serious
Anyway i do not like Discourse
User avatar
Gingko
Registered User
Posts: 25
Joined: Mon Aug 18, 2003 2:16 pm
Location: IDF, France
Contact:

Re: Illegal use of $_SERVER. You must use the request class to access input data.

Post by Gingko »

Kailey wrote: Fri Nov 25, 2022 5:28 pm
jimc_hjones wrote: Fri Nov 25, 2022 5:07 pm $server_script = @$_SERVER['SCRIPT_NAME'];
Documentation for the request class is here. Try doing

Code: Select all

$server_script = htmlspecialchars_decode($request->server('SCRIPT_NAME'));
Hello.

I have a similar problem (trying to upgrade from 3.2.2 to 3.3.10).

I am currently using the following trick inside the main config.php file, in order to select alternate databases according to the requested host name on the same server :

Code: Select all

$HTTP_HOST = $_SERVER['HTTP_HOST'];

if (strpos($HTTP_HOST, '-test') !== false) {
	$dbname = 'something';
} elseif (strpos($HTTP_HOST, '-old') !== false) {
	$dbname = 'something_else';
} else {
	$dbname = 'something_else_again';
}
This worked properly in 3.2.2 but not in 3.3.10.

Now it generates the following error message (as a plain text JSON data) when I try to access the https://asperansa-forum/install/app.php/convert page :

Code: Select all

{
	"errors": [
		{
			"title": "<b>General Error:</b><br>Illegal use of $_SERVER. You must use the request class to access input data. Found in /srv/www/asperansa/forum/config.php on line 7. This error message was generated by deactivated_super_global.<br> in file /srv/www/asperansa/forum/phpbb/request/deactivated_super_global.php on line 67<br><br>"
		}
	],
	"over": true
}
Reading the request class documentation (https://area51.phpbb.com/docs/dev/maste ... quest.html) doesn't help me very much :

This explains how to USE the request class, but this doesn't tell me how to GET it.

As request is a class, it needs instantiation.

As the super_global $_SERVER is already no longer available at this time, it means that the class is already instantiated, so I don't think I could use
$variable = new request;
in order to use it.

I tried to use
$request->server('HTTP_HOST');
but it didn't work.

I suppose I have to write something like this :
<something_unknown>->server('HTTP_HOST');

What should I write instead of <something_unknown> ?
Gingko
User avatar
Mike-on-Tour
Registered User
Posts: 462
Joined: Wed Jan 15, 2020 3:51 pm
Location: Germany
Name: Michael
Contact:

Re: Illegal use of $_SERVER. You must use the request class to access input data.

Post by Mike-on-Tour »

Have you tried to use

Code: Select all

global $request;
to get the request class?
Watz fo lunch?
If you like my extensions or my support please consider a donation: Image
User avatar
Gingko
Registered User
Posts: 25
Joined: Mon Aug 18, 2003 2:16 pm
Location: IDF, France
Contact:

Re: Illegal use of $_SERVER. You must use the request class to access input data.

Post by Gingko »

Mike-on-Tour wrote: Sat Apr 01, 2023 2:39 pm Have you tried to use

Code: Select all

global $request;
to get the request class?
Excellent idea, thank you very much.

As it looked like global, I haven't thought that config.php could have been included wrapped inside a function.

So my script becomes now:

Code: Select all

if (is_array($_SERVER)) {
	$http_host = $_SERVER['HTTP_HOST'] ?? '';
} else {
	global $request;

	$http_host = $request->server('HTTP_HOST');
}
if (empty($http_host)) {
	die('No way to access $_SERVER superglobal');
}

if (strpos($http_host, '-test') !== false) {
	$dbname = 'something';
} elseif (strpos($http_host, '-old') !== false) {
	$dbname = 'something_else';
} else {
	$dbname = 'something_else_again';
}
Gingko
User avatar
Mick
Support Team Member
Support Team Member
Posts: 26505
Joined: Fri Aug 29, 2008 9:49 am

Re: Illegal use of $_SERVER. You must use the request class to access input data.

Post by Mick »

What method are you using for your upgrade?
  • "The more connected we get the more alone we become" - Kyle Broflovski©
  • "The good news is hell is just the product of a morbid human imagination.
    The bad news is, whatever humans can imagine, they can usually create.
    " - Harmony Cobel
User avatar
Gingko
Registered User
Posts: 25
Joined: Mon Aug 18, 2003 2:16 pm
Location: IDF, France
Contact:

Re: Illegal use of $_SERVER. You must use the request class to access input data.

Post by Gingko »

Mick wrote: Sat Apr 01, 2023 5:50 pm What method are you using for your upgrade?
I upgrade by manually replacing files (after applying my own patches), and I use the /install interface for upgrading the database.

Now on a test server with a copy of the forum and the database, until completely tested.

But it will likely works, now.
Gingko
Post Reply

Return to “phpBB Custom Coding”