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

516

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 themeta name="viewport" tag when the URL parameter?resp=no (URL becomes: www.2eenheid.de/?resp=no) is called. By removing themeta 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?

541

Answer

Solution:

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

Answer

Solution:

Usings sessions for this is too much, a better mechanish exists, called "output_add_rewrite_var". Using this system, we can say the following:

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

The above code fits perfectly in the view place of a MVC system, since it is a view specific option. The code works by looking if$_GET['resp'] exists or not, and depending on that, if it exists, callingoutput_add_rewrite_var("resp", "1"); to put itself in other url's, and if it doesn't exists, it adds the meta tag.

People are also looking for solutions to the problem: php - Insert correctly data with ajax for highcharts

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.