note 125405 added to reserved.variables.request

From: Date: Fri, 02 Oct 2020 07:40:02 +0000
Subject: note 125405 added to reserved.variables.request
Groups: php.notes 
Request: Send a blank email to php-notes+get-215502@lists.php.net to get a copy of this message
Warning: The way $_REQUEST is populated by default can lead to strange bugs because of the unfortunate default setting of the configuration directive 'variables_order'. Example: In an e-shop you want to display prices based on user preference. User can either switch the currency or the previous selection is used. By defaut, the following code WILL NOT WORK as expected: <?php if ($_REQUEST['currency']) # change currency on user request { $currency = $_REQUEST['currency']; # use it setcookie('currency', $_REQUEST['currency'], 0, 'eshop.php'); # store it } else # use default currency { $currency = 'USD'; } # display shop contents with user selected currency echo 'All prices are shown in ', $currency; # let the user switch currency echo '<a href="eshop.php?currency=USD">Switch to USD</a>'; echo '<a href="eshop.php?currency=EUR">Switch to EUR</a>'; ?> Regardless of the user choice, the cookie value is used, so unless you change the default 'request_order' or 'variables_order' the $_REQUEST[something] variable is stuck with the cookie value forever regardless of the user 'REQUEST'. Fix 1: <?php ini_set('request_order', 'CGP'); # use previous value (stored in cookie) or new value upon user request . . . ?> Fix 2: Be very careful and patient and go with $_GET, $_POST and $_COOKIE instead of the convenient $_REQUEST. Good luck. ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.172 (proxied: 89.177.41.51) ---- Manual Page -- http://php.net/manual/en/reserved.variables.request.php Edit -- https://master.php.net/note/edit/125405 Del: integrated -- https://master.php.net/note/delete/125405/integrated Del: useless -- https://master.php.net/note/delete/125405/useless Del: bad code -- https://master.php.net/note/delete/125405/bad+code Del: spam -- https://master.php.net/note/delete/125405/spam Del: non-english -- https://master.php.net/note/delete/125405/non-english Del: in docs -- https://master.php.net/note/delete/125405/in+docs Del: other reasons-- https://master.php.net/note/delete/125405 Reject -- https://master.php.net/note/reject/125405 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#215502) next »