JavaScript import_request_variables
Import GET/POST/Cookie variables into the global scope
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 | function import_request_variables (types, prefix) { // Import GET/POST/Cookie variables into the global scope // // version: 1008.1718 // discuss at: http://phpjs.org/functions/import_request_variables // + original by: Jalal Berrami // + reimplemented by: Brett Zamir (http://brett-zamir.me) // + improved by: Brett Zamir (http://brett-zamir.me) // % note 1: IMPORTANT: You must sanitize user variables passed in via URL in JavaScript as in PHP, // % note 1: especially if you want to use any of these variables in an eval()-like function (not recommended)! // * example 1: document.cookie = 'snack=yummy'; // * example 1: import_request_variables('gc', 'pr_'); // * results 1: pr_snack == 'yummy' var i = 0, current = '', url = '', vars = '', arrayBracketPos = -1, arrName='', win = this.window, requestObj = this.window, getObj = false, cookieObj = false; prefix = prefix || ''; var that = this; var _ini_get = function (ini) { if (that.php_js && that.php_js.ini && that.php_js.ini[ini] && that.php_js.ini[ini].local_value) { // Allow designated object to be used instead of window return that.php_js.ini[ini].local_value; } return false; }; requestObj = _ini_get('phpjs.requestVarsObj') || requestObj; if (/g/i.test(types)) { // GET getObj = _ini_get('phpjs.getVarsObj') || getObj; for (i = 0, url = win.location.href, vars = url.substring(url.lastIndexOf('?') + 1, url.length).split('&'); i < vars.length; i++){ current = vars[i].split('='); current[1] = decodeURIComponent(current[1]); arrayBracketPos = current[0].indexOf('['); if (arrayBracketPos !== -1) { arrName = current[0].substring(0, arrayBracketPos); arrName = decodeURIComponent(arrName); if (!requestObj[prefix+arrName]) { requestObj[prefix+arrName] = []; } requestObj[prefix+arrName].push(current[1] || null); if (getObj) { if (!getObj[prefix+arrName]) { getObj[prefix+arrName] = []; } getObj[prefix+arrName].push(current[1] || null); } } else { current[0] = decodeURIComponent(current[0]); requestObj[prefix+current[0]] = current[1] || null; if (getObj) { getObj[prefix+current[0]] = current[1] || null; } } } } if (/c/i.test(types)) { // COOKIE cookieObj = _ini_get('phpjs.cookieVarsObj') || cookieObj; for (i = 0, vars = win.document.cookie.split("&"); i < vars.length;i++){ current = vars[i].split("="); requestObj[prefix+current[0]] = current[1].split(";")[0] || null; if (cookieObj) { cookieObj[prefix+current[0]] = current[1].split(";")[0] || null; } } }} |
Examples
Running
1 2 | document.cookie = 'snack=yummy'; import_request_variables('gc', 'pr_'); |
Should result in
1 | pr_snack == 'yummy' |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have import_request_variables goodness in JavaScript.
@Stuart, Thanks for your comments.
1) There is an example which shows how it works with cookies. The PHP documentation basically shows how it works otherwise, or if not, the source has to work as documentation until such time as volunteers may create documentation (though we're happy to address most questions if it's not clear). Admittedly in this case, this function involves our own (optional) custom behavior, so it does need a little more explaining to use it with all options.
JavaScript can detect the current URL with its query string (window.location.href), so this function takes advantage of that. For $_COOKIE, we get it via document.cookie. As far as $_POST, you won't be able to implement this one, I think I can safely say, unless you're actually working with Server-Side JavaScript (not PHP). With HTML 5, we could perhaps use $_POST for messages submitted by another tab/window, but that's a different story.
As far as why we don't do it during onload, there are two reasons:
1) Kevin has wanted to stick to functions only.
2) import_request_variables() function is not designed to replicate $_GET. It is more designed to control register_globals behavior. register_globals is deprecated and a bad idea, including if using this function, particular without a second argument, so we don't want to automatically run this function. That being said, the function allows you to use our own custom ini setting (ini_set() being a PHP function which we use for PHP ini settings and our own) to designate an object (instead of the global object, as in PHP) which can hold the request variables.
So, if you have "?myvar=hello" at the end of your URL, you can do the following:
ini_set('phpjs.getVarsObj', $_GET = {}); // Only works in PHP.JS, not PHP (!), though by using ini_set(), it does work as though PHP.JS were an extension to PHP
import_request_variables('g'); // We only import $_GET here, but we don't add any prefix to the variable names since we used a more proper "namespace" via the ini_set() call above.
alert($_GET['myvar']); // 'hello'
Note, however, that the (optional) non-PHP behavior of some PHP.JS functions is not guaranteed to remain stable; it's just a convenience, so that's why we don't spend much time documenting it (at least until it's tried out more).
Another option you/we have is to allow some configuration in the namespaced version to set $_GET (or it would also work with the non-namespaced version if run to work in an anonymous namespace).
Just want you guys to know that since this is not a normal PHP function you guys should document it better.
for instance, how do I:
$_GET
$_POST
$_COOKIE
And explain why its called by a function and not just done at window.onload.
O and another thing, have the admin of this site add $_GET, $_POST, $_COOKIE in the function list and have it forward here because at first it seamed like these are missing and or are not possible with this library.
Luckily I found it on another website.
:) but any ways thanks for the time you placed in this one. This is a very important function.


Kevin van Zonneveld
22 Aug '09
Well, no, because those are not functions. They're just global variables, something I'd rather not touch (at least not by default).
Fair enough: If you can achieve this with PHP.JS by going through some hoops like Brett pointed out; you are actively & intentionally bringing globals to your project and I can live with that.
What I could do though is see if there's a better place to reference to Brett's solution for people who are craving; but like he said: I personally don't want to port or maintain anything else than just functions.