Tuesday, June 21, 2022
HomeWordPress Developmentcookies - Modifying request $_COOKIE

cookies – Modifying request $_COOKIE


I’ve setup some conditional logic inside a WP theme to show sure sections based mostly a cookie worth.

I’m making an attempt now add some logic to override that cookie worth within the request itself, based mostly on a URL parameter.

The purpose being, that if the cookie exists, the traditional conditional logic follows, but when the URL parameter exists, then the cookie must be modified BEFORE WP processes something (but in addition ideally, earlier than the server processes it*)

Q1: I presently have a working resolution for WP, however I believe it’s utilizing some dangerous observe (instantly modifying superglobals) and I would wish to know if there’s a higher strategy.

Q2: I might additionally wish to know if there’s a technique to catch the request a lot earlier, like maybe a way to do that in htaccess, in order that LS Cache can serve the right cached model based mostly on a cookie cache differ…..
I’m utilizing Litespeed and can setup a differ cache based mostly on the cookie, however I dont assume this can work with my present resolution as I am solely altering the cookie as soon as it will get to WP – I consider Litespeed would have already served the cached model.

That is what I am presently doing – fairly easy.

add_filter('parse_request', 'maybe_set_dynamic_state_cookie');

perform maybe_set_dynamic_state_cookie() {

    if (isset($_GET['state'])) {

        $valid_states = array('QLD', 'NSW', 'VIC', 'SA', 'WA', 'NT', 'ACT', 'TAS');

        $state = strtoupper( $_GET['state'] );

        if(in_array($state, $valid_states) ) {
            $_COOKIE['dynamicDataState'] = $state;
        }
    
    }
}

So the server could obtain for instance the next request:

:methodology: GET
:path: /college students/?state=vic
:scheme: https
cookie: dynamicDataState=NSW
...

the place URL parameter is completely different from submitted cookie. On this case, earlier than something occurs it must be up to date to:

:methodology: GET
:path: /college students/?state=vic
:scheme: https
cookie: dynamicDataState=VIC
...

That is what presently happens with the above perform, however solely as soon as WP will get to it… I believe LSChache would have already served the dynamicDataState=NSW cached model.

It’s of much less significance to the query right here, however I’m additionally doing the next to replace the response headers so the customers browser is up to date to match the modified request change.

add_action( 'init', 'set_dynamic_state_cookie' );

perform set_dynamic_state_cookie() {
    if (isset($_GET['state'])) {



        $valid_states = array('QLD', 'NSW', 'VIC', 'SA', 'WA', 'NT', 'ACT', 'TAS');

        $state = strtoupper( $_GET['state'] );

        $identify="dynamicDataState";

        if(in_array($state, $valid_states) ) { 
            setcookie( $identify, $state, time() + 3600, "/", COOKIE_DOMAIN );
        }
       
    }
}

UPDATE

I’ve obtained bit additional with this and labored out that I can in reality use an htacess rewrite rule to vary the cookie. …I believe it is working with LSCache and the conditional logic…
I am positive it could possibly be written extra cleanly although and I nonetheless must get a response header cookie despatched again…

RewriteEngine on
RewriteCond %{QUERY_STRING} state=qld
RewriteRule ^(.*)$ - [CO=dynamicDataState:QLD:/:3600]
RewriteCond %{QUERY_STRING} state=new
RewriteRule ^(.*)$ - [CO=dynamicDataState:NSW:/:3600]
RewriteCond %{QUERY_STRING} state=vic
RewriteRule ^(.*)$ - [CO=dynamicDataState:VIC:/:3600]
RewriteCond %{QUERY_STRING} state=wa
RewriteRule ^(.*)$ - [CO=dynamicDataState:WA:/:3600]
RewriteCond %{QUERY_STRING} state=sa
RewriteRule ^(.*)$ - [CO=dynamicDataState:SA:/:3600]
RewriteCond %{QUERY_STRING} state=tas
RewriteRule ^(.*)$ - [CO=dynamicDataState:TAS:/:3600]
RewriteCond %{QUERY_STRING} state=nt
RewriteRule ^(.*)$ - [CO=dynamicDataState:NT:/:3600]
RewriteCond %{QUERY_STRING} state=act
RewriteRule ^(.*)$ - [CO=dynamicDataState:ACT:/:3600]

So as to add some extra data:

  • Customers are initially utilizing a drop down choose to decide on a ‘state’, this triggers a script that provides the cookie initially after which reloads the web page in order that they see their state particular data on that web page and the remainder of the positioning.
  • I’ve a cache differ setup for this cookie in LSCache so the right pages are served to customers with the cookie set.
  • As soon as the web page is cached, then WP is totally bypassed so any logic I put in there to vary the cookie based mostly on URL doesnt work, nor does sending a brand new cookie again within the response header
  • The above start line for my htacess resolution appears to work to date, however doesn’t ship a brand new cookie header response again so the person nonetheless has the outdated cookie of their browser. I must resolve this?

So new questions:

Is there a cleaner, extra concise technique to write the above rewrite?

Is there a technique to additionally conditionally ship a response header again even when a cached web page is served?

…OR is there a bette means typically to attain this?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments