request_var

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
radyk
Registered User
Posts: 19
Joined: Thu Jun 18, 2015 7:39 pm

request_var

Post by radyk » Fri Jun 07, 2019 5:00 pm

I am testing the use of cookies and I am having some issues. I thought the following statements would result in "123" being put in the phpbb3_9e25h_poster2 cookie but instead, when I am reading the cookie previously set, it is blank. Does anyone know why?

$user->set_cookie('poster', "123", time() + 120);
$rad3 = request_var($config['cookie_name'] . '_poster', '', false, true);
$user->set_cookie('poster2', $rad3, time() + 120);

Cookie results below:

phpbb3_9e25h_poster 123 localhost / 2019-06-07T16:44:47.010Z 22 ✓
phpbb3_9e25h_poster2 localhost / 2019-06-07T16:44:47.010Z 20 ✓

In the above I expected "123" in poster2 cookie.

User avatar
EA117
Registered User
Posts: 920
Joined: Wed Aug 15, 2018 3:23 am
Contact:

Re: request_var

Post by EA117 » Fri Jun 07, 2019 5:50 pm

The reason is because set_cookie() is building Set-Cookie directives to be included in the HTTP headers of a still-in-progress HTTP response being constructed by the PHP code.

Until the HTTP response has been completed and sent back to the HTTP client, there has not yet been an opportunity for that cookie to be saved, nor for that cookie to have been sent as part of any future HTTP request. Only cookies that had been successfully saved during a previous HTTP response would have been present in the current HTTP request that request_var() is letting you examine.

User avatar
mrgoldy
Jr. Extension Validator
Posts: 1163
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: request_var

Post by mrgoldy » Sat Jun 08, 2019 11:57 am

Moreover, I would suggest to not use request_var().
Use the request class (\phpbb\request\request) instead.

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

Post Reply

Return to “phpBB Custom Coding”