note 54634 deleted from language.variables.external by danbrown

From: Date: Wed, 09 Mar 2011 14:36:35 +0000
Subject: note 54634 deleted from language.variables.external by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-177699@lists.php.net to get a copy of this message
Note Submitter: Jonas Lindeløv ---- Here is my solution to the following annoying problems: - When POSTing data and updating/going back one page, an annoying message appears from the browser since the POST data has been preserved. - External user-inputs (POST and GET) are devided into two arrays (POST and GET). Problems often solved with rather exotic solutions, dependent on the specific context. This is my exotic solution, independent of context. A little code-block to be copy-pasted to the top of the desired PHP document. Performing in about 0.1 ms, session_start() not included + around 0.01 ms pr. input variable. Filestreams and URL modifications must be executed before this class is executed. Overview: collects POST and GET data in a class. When data is POSTed, the data is serialized into a session variable, page is reloaded (without POST data) and the data is unserialized into the class again for use through the rest of the document. <?PHP session_start(); class _xvar { function _xvar() { if(!$_SESSION['xvars_serialized']) { // If data has just been recieved, it is saved in $this and the current URL is builded. foreach ($_GET as $key => $value) $this->{$key} = $value; foreach($_POST as $key => $value) $this->{$key} = $value; $this->doc_url = 'http://' .$_SERVER['HTTP_HOST'] .$_SERVER['PHP_SELF'] .'?' .$_SERVER['argv'][0]; // If no POST data is recieved, nothing more is to be done. If POST data is recieved it is serialized to session and the page is redirected to itself with GET data but without POST data. if($_POST) { $_SESSION['xvars_serialized'] = serialize($this); header('Location: ' .$this->doc_url); exit(); } } // Just after the above redirecting, GET and POST data is unserialized into the object. else { $this = unserialize($_SESSION['xvars_serialized']); $_SESSION['xvars_serialized'] = 0; } } } $_XVAR = new _xvar; // Calling the data throughout the document. echo $_XVAR->foo; ?> I find it very useful in my project-management systems with lots of forms and links as user-inputs. The code is completly $_GET and $_POST free! :-)

« previous php.notes (#177699) next »