Switch between mobile and full site

寵の児 提交于 2019-12-23 04:18:08

问题


All my full site pages are under folder public_html, which contains index.php and folder m which contains all the mobile site pages. index.php is like this:

<?php
  if (...mobile client) {
    header('Location: m/');
  } else {
    include 'front.html.php';
  }
?>

There is also an index.php in m, simply `include 'front.html'.

This script can detect user's client and direct to full site and mobile site automatically.

But now I want a link on the mobile site to switch to full site. If it is like <a href="../front.html.php">switch to full site</a>, there will be front.html.php in the address bar, and I don't want this. However, if it is like <a href="../">switch to full site</a>, it will detect again and back to mobile site.

How to deal with that?


回答1:


You can accomplish this by setting a cookie.

  setcookie("forceClient", "full", time()+3600);

Then from that php script, redirect to the home page.

In your index.php, check the cookie:

if($_COOKIE["forceClient"] == "full"){
  //logic
}



回答2:


Make a session variable like

$_SESSION['viewer_type'] = "Mobile";

or

$_SESSION['viewer_type'] = "Regulare";

and then define a new variable call it base_url and save it in the session also and do this

if($_SESSION['viewer_type'] == 'Mobile'):
     $_SESSION['base_url'] = "http://www.m.site.com/";
else:
     $_SESSION['base_url'] = "http://www.site.com/"; 
endif; 

the link now will be

<a href="<?php echo $_SESSION['base_url'] ?>front.html.php">Test</a>

You need to set the session variable each time the view changed from mobile to regular or visa versa



来源:https://stackoverflow.com/questions/8461301/switch-between-mobile-and-full-site

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!