phpjs
Version:
89 lines (62 loc) • 5.43 kB
HTML
<!-- Generated by Rakefile:build -->
<strong>
<a href="http://tablefield.com" rel="nofollow">Oria</a>
</strong>
on 2012-03-31 03:31:00 <br />
Hi,
If I got it right - by default the requestObj is the globals?? run over all my global vars with the ones from cookies and url? that's really a bad idea...
PHP had removed that feature, so I feel free to criticize it here as well :)
In any case requestObj should consider the ini_get('register_globals') and skip requestObj when this setting is off.
I feel that by default it should either do nothing (like get and cookies are not populated by default).
Though personally I would be really happy that by default this function will populate $_GET and $_COOKIES and $_REQUEST under the current namespace (even if it is this.window, because no one would ever use a var named exactly "$_GET" for any other use...)
I suggest:
<pre><code>
line 33: if (!_ini_get('register_globals')) requestObj=false;
line 44: if (requestObj!==false && !requestObj[prefix + arrName]) {
line 47: if (requestObj!==false) requestObj[prefix + arrName].push(current[1] || null);
line 56: if (requestObj!==false) requestObj[prefix + current[0]] = current[1] || null;
line 67: if (requestObj!==false) requestObj[prefix + current[0]] = current[1].split(";")[0] || null;
</code></pre>
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2009-08-22 17:11:52 <br />
@ Stuart: "have the admin of this site add $_GET, $_POST, $_COOKIE in the function list".
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.
<hr />
<strong>
<a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a>
</strong>
on 2009-08-16 18:08:51 <br />
, 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:
<pre><code>
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'
</code></pre>
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).
<hr />
<strong>
Stuart
</strong>
on 2009-08-14 07:13:58 <br />
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.
<hr />