最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

cookies - How to generate the COOKIEHASH from JavaScript

programmeradmin0浏览0评论

The overarching question

How do I get the contents of the PHP-variable: COOKIEHASH inside a Cypress-test.


I'm writing some Cypress-test for WordPress, and in order to set the cookies to log in a user in WordPress using Cypress, then I need the contents of the COOKIEHASH-variable.

For those not familiar with the COOKIEHASH, go to a WordPress-installation and put in this code anywhere:

echo '<pre>';
print_r(COOKIEHASH);
echo '</pre>';
die();

And then you'll see it. It looks something like this: a8b94154380982c3284a467b8aa224c6.

When I run my Cypress-tests for the first time, then I don't know what that hash is, so I don't know which cookies to set.

So currently I log in manually, go to cookies in my browser and get the hash. Which is quite the manual operation for automated tests. :-)


My attempts / thoughts

  1. It looks like an MD5-hash. But I can't recreate the cookie-hash with an JS MD5-function. It doesn't correspond. I haven't tried that many MD5-JS-functions, it should be said.

If I hash zeth.dk, then the js-md5-hash gives me: 8aa4b9bbc2a9e946b670fd8372a3081a
and I can see in my cookies that the hash is: b7d8d5a26884619e3b3b4481ba778642

  1. I considered making an API-endpoint (in my functions.php), that returns the cookie-hash. But this just feels wrong, making my code to backflips like this.

The overarching question

How do I get the contents of the PHP-variable: COOKIEHASH inside a Cypress-test.


I'm writing some Cypress-test for WordPress, and in order to set the cookies to log in a user in WordPress using Cypress, then I need the contents of the COOKIEHASH-variable.

For those not familiar with the COOKIEHASH, go to a WordPress-installation and put in this code anywhere:

echo '<pre>';
print_r(COOKIEHASH);
echo '</pre>';
die();

And then you'll see it. It looks something like this: a8b94154380982c3284a467b8aa224c6.

When I run my Cypress-tests for the first time, then I don't know what that hash is, so I don't know which cookies to set.

So currently I log in manually, go to cookies in my browser and get the hash. Which is quite the manual operation for automated tests. :-)


My attempts / thoughts

  1. It looks like an MD5-hash. But I can't recreate the cookie-hash with an JS MD5-function. It doesn't correspond. I haven't tried that many MD5-JS-functions, it should be said.

If I hash zeth.dk, then the js-md5-hash gives me: 8aa4b9bbc2a9e946b670fd8372a3081a
and I can see in my cookies that the hash is: b7d8d5a26884619e3b3b4481ba778642

  1. I considered making an API-endpoint (in my functions.php), that returns the cookie-hash. But this just feels wrong, making my code to backflips like this.
Share Improve this question edited Oct 23, 2020 at 19:02 Zeth asked Oct 23, 2020 at 16:08 ZethZeth 9282 gold badges13 silver badges43 bronze badges 5
  • What did you try hashing? It's the site URL. Or you could define your own fixed COOKIEHASH in wp-config instead. But it shouldn't change for a given site, so even if you do have to do it once manually that shouldn't be a problem? – Rup Commented Oct 23, 2020 at 16:31
  • Looking at your Cypress tests on your other question, you're logging in properly once to get the cookies set then saving the values from there. Couldn't you just save any cookie that matches wordpress_* automatically? and/or find one of them and extract the hash from that. – Rup Commented Oct 23, 2020 at 16:57
  • Thanks for weighing in, @Rup . I updated the question with examples of the JS-MD5 that I used. I tried several and none of the matched. Regarding saving any cookies that matches wordpress_*, then I played around with that for ages. I experienced som wierd bugs. I tried using preserve with a regex, as you suggests - but I just couldn't get it to work. On paper, - what you mention is right. But I just couldn't get it to work. So this is my extensive work-around (that works). :-) – Zeth Commented Oct 23, 2020 at 19:07
  • Try hashing https://zeth.dk - I think that works – Rup Commented Oct 23, 2020 at 19:10
  • Oh, God. You're right! I think I must have messed up http/https for the sites I checked. If you write your comment as an answer, I can mark it as the answer. – Zeth Commented Oct 23, 2020 at 19:37
Add a comment  | 

1 Answer 1

Reset to default 2

By default it's the MD5 hash of the site URL:

    $siteurl = get_site_option( 'siteurl' );
    if ( $siteurl ) {
        define( 'COOKIEHASH', md5( $siteurl ) );
    }

This includes the scheme but not a trailing slash, so in your example you'd need the hash of https://zeth.dk.

You can also override this by defining COOKIEHASH in wp-config.php with some other value.

发布评论

评论列表(0)

  1. 暂无评论