admin管理员组

文章数量:1297093

I'm trying to determine if the user is browsing PWA on server side. On client side I can check if the browser mode is standalone via JavaScript and detect PWA, but on server side this is not an option. So I'm using simple query parameter for that. Start URL in manifest has query param isPwa appended and every time user opens PWA, it goes to /?isPwa. What I need now is to keep that parameter while user browses PWA, so I need to set isPwa parameter to next URL that user opens if the referer URL already had isPwa parameter, but my code goes to redirection loop and I'm unable to identify the cause of this. Here's my code:

function addIsPwaQueryArg() {
    $referer = wp_get_referer();
    if (strpos($referer, 'isPwa') !== false) {
        $location = remove_query_arg('isPwa');
        wp_redirect(add_query_arg('isPwa', '', $location));
        exit;
    }
}
add_action('template_redirect', 'addIsPwaQueryArg');

Can someone tell me what's wrong with my code and why is it not working?

Thanks!

I'm trying to determine if the user is browsing PWA on server side. On client side I can check if the browser mode is standalone via JavaScript and detect PWA, but on server side this is not an option. So I'm using simple query parameter for that. Start URL in manifest has query param isPwa appended and every time user opens PWA, it goes to https://example/?isPwa. What I need now is to keep that parameter while user browses PWA, so I need to set isPwa parameter to next URL that user opens if the referer URL already had isPwa parameter, but my code goes to redirection loop and I'm unable to identify the cause of this. Here's my code:

function addIsPwaQueryArg() {
    $referer = wp_get_referer();
    if (strpos($referer, 'isPwa') !== false) {
        $location = remove_query_arg('isPwa');
        wp_redirect(add_query_arg('isPwa', '', $location));
        exit;
    }
}
add_action('template_redirect', 'addIsPwaQueryArg');

Can someone tell me what's wrong with my code and why is it not working?

Thanks!

Share Improve this question asked Mar 29, 2021 at 21:05 DaftPlugDaftPlug 31 silver badge4 bronze badges 1
  • 1 You shouldn't rely on the referrer, it can be modified by the user, spoofed, and lots of browsers and extensions strip it for privacy purposes – Tom J Nowell Commented Mar 29, 2021 at 21:40
Add a comment  | 

1 Answer 1

Reset to default 0

You're not passing the actual querystring to remove_query_arg(). After some quick testing, I think your $location value is probably an empty string, and the location you're attempting to redirect to is ?isPwa.

The 2nd parameter to remove_query_arg() should be the $referer value.

function addIsPwaQueryArg() {
    $referer = wp_get_referer();
    if (strpos($referer, 'isPwa') !== false) {
        $location = remove_query_arg('isPwa', $referer );
        wp_redirect(add_query_arg('isPwa', '', $location));
        exit;
    }
}
add_action('template_redirect', 'addIsPwaQueryArg');

However: I'm not sure that's the root cause of your redirect loop. It looks like you're checking to see if isPwa is in the querystring, removing it if it is, and then adding it back in (with a blanked value) as you redirect. Is this your intent?

本文标签: plugin developmentDetect if requested page is PWA on server side