使用 PHP 检索横向/纵向模式
有没有办法检索智能手机的模式?如果是横向模式还是纵向模式?所有这些都可以在 PHP 中访问吗?
JS 中似乎无法访问 Session。但您可以使用 Cookie 来做到这一点。 一个片段(未经测试)
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/; domain=.example.com";
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
window.onorientationchange = function() {
/*window.orientation returns a value that indicates whether iPhone is in portrait mode, landscape mode with the screen turned to the
left, or landscape mode with the screen turned to the right. */
var orientation = window.orientation;
switch(orientation) {
case 0:
// If in portrait mode
document.getElementById("google_map").style.width="300px";
document.getElementById("google_map").style.height="200px";
eraseCookie("orientation");
createCookie('orientation','portrait','0');
break;
case 90:
// If in landscape mode with the screen turned to the left
document.getElementById("google_map").style.width="450px";
document.getElementById("google_map").style.height="325px";
eraseCookie("orientation");
createCookie('orientation','landscape','0');
break;
case -90:
// If in landscape mode with the screen turned to the right
document.getElementById("google_map").style.width="450px";
document.getElementById("google_map").style.height="325px";
eraseCookie("orientation");
createCookie('orientation','landscape','0');
break;
case 180:
// If in portrait mode with the screen flipped
document.getElementById("google_map").style.width="300px";
document.getElementById("google_map").style.height="200px";
eraseCookie("orientation");
createCookie('orientation','portrait','0');
break;
}
}
这是PHP 中的
$orientation = $_COOKIE["orientation"];
if($orientation=="portrait"){
// do something
}else if($orientation=="landscape"){
// do something different
}else{
// fallback
}
Is there a way to retrieve the mode of a smartphone? If it is in landscape or portrait mode? And all that accessible within PHP?
It seems Session cannot be accessed within JS. But you can do that with Cookies. Here is a snippet (not tested)
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/; domain=.example.com";
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
window.onorientationchange = function() {
/*window.orientation returns a value that indicates whether iPhone is in portrait mode, landscape mode with the screen turned to the
left, or landscape mode with the screen turned to the right. */
var orientation = window.orientation;
switch(orientation) {
case 0:
// If in portrait mode
document.getElementById("google_map").style.width="300px";
document.getElementById("google_map").style.height="200px";
eraseCookie("orientation");
createCookie('orientation','portrait','0');
break;
case 90:
// If in landscape mode with the screen turned to the left
document.getElementById("google_map").style.width="450px";
document.getElementById("google_map").style.height="325px";
eraseCookie("orientation");
createCookie('orientation','landscape','0');
break;
case -90:
// If in landscape mode with the screen turned to the right
document.getElementById("google_map").style.width="450px";
document.getElementById("google_map").style.height="325px";
eraseCookie("orientation");
createCookie('orientation','landscape','0');
break;
case 180:
// If in portrait mode with the screen flipped
document.getElementById("google_map").style.width="300px";
document.getElementById("google_map").style.height="200px";
eraseCookie("orientation");
createCookie('orientation','portrait','0');
break;
}
}
In PHP:
$orientation = $_COOKIE["orientation"];
if($orientation=="portrait"){
// do something
}else if($orientation=="landscape"){
// do something different
}else{
// fallback
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您最多可以做的就是用 Javascript 进行检测,然后将该数据发送回服务器。查看
window.orientation
和orientationchange
事件。The most you could do is detect in Javascript and then send that data back to the server. Check out
window.orientation
and theorientationchange
event.