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


Appending URL parameter at every request only if parameter already exists to save opt-out option responsive design

我有一个响应式Wordpress网站:www.2eenheid.de。我的客户希望在手机上有一个选项来查看完整的网站,一个选择退出响应式选项。现在,我找到了这个解决方案:

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

当调用URL参数?resp=no(URL变为:www.2eenheid.de/?resp=no)时,它使用PHP删除meta name="viewport"标记。通过移除meta name="viewport"标签,该网站在移动设备上变得完整。参见以下代码:

<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"; } ?>">

这会将当前页面更改为fullsize,但如果我单击其他链接,它会变回responsible,这是合乎逻辑的,因为该链接不再包含参数?resp=no

现在,我的客户要求,如果用户想要完整的网站,即使在点击不同的URL后,它也需要保持完整的大小,直到用户将其更改为响应。

因此,我的问题是,对于每个URL请求,一旦单击,我如何保存?resp=no参数,直到用户通过单击不同的链接(可能使用不同的参数)将其更改回响应?

我试过在谷歌上搜索,但找不到任何好的解决方案。我看到有人建议会话值,但我发现这很难理解,有些人说这不是一个好的做法,因为会话值用于登录。

如有任何帮助,我们将不胜感激。

第3版:下面的建议看起来不错,但我对PHP会话还很陌生。我用下面的建议更改了我的代码:

<?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"; } ?>">

有人能帮忙吗?

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

和:

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

正如评论中所说,你可能也可以使用cookie,但我从来没有看过它的工作原理。

编辑:一个例子:

<?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>

使用会话太多了,存在一种更好的机制,称为"output_add_rewrite_var"。使用这个系统,我们可以说如下:

<?php if(!isset($_GET['resp'])): ?>
<meta name="viewport" content="width=device-width">
<?php else:
    output_add_rewrite_var("resp", "1");
endif; ?>

上面的代码非常适合MVC系统的视图位置,因为它是一个特定于视图的选项。代码的工作原理是查看$_GET['resp']是否存在,根据这一点,如果存在,调用output_add_rewrite_var("resp", "1");将自己放在其他url中,如果不存在,则添加元标记。