简体   繁体   English

使用URL参数设置cookie并禁用重定向

[英]Using URL paramaters to set a cookie and disable redirect

I want to allow my mobile users a way out back to the desktop version of our site. 我想让我的移动用户可以回到我们网站的桌面版本。 I would like to give them 2 options; 我想给他们两个选择; 1 sending them back to the desktop version for only one page load, 2 setting a cookie so they do not get redirect back to the mobile sub domain for a period of 1 week. 1将它们发送回桌面版本仅需加载一页,2设置cookie以使它们在1周内不会重定向回移动子域。

My site is a web proxy where there is only 1 index page and the rest being all dynamic content. 我的网站是一个Web代理,其中只有1个索引页,其余都是动态内容。 For that reason only my index page has a mobile version. 因此,只有我的索引页面具有移动版本。

Example, desktop users come to www.domain.com while mobile users get redirected to m.domain.com. 例如,桌面用户访问www.domain.com,而移动用户重定向到m.domain.com。 But once using the proxy there is no mobile version and all users get sent to proxy.domain.com/dynamiccontent. 但是一旦使用代理,便没有移动版本,所有用户都将发送到proxy.domain.com/dynamiccontent。

This is the PHP i am using to direct mobile browsers to the mobile version of the site using the " http://detectmobilebrowsers.com/ " PHP Regex. 这就是我用来通过“ http://detectmobilebrowsers.com/ ” PHP Regex将移动浏览器定向到网站的移动版本的PHP。

if (isset($_COOKIE['desktop'])) {
  $version = "desktop";
} else 

if (preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i', $_SERVER['HTTP_USER_AGENT'])) {
   $version = "mobile";
} 

if ($version == "mobile") {
header('Location: http://m.domain.com');
}

This is working great but i need to give my mobile users 2 extra options and i was hoping i could do with with URL parameters. 这很好用,但是我需要给移动用户2个额外的选项,我希望可以使用URL参数。 First "Use desktop site" which would load the desktop site for 1 page load but if the user were to reload www.domain.com they would once again get directed to the mobile. 首先是“使用桌面网站”,它将以1页的加载速度加载桌面网站,但是如果用户要重新加载www.domain.com,他们将再次被定向到移动设备。 Secondly "Always use desktop site" which should set the cookie to ignore the redirect to the mobile site until the cookie expires (ex 604800). 其次,“始终使用桌面站点”应将cookie设置为忽略对移动站点的重定向,直到cookie过期(ex 604800)。

I know i could create a new .php file to set the cookie and make the "Always use desktop site" call that file but due to the way themes are called as well as how many different themes and domains are involved i was hoping to be able to set the cookie using URL parameters. 我知道我可以创建一个新的.php文件来设置Cookie并让“始终使用桌面网站”调用该文件,但是由于调用主题的方式以及所涉及的不同主题和域的数量,我希望能够使用URL参数设置Cookie。

Something along the lines of the following would be my desired outcome. 以下是我所期望的结果。

<a href="//www.domain.com/?noredirect" title="Go to desktop site">Use desktop site</a>  

Should send the mobile viewer to the desktop site without setting a cookie 应该将移动查看器发送到桌面站点而不设置cookie

<a href="//www.domain.com/?setdesktop" title="Set desktop site to default">Always use desktop site</a>

Should send the mobile viewer to the desktop site while setting the "desktop" cookie for 1 week to work with the cookie my IF statement is checking for. 在将“桌面” Cookie设置1周以与我的IF语句正在检查的Cookie配合使用时,应将移动查看器发送到桌面站点。

This is what i ended up doing and it seems to be working exactly how i need it. 这就是我最终要做的事情,而且它似乎完全可以满足我的需要。

If the cookie is there is sets the version variable to desktop. 如果存在cookie,则将version变量设置为desktop。 If the noredirect url parameter is there the user is not redirected and if the setdesktop url parameter is there then the desktop cookie is created and the user also does not get redirected. 如果使用noredirect url参数,则不会重定向用户;如果使用setdesktop url参数,则将创建桌面cookie,也不会重定向用户。

This is placed on the desktop page 这放置在桌面页面上

if (isset($_COOKIE['desktop'])) {
  $version = "desktop";
} else

if (preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i', $_SERVER['HTTP_USER_AGENT'])) {
       $version = "mobile";
}

if (isset($_GET['noredirect'])) {
$version = "desktop";
}

if (isset($_GET['setdesktop'])) {
setcookie("desktop", time()+604800);
$version = "desktop";
}

if ($version == "mobile") {
header('Location: http://m.domain.com');
}

These are the links i use on my mobile page 这些是我在移动页面上使用的链接

    <a href="//www.domain.com/?noredirect" title="Go to desktop site">Use desktop site</a>  

    <a href="//www.domain.com/?setdesktop" title="Set desktop site to default">Always use desktop site</a>

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

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