简体   繁体   中英

View full site with cookies, javascript and mobile redirect

I have a website(www.website.com) and a mobile site (m.website.com) and I'm trying to allow users to "View Full Site" if they want from mobile. Currently I am using some Javascript to check screen width on full site then redirecting to mobile.

if (screen.width <= 699) {
document.location = "http://m.website.com";
}

This works fine. On the mobile site there is a "View Full Site" button. When you click this it redirects you to my script "force_desktop.php" which sets a cookie and then sends you to the main site.

<?php
setcookie("force_desktop", "1");
header("Location: http://www.mywebsite.com");
?>

Now that we set a cookie and redirected to the main website we need to check for the cookie instead of just checking for the screen width.

Logic

If Cookie force_desktop is found

Then exit

Else

run screen size test

Here is the code I attempted to use but doesn't work. This code is placed in my head.php file which will be run on every page and is placed between the script opening and closing tags.

Attempt 1

if($_COOKIE['force_desktop'] == '1' {
exit;
} else if($_COOKIE['force_desktop'] != '1' {
if (screen.width <= 699) {
document.location = "http://m.website.com";
}
};

Attempt 2

if(isset ($_COOKIE["force_desktop"])){
exit;
else
if (screen.width <= 699) {
document.location = "http://m.mywebsite.com";
};

Alternative logic that could work

IF Cookie force_desktop is not found AND screen.width <= 699

Then redirect to m.myseite.com

Else

Exit

Note

I have run the following script to make sure a cookie is being placed, and it is.

<?php
print_r($_COOKIE);
?>

I've run out of ideas and know my coding isn't correct, especially the If/Else statement within the If/Else statement. I also am not sure if it is better to use the "isset" to see if the cookie is being used or ($_COOKIES['variable'] == "#"). I'd really appreciate some good feedback on this one.

Thanks in advance for your help and suggestions,

Matt

You're mixing JavaScript and PHP. You're trying to use screen.width in your PHP script, which doesn't make sense. You need to use an echo statement to output the JavaScript into the page. It'll then check the user's screen resolution and do the redirect.

Try this:

if(isset ($_COOKIE["force_desktop"])){
    exit;
}
else
{
    echo '<script> 
    if (screen.width <= 699) {
    document.location = "http://m.mywebsite.com";}
    </script>'; 
};

you should do this test at the top of the php page, and for sure you cannot mix php and java script like this

u can alter this code like

<?php
 $flag = 0;
 if(isset($_COOKIE['force_desktop']))$flag++;
?>

later in the page use the code as soon as <head> tag starts

..
..
<head>
<?php
if(!$flag){
echo '<script> 
if (screen.width <= 699) {
document.location = "http://m.mywebsite.com";
</script>'; 
 }
?>

You cannot mix javascript and PHP, javascript is front end and PHP is back end. Try something like this:

if( $something ){

   Header("Location: somewhere.php");

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM