0

A member sign-in form is processed in the theme's functions.php with the code below. For a test, when good credentials are entered in the form, the process is stopped with die("I LIKE COOKIE!");. So when that phrase appears in the browser it is expected that a rma_member cookie would be found in Chrome's settings. There is no cookie rma_member, just PHPSESSID, 4 wordpress..., and 2 wp... cookies. Varying arguments 2 - n in setcookie() has not (yet) made any difference. The setcookie() function returns true.

Edit: The real issue is not whether a cookie is set while in that function but whether the test for the cookie passes on the redirected page. I've proved to myself elsewhere that a cookie is not relevant in this code but on a new page. The redirect appears not to satisfy this requirement.

add_action('init', 'member_password_check');
...
    function member_password_check() {
        if (isset($_POST['_email'])) {
            $email = $_POST['_email'];
            if (empty($email)) {
                die('No email provided!');
            }
            $url = get_option('rma_base_url');
            $get_hash = get_option('rma_get_hash');
            $getURI = $url . $get_hash . '/' . $email;
            $data = wp_remote_get($getURI);
            $code = $data['response']['code'];
            if ('200' == $code) {
                //if good data returned
                $pwObject = json_decode($data['body']);
                $hash = $pwObject[0]->password;
                $password = $_POST['_password'];
                if (password_verify($password, $hash)) {
                    setcookie('rma_member', true, 3600, COOKIEPATH, COOKIE_DOMAIN);
                    die("I LIKE COOKIE!");
                    wp_redirect(home_url('member_content'));
                }
            } else {
                die('Username/password not found');
            }
        }
    }
  • IMO, init is too late to insert cookies. You need to find an earlier action hook. – user42826 Apr 11 '17 at 0:51
  • I've edited the question to include a 'feature' of cookies - that not being set in the if statement is not a sufficient test. So the real question may be why wp_redirect() is defeating the cookie set. – geoB Apr 11 '17 at 2:14
  • Are you sure COOKIEPATH and COOKIE_DOMAIN exist? I wouldnt assume so. I would adding checks or setting them manually. For testing I recommend hard-coding them. – user42826 Apr 11 '17 at 18:23
  • I just noticed your setcookie expiretime is wrong too. You need 'time() + 3600' – user42826 Apr 11 '17 at 18:43
0

This is the correct format for a cookie to expire in one hour (3600 seconds):

setcookie('rma_member', true, time() + 3600, COOKIEPATH, COOKIE_DOMAIN);

setcookie Expire: The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.