Reading/Writing cookies in phpbb

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
richey
Registered User
Posts: 608
Joined: Mon Feb 18, 2002 4:26 pm
Location: now@Cyberspace
Contact:

Reading/Writing cookies in phpbb

Post by richey » Mon Mar 02, 2015 12:02 pm

Hello,

trying to read + write cookies with an embedded php script I got an error message

Code: Select all

Illegal use of $_COOKIE. You must use the request class or request_var() to access input data. Found in /var/www/....xyz.php on line 321. This error message was generated by deactivated_super_global.
What is the correct way to read & write cookies from 'within' phpbb?
.

User avatar
david63
Jr. Extension Validator
Posts: 15060
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Reading/Writing cookies in phpbb

Post by david63 » Mon Mar 02, 2015 12:48 pm

I spent ages trying to understand and get this working. You need to use something along the lines of this to read the cookie$cookie_set = $this->request->is_set('my_cookie', \phpbb\request\request_interface::COOKIE) ? true : false;
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
richey
Registered User
Posts: 608
Joined: Mon Feb 18, 2002 4:26 pm
Location: now@Cyberspace
Contact:

Re: Reading/Writing cookies in phpbb

Post by richey » Mon Mar 02, 2015 1:50 pm

I found it out. Luckily, there's a simpler way:

Read cookie:

Code: Select all

$cookie_value = request_var('MyCookieID','',false,true);
Write cookie:

Code: Select all

$HostDomain = str_replace("http://","",$request->server('HTTP_HOST'));
$SetCookieExpire = mktime() +604800; // 1 week
setcookie('MyCookieID', $cookie_value, $SetCookieExpire, "/", $HostDomain, 0);
Hope this is of help for someone.
.

User avatar
david63
Jr. Extension Validator
Posts: 15060
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Reading/Writing cookies in phpbb

Post by david63 » Mon Mar 02, 2015 2:11 pm

request_var has been depreciated in 3.1 - you should now be using $request_variable - see
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
richey
Registered User
Posts: 608
Joined: Mon Feb 18, 2002 4:26 pm
Location: now@Cyberspace
Contact:

Re: Reading/Writing cookies in phpbb

Post by richey » Mon Mar 02, 2015 6:27 pm

there went another chance to prove my brilliance :cry: :lol:
.

User avatar
martti
Registered User
Posts: 684
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: Reading/Writing cookies in phpbb

Post by martti » Sat Nov 17, 2018 7:07 am

Setting a cookie:

Code: Select all

/**
* $this->user is of type \phpbb\user   (@user is the short name in the service configuration)
* 
* You could also use \phpbb\session   (@session)
*/

$this->user->set_cookie('my_cookie_name', 'my_cookie_value', 0);

/**
* The 3rd parameter is the unix time for the cookie life. 
* When it is zero, the cookie will stay as long as the session.
*/
Reading a cookie:

Code: Select all

/**
* $this->request is of type \phpbb\request\request (@request)
* $this->config is of type \phpbb\config\config   (@config)
*/

$my_cookie_value = $this->request->variable($this->config['cookie_name'] . '_my_cookie_name', '', false, \phpbb\request\request_interface::COOKIE);

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: Baidu [Spider] and 10 guests