note 74436 added to reserved.variables

From: Date: Wed, 11 Apr 2007 14:37:54 +0000
Subject: note 74436 added to reserved.variables
Groups: php.notes 
Request: Send a blank email to php-notes+get-125295@lists.php.net to get a copy of this message
If you're working with $_GET a lot and need to preserve already set variables in a link for the next page, this function is pretty handy for simplifying the process of generating a new URL: string setUrlVariables([string var, string value], [varN, valueN], ...) <?php function setUrlVariables() { $arg = array(); $string = "?"; $vars = $_GET; for ($i = 0; $i < func_num_args(); $i++) $arg[func_get_arg($i)] = func_get_arg(++$i); foreach (array_keys($arg) as $key) $vars[$key] = $arg[$key]; foreach (array_keys($vars) as $key) if ($vars[$key] != "") $string.= $key . "=" . $vars[$key] . "&"; if (SID != "" && SID != "SID" && $_GET["PHPSESSID"] == "") $string.= htmlspecialchars(SID) . "&"; return htmlspecialchars(substr($string, 0, -1)); } ?> You use it like this: <a href="nextpage.php<?php echo setUrlVariables(); ?>">Link</a> In this case setUrlVariables() will simply add all the $_GET variables of the current page/URL and even takes care of the PHPSESSID if you use one (and didn't change the default variable name). The above HREF would complete to e.g.: "nextpage.php?var=21&amp;PHPSESSID=BI89J" If you supply arguments, do it like this: <?php echo setUrlVariables("user", "foobar"); ?> This would complete the HREF to e.g.: "nextpage.php?user=foobar&amp;var=21&amp;PHPSESSID=BI89J" Unsetting variables works by supplying an empty value: <?php echo setUrlVariables("var", ""); ?> "nextpage.php?user=foobar&amp;PHPSESSID=BI89J" setUrlVariables() also makes sure it produces "pretty URLs", so it doesn't output any unnecessary garbage. ;) <?php session_destroy(); echo setUrlVariables("user", ""); ?> "nextpage.php" ---- Server IP: 61.195.146.164 Probable Submitter: 220.209.181.27 ---- Manual Page -- http://www.php.net/manual/en/reserved.variables.php Edit -- https://master.php.net/note/edit/74436 Del: integrated -- https://master.php.net/note/delete/74436/integrated Del: useless -- https://master.php.net/note/delete/74436/useless Del: bad code -- https://master.php.net/note/delete/74436/bad+code Del: spam -- https://master.php.net/note/delete/74436/spam Del: non-english -- https://master.php.net/note/delete/74436/non-english Del: in docs -- https://master.php.net/note/delete/74436/in+docs Del: other reasons-- https://master.php.net/note/delete/74436 Reject -- https://master.php.net/note/reject/74436 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125295) next »