douyinliu8813 2013-07-19 08:12 采纳率: 100%
浏览 65
已采纳

仅当参数已存在以保存选择退出选项响应式设计时,才在每个请求中附加URL参数

I have a responsive Wordpress site: www.2eenheid.de. My client wants an option on a mobile to view the site in fullsize, an opt-out responsive option. Now, I found this solution:

http://css-tricks.com/user-opt-out-responsive-design/

Which uses PHP to remove the meta name="viewport" tag when the URL parameter ?resp=no (URL becomes: www.2eenheid.de/?resp=no) is called. By removing the meta name="viewport" tag the site becomes fullsize on mobile. See code below:

<head>

   <title>My cool site huzzah</title>

   <?php
     if (!$_GET['resp'] == 'no') { ?>
       <meta name="viewport" content="width=device-width">
   <?php } ?>

</head>

<body class="<?php if (!$_GET['resp'] == 'no') { echo "resp"; } ?>">

This changes the current page to fullsize, but if I click a different link it changes back to responsive, which is logical because the link doesn't contain the parameter ?resp=no anymore.

Now my client has the requirement that if a user wants the fullsize website, it needs to stay fullsize even after clicking a different URL until a user changes it back to responsive.

So my question is how can i save the ?resp=no parameter once clicked for every URL request, until a user changes it back to responsive by e.g. clicking a different link (maybe with a different parameter)?

I've tried googling but I can't find any good solutions. I've seen people suggesting session values but I find that very hard to understand and some people say that thats not good practice because session values are used for logins.

Any help would be highly appreciated.

EDIT 3: The suggestion below seems good, but I'm quite new to PHP sessions. I changed my code with the suggestion below to this:

<?php ini_set('display_errors', true);

session_start(); 
if(isset($_REQUEST['resp'])) {
    $_SESSION['resp'] = boolval($_REQUEST['resp']);
}

// Check if enabled
$enabled = isset($_SESSION['resp']) && $_SESSION['resp'];


?><!DOCTYPE html>

<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />

<?php if($enabled): ?>
    <meta name="viewport" content="width=device-width">
<?php endif; ?>

<title>LALALA</title>

</head>

<body <?php body_class(); ?> id="<?php if(isset($_SESSION['resp']) && $_SESSION['resp']) { echo "resp"; } ?>">

Can anyone help with this?

  • 写回答

2条回答 默认 最新

  • duanhao9176 2013-07-19 08:18
    关注
    session_start();
    
    // ...
    
    if(isset($_REQUEST['resp'])) {
        $_SESSION['resp'] = boolval($_REQUEST['resp']);
    }
    

    And :

    <?php if(isset($_SESSION['resp']) && $_SESSION['resp']): ?>
        <meta name="viewport" content="width=device-width">
    <?php endif; ?>
    

    You could probably use cookies too as said in a comment, but i never looked how it works.

    EDIT : An example :

    <?php
    
    // If the parameter is in the URL, enable/disable (else leave as it currently is).
    if(isset($_REQUEST['enableMode'])) {
        $_SESSION['enableMode'] = boolval($_REQUEST['enableMode']);
    }
    
    // Check if enabled
    $enabled = isset($_SESSION['enableMode']) && $_SESSION['enableMode'];
    
    ?>
    
    <?php if($enabled): ?>
        <meta name="viewport" content="width=device-width">
    <?php endif; ?>
    
    <a href="index.php?enableMode=1">Enable mode</a>
    <a href="index.php?enableMode=0">Disable mode</a>
    <a href="index.php">Change page without changing anything</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch