admin管理员组

文章数量:1122832

On my localhost environment the following code works perfect

    $creds['user_login'] = $user_login;
    $creds['user_password'] = $user_password;
    $user = wp_signon( $creds, true );
    if(!is_wp_error($user)) {
        wp_redirect('/home');
        exit;
    }

But on the server it doesn't work anymore. The user is never logged in. The website is hosted on a https subdomain on an external server.

I tried setting a cookie domain in my wp-config:

define('COOKIE_DOMAIN', '.domain');

And also with the subdomain included:

define('COOKIE_DOMAIN', 'sub.domain');

Neither worked for me.

The server runs on PHP 7.1 with Varnish. Swift Performance is used for caching.

On my localhost environment the following code works perfect

    $creds['user_login'] = $user_login;
    $creds['user_password'] = $user_password;
    $user = wp_signon( $creds, true );
    if(!is_wp_error($user)) {
        wp_redirect('/home');
        exit;
    }

But on the server it doesn't work anymore. The user is never logged in. The website is hosted on a https subdomain on an external server.

I tried setting a cookie domain in my wp-config:

define('COOKIE_DOMAIN', '.domain.com');

And also with the subdomain included:

define('COOKIE_DOMAIN', 'sub.domain.com');

Neither worked for me.

The server runs on PHP 7.1 with Varnish. Swift Performance is used for caching.

Share Improve this question edited Jan 7, 2020 at 10:21 andy asked Jan 7, 2020 at 8:32 andyandy 1012 bronze badges 2
  • Can you use your browser's debug tools to look for the 'Set-Cookie' response header you're getting on login, and work out why that's wrong? – Rup Commented Jan 7, 2020 at 9:53
  • There are no cookies at all in the response headers on the server. In the localhost environment I see some session cookies being set.... Do you might know if this is a server problem? – andy Commented Jan 7, 2020 at 10:19
Add a comment  | 

2 Answers 2

Reset to default 0

You need to set the auth cookie after wp_signon. Add this code and try and let me know the result.

$user = wp_signon( $creds, false );

$userID = $user->ID;

wp_set_current_user( $userID, $creds['user_login']  );
wp_set_auth_cookie( $userID, true, false );
do_action( 'wp_login',$creds['user_login']  );

Varnish was the problem.

Varnish removes cookies from the response. That's why it didn't work in our case!

本文标签: phpwpsignon works localnot on https