[英]Redirecting to responsive site in PHP based on screen resolution detected in Javascript
Currently I have a PHP library that looks at header information and decides what kind of browser/device the user is using to access my site. 目前,我有一个PHP库,用于查看标头信息并确定用户使用哪种浏览器/设备访问我的网站。 I hooked up my router to this library so that when a user is at my site, based on what the PHP library say I redirect him to a mobile or desktop version.
我将路由器连接到该库,以便当用户在我的站点上时,根据PHP库所说的,我将其重定向到移动或台式机版本。 All this happens in pure PHP.
所有这些都发生在纯PHP中。
Now the complication is that many tablet devices have such wide screens that showing them a responsive site for their screen dimension does not really make much sense. 现在的麻烦在于,许多平板电脑设备的屏幕如此宽阔,以至于无法向他们展示其屏幕尺寸的响应位置确实没有多大意义。 I would like it that my router can get information about the screen dimension/viewport of the user and then make the choice instead of just checking if it
isTablet()
. 我希望路由器可以获取有关用户的屏幕尺寸/视口的信息,然后做出选择,而不只是检查它是否为
isTablet()
。
I know that I need to use Javascript for this. 我知道我需要为此使用Javascript。 I wanted to check if anyone has experience doing something like this.
我想检查是否有人有做过这样的事情的经验。 To keep it clean, I would like my router to be the decision maker, and not have both Javascript and PHP making independent decisions on where to redirect.
为了保持整洁,我希望路由器成为决策者,而不是让Javascript和PHP都对重定向的位置做出独立的决定。 Is there any library/strategy to handle this use case that anyone has prior experience of?
是否有任何图书馆/策略可以处理任何人都有过使用经验的用例?
You can use the windows width to check for a redirect in your <head>
, that redirect should set a $_SESSION
variable to not show the mobile site in php
. 您可以使用Windows宽度检查
<head>
中的重定向,该重定向应设置$_SESSION
变量以不显示php
的移动网站。
The following method requires that the jQuery library be loaded in <head></head>
and placing: 以下方法要求将jQuery库加载到
<head></head>
并放置:
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
JS: JS:
if ( $(window).width() > 1000 ) {
document.location = '/no-mobile-redirect.php?set=true';
}
no-mobile-redirect.php would have no-mobile-redirect.php将有
<?php
session_start();
$_SESSION['noMobileRedirect'] = isset($_GET['set']);
header("Location: /home");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.