Issue with super global $_server

Discussion forum for Extension Writers regarding Extension Development.
S33K3R
Registered User
Posts: 1
Joined: Tue Jan 20, 2015 11:02 pm

Issue with super global $_server

Post by S33K3R »

Hey guys,

I wanted to write a pretty simple browser recognition in php. It actually should show different add sizes depending on mobile / or not.

Code: Select all

<?php	function isMobile() 
		{
			return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
		}
		
		if(isMobile())
			{
				echo "mobile";
			}

		else
			{
				echo	"desktop";
			}
?>
That was the actual code. The two echos mobile and desktop are just placeholders for the add code. The script works but needs to be alternated to work without the simple use of $_server to run as an include in the forum.

We tried so far:

Code: Select all

request_var(('HTTP_USER_AGENT', '', false, \phpbb\request\request_interface::SERVER)

Code: Select all

$request->server('HTTP_USER_AGENT', '')

Code: Select all

$request->request('HTTP_USER_AGENT', '', false, \phpbb\request\request_interface::SERVER)
None of the above changes to the $_SERVER["HTTP_USER_AGENT"] part did work. Even enabling the access to super globals and using the original code did not work.


I really need some help to understand where we made the mistake. A working version of my code would obviously be appreciated as well ;).

Thx in advance
Last edited by Oyabun1 on Tue Jan 20, 2015 11:31 pm, edited 1 time in total.
Reason: Moved from 3.1.x Support forum
User avatar
T0ny
Registered User
Posts: 1383
Joined: Sun Jan 29, 2006 8:42 pm
Location: Lancashire
Name: Tony

Re: Issue with super global $_server

Post by T0ny »

To get the HTTP_USER_AGENT use:

Code: Select all

$request->variable('HTTP_USER_AGENT', '', false, \phpbb\request\request_interface::SERVER);
or the shortcut method

Code: Select all

$request->server('HTTP_USER_AGENT', '');
Note that if you're doing this within a function you will
need to bring the $request object into scope with global $request;
AlienWorkshop
Registered User
Posts: 5
Joined: Thu Jan 22, 2015 8:53 am

Re: Issue with super global $_server

Post by AlienWorkshop »

HI T0ny,

thanks for your reply. I am in the same Team with S33k3r,

as you can see from his post we already tried to use

Code: Select all

$request->server('HTTP_USER_AGENT', '');

we tested with all these funktions on his post outside of all functions and also decleared the $request with a new before we try to use it.

The Problem is that in all cases we geht the "Illegal use of $_SERVER" error message so i assume our code is correct and working and we simply dont understand why we get this message, with all of this valid (from the Definition) methods

we also tried to unlock the super_globals with the enable_super_globals() which did not do the trick as well

Could this be related to phpbb3.1 code itself or do we have to alter something in the php.conf ?
AlienWorkshop
Registered User
Posts: 5
Joined: Thu Jan 22, 2015 8:53 am

Re: Issue with super global $_server

Post by AlienWorkshop »

Noone? :-(
User avatar
Wolfsblvt
Registered User
Posts: 634
Joined: Sun Oct 26, 2014 9:12 pm
Location: Solingen, Germany
Contact:

Re: Issue with super global $_server

Post by Wolfsblvt »

AlienWorkshop wrote:Noone? :-(
It would really help if you would post the complete code where you have added this function.
If you have a specific extension request and you are willing to pay for, you can write me a PM.
My extensions (Trending: @Mention SystemAdvanced PollsUser Online Time)

»Du kamst zu uns. Deine Stimme kam. Du zeigtest uns die Sterne. Sie funkelten. Wir konnten sehen.«
User avatar
RMcGirr83
Recognised Extension Developer
Posts: 21337
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: Issue with super global $_server

Post by RMcGirr83 »

$request->header('User-Agent') :?:

Not sure if that will work though as it looks to just revert back to $request->server
Appreciate the extensions/mods/support then buy me a beerImage
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
AlienWorkshop
Registered User
Posts: 5
Joined: Thu Jan 22, 2015 8:53 am

Re: Issue with super global $_server

Post by AlienWorkshop »

@Wolfsblvt

the code can be seen on the first post form S33K3ER but here it is again.

Code: Select all

<?php   function isMobile() 
      {
         return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
      }
      
      if(isMobile())
         {
            echo "mobile";
         }

      else
         {
            echo   "desktop";
         }
?>
insead of

Code: Select all

$_SERVER["HTTP_USER_AGENT"] 
we also tried to gather the Information via

Code: Select all

request_var(('HTTP_USER_AGENT', '', false, \phpbb\request\request_interface::SERVER)

Code: Select all

$request->server('HTTP_USER_AGENT', '')

Code: Select all

$request->request('HTTP_USER_AGENT', '', false, \phpbb\request\request_interface::SERVER)
all throws the same error message that the superglobal $Server cannot be accessed because ist prohibited


@RMcGirr83
Thanks i ll give it a try.
AlienWorkshop
Registered User
Posts: 5
Joined: Thu Jan 22, 2015 8:53 am

Re: Issue with super global $_server

Post by AlienWorkshop »

RMcGirr83 wrote:$request->header('User-Agent') :?:

Not sure if that will work though as it looks to just revert back to $request->server
Tested this but this gives me an

Code: Select all

Warning: Cannot modify header information - headers already sent by .... 
also it throws the

Code: Select all

Illegal use of $_SERVER. You must use the request class or request_var() to access input data. Found in /home/jvjxygfi/soc/test_soc_forum/phpbb/request/request.php on line 389. This error message was generated by deactivated_super_global.
this is the message we get with all variants we trying..
User avatar
RMcGirr83
Recognised Extension Developer
Posts: 21337
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: Issue with super global $_server

Post by RMcGirr83 »

T0ny wrote: Note that if you're doing this within a function you will
need to bring the $request object into scope with global $request;
Appreciate the extensions/mods/support then buy me a beerImage
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
AlienWorkshop
Registered User
Posts: 5
Joined: Thu Jan 22, 2015 8:53 am

Re: Issue with super global $_server

Post by AlienWorkshop »

We are not using a Funktion

we are including a php file

the included file is only .. at the Moment

Code: Select all

$request = new \phpbb\request\request();
echo $request->header('User-Agent');
User avatar
MuhClaren
Registered User
Posts: 126
Joined: Sat Feb 21, 2015 5:23 am

Re: Issue with super global $_server

Post by MuhClaren »

user_agent.php:

Code: Select all

<?php
/*******
 * 	Get User Agent
 */

function getUA() {

	global $request;

	$request = $request -> server('HTTP_USER_AGENT');

	return $request;
}

$useragent = getUA();

echo $useragent;
?>
Put it in your root phpbb directory.

Call it with the template tag for PHP:

Code: Select all

<!-- INCLUDEPHP user_agent.php -->
It'll parse into something like:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0
Modify the function to suit your needs.

Working for PHPBB v3.1.3
JuergenF
Registered User
Posts: 22
Joined: Mon Mar 16, 2009 8:57 am
Location: Germany
Name: Juergen

Re: Issue with super global $_server

Post by JuergenF »

I just tried this function from MuhClaren but the code throws the following error:
Fatal error: Call to a member function server() on a non-object in /www/htdocs/w00xxxx/poxxx/forum/server.php on line 8

Code: Select all

<?php
/*******************
 *    Get User Agent
 *******************/
function getUA() {
global $request;
$request = $request -> server('HTTP_USER_AGENT');
return $request;
}
$useragent = getUA();
echo $useragent;
?>
Line 8 is: $request = $request -> server('HTTP_USER_AGENT');
Any idea? Thanks
Juergen

(I have the same issue with $_SERVER)
User avatar
MuhClaren
Registered User
Posts: 126
Joined: Sat Feb 21, 2015 5:23 am

Re: Issue with super global $_server

Post by MuhClaren »

JuergenF wrote:I just tried this function from MuhClaren but the code throws the following error:
Fatal error: Call to a member function server() on a non-object in /www/htdocs/w00xxxx/poxxx/forum/server.php on line 8

Code: Select all

<?php
/*******************
 *    Get User Agent
 *******************/
function getUA() {
global $request;
$request = $request -> server('HTTP_USER_AGENT');
return $request;
}
$useragent = getUA();
echo $useragent;
?>
Line 8 is: $request = $request -> server('HTTP_USER_AGENT');
Any idea? Thanks
Juergen

(I have the same issue with $_SERVER)
That's just an example for the OP using it in a stand alone page, not from an extension. To get it from inside your extension, you need to bring the request object into scope.

In your extension, do this by:
1. Adding the argument to your services.yml file:

Code: Select all

services:
  your_vendor_name.your_extension_name.functions:
    class: your_vendor_name\your_extension_name\core\functions
    arguments:
      - @request
2. Add it to your class:

Code: Select all

class functions
{
	/** @var \phpbb\request\request */
	protected $request;
	
	/**
	 * Constructor
	 *
	 * @param \phpbb\request\request   $request     the request object
	 */
	public function __construct(
		$request,
	) {
		$this->request  = $request;
	}
Then you can work with the request object in a proper object-oriented way. Calling it from a member of the same class, use $this, such as:

Code: Select all

$user_agent = $this->request->server('HTTP_USER_AGENT');
User avatar
MuhClaren
Registered User
Posts: 126
Joined: Sat Feb 21, 2015 5:23 am

Re: Issue with super global $_server

Post by MuhClaren »

My above answer assumes you're using it in an extension, however, looking closer at your error message, it appears you're using the function in a stand-alone page. If that's the case, make sure you're calling it from the phpBB template using the includephp template tag:

Code: Select all

<!-- INCLUDEPHP 'server.php' -->
You must also enable embedded PHP in your templates, which is an option in the admin control panel. Additionally, if server.php isn't in your forum root, adjust the template tag accordingly.
JuergenF
Registered User
Posts: 22
Joined: Mon Mar 16, 2009 8:57 am
Location: Germany
Name: Juergen

Re: Issue with super global $_server

Post by JuergenF »

Thanks MuhClaren - here's a little explaination of my little script:

My php script checks a visitor's IP address ($_SERVER['REMOTE_ADDR']) - checks if already stored in a MySQL database, if not stores it together with timestamp etc in a new row. After one day the entries will be deleted.

Here's how it looks like, hope it's ok to include the link here?
http://popstop-forum.de/counter

Until 3.0.14 the script was included in the statistics (bottom in index_body.html of prosilver) with
<-- INCLUDEPHP counter.php -->
and of course php was enabled in the ACP.
I didn't know that super global $_SERVER is no longer allowed and with the upgrade got that error:

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/wxxx/popstop/forum/cache/twig/ff/0b/7323ad60e47acff22900724a2f81.php:20) in /www/htdocs/wxxx/popstop/forum/includes/functions.php on line 2531
Zurück zur Foren-Übersicht	

Allgemeiner Fehler
Illegal use of $_SERVER. You must use the request class or request_var() to access input data. Found in /www/htdocs/wxxx/popstop/forum/counter.php on line 38. This error message was generated by deactivated_super_global.

BACKTRACE

FILE: (not given by php)
LINE: (not given by php)
CALL: msg_handler()

FILE: [ROOT]/phpbb/request/deactivated_super_global.php
LINE: 67
CALL: trigger_error()

FILE: [ROOT]/phpbb/request/deactivated_super_global.php
LINE: 87
CALL: phpbb\request\deactivated_super_global->error()

FILE: [ROOT]/counter.php
LINE: 38
CALL: phpbb\request\deactivated_super_global->offsetGet()

FILE: [ROOT]/cache/twig/91/9b/b3f07583619e7efbb187a71f572d.php
LINE: 294
CALL: require('[ROOT]/counter.php')

FILE: [ROOT]/vendor/twig/twig/lib/Twig/Template.php
LINE: 264
CALL: __TwigTemplate_919bb3f07583619e7efbb187a71f572d->doDisplay()

FILE: [ROOT]/vendor/twig/twig/lib/Twig/Template.php
LINE: 238
CALL: Twig_Template->displayWithErrorHandling()

FILE: [ROOT]/vendor/twig/twig/lib/Twig/Environment.php
LINE: 299
CALL: Twig_Template->display()

FILE: [ROOT]/phpbb/template/twig/twig.php
LINE: 332
CALL: Twig_Environment->display()

FILE: [ROOT]/includes/functions.php
LINE: 5413
CALL: phpbb\template\twig\twig->display()

FILE: [ROOT]/index.php
LINE: 207
I'm not special in php and I'm just wondering how I could fix that problem so any help would be much appreciated. TIA
Juergen
Post Reply

Return to “Extension Writers Discussion”