简体   繁体   English

查看包含Cookie,JavaScript和移动设备重定向的完整网站

[英]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. 我有一个网站(www.website.com)和一个移动网站(m.website.com),并且我试图允许用户从移动设备上“查看完整站点”。 Currently I am using some Javascript to check screen width on full site then redirecting to mobile. 目前,我正在使用一些Javascript来检查整个网站上的屏幕宽度,然后重定向到移动设备。

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. 当您单击此按钮时,它会将您重定向到我的脚本“ force_desktop.php”,该脚本设置一个cookie,然后将您发送到主站点。

<?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. 现在,我们设置了一个cookie并重定向到主网站,我们需要检查cookie,而不仅仅是检查屏幕宽度。

Logic 逻辑

If Cookie force_desktop is found 如果找到Cookie force_desktop

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. 这段代码放置在我的head.php文件中,该文件将在每个页面上运行,并放置在脚本的开始和结束标记之间。

Attempt 1 尝试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 尝试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 如果找不到Cookie cookie force_desktop并且screen.width <= 699

Then redirect to m.myseite.com 然后重定向到m.myseite.com

Else 其他

Exit 出口

Note 注意

I have run the following script to make sure a cookie is being placed, and it is. 我已经运行以下脚本,以确保正在放置cookie。

<?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. 我已经没有足够的想法了,并且知道我的编码是不正确的,尤其是If / Else语句中的If / Else语句。 I also am not sure if it is better to use the "isset" to see if the cookie is being used or ($_COOKIES['variable'] == "#"). 我也不确定是否最好使用“ isset”来查看是否正在使用cookie或($ _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. 您正在混合使用JavaScript和PHP。 You're trying to use screen.width in your PHP script, which doesn't make sense. 您正在尝试在PHP脚本中使用screen.width ,但这没有任何意义。 You need to use an echo statement to output the JavaScript into the page. 您需要使用echo语句将JavaScript输出到页面中。 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 您应该在php页面的顶部进行此测试,并确保您不能像这样混合使用php和Java脚本

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>标记启动,就使用代码

..
..
<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. 您不能将javascript和PHP混合使用,javascript是前端,而PHP是后端。 Try something like this: 尝试这样的事情:

if( $something ){

   Header("Location: somewhere.php");

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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