2

I wrote a small WordPress plugin (mobileesp-for-wordpress) that will redirect mobile users to the mobile site. It's based on mobileesp.

The plugin itself works fine the problem is I added an option to view the full site. This function works by checking for a cookie and if the cookie is there don't redirect the user to the mobile site.

This also works but the user must click the link twice. Once to set the cookie and the second time to take them back into the WordPress site. The full code can be found here but I think this is the relevent part.

$get_cookie_check = $_GET['view_full_site'];
if(isset($get_cookie_check)){
    if($get_cookie_check =='true'){
        //set the cookie
        setcookie("mobileesp_wp_full_site", 'true', time()+86400, "/", $domain);
    }
}
//cookie variable
$full_site_cookie= $_COOKIE['mobileesp_wp_full_site']; 

//make sure the target url is set and full site cookie isn't set
if((get_option('mobileesp_wp_target_url') != '') && ($full_site_cookie !="true")){
    //check for a mobile browser and redirect the user 
}

}

The temporary solution would be to check for the cookie on the mobile site then redirect them but is there a better way to do this.

1 Answer 1

2

The setcookie function prepares the HTTP cookie header to be sent at the next page load. That's why the $_COOKIE superglobal is not updated automatically.

You could manually update $_COOKIE for use on the current request. Just put this below your setcookie() line:

$_COOKIE['mobileesp_wp_full_site'] = 'true';

By the way, I'd prefer not to work with strings like 'true', but just with integers (0 and 1). You then can simply check them like this: ! empty($_GET['view_full_site']).

1
  • I took your advice and am not using 0 and 1 for cookie values thanks!
    – Brooke.
    Commented Nov 13, 2011 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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