note 54634 added to language.variables.external

From: Date: Sun, 10 Jul 2005 22:49:06 +0000
Subject: note 54634 added to language.variables.external
Groups: php.notes 
Request: Send a blank email to php-notes+get-91747@lists.php.net to get a copy of this message
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['vars_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! :-) ---- Manual Page -- http://www.php.net/manual/en/language.variables.external.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54634 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54634&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54634&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54634&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54634&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54634&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54634&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54634&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91747) next »