Skip to main content

A useful shortcut in PHP 5

Recently I started using a shortcut for my $_GET variables in PHP 5. It seems that instead of doing the following:

<?php
    $page = $_GET['page'];
    $article = $_GET['article'];
?>

There is an easier way where all the $_GET variables can be assigned automatically.

The function is called parse_str(). See the example below:

<?php
    //example url is: http://www.test.com/index.php?page=home&article=934
    parse_str($_SERVER['QUERY_STRING']);
    //this would give the following:
    //$page = "home"
    //$article = "934"
?>

So this function uses the ampersand sign (&) to divide the string and assign the elements to variables. I wonder if it works with the validation rules of W3C, because normal & are not allowed in a URL query string. One must use &amp; instead.

UPDATE: It does work with the &amp; hardcoded in a URL. The URL shows a normal & sign, so PHP just parses it like a normal symbol.

Comments

Thijs Zumbrink said…
Isn't this unsafe? Reminds me a lot of "register globals" which becomes deprecated in PHP6 with good reason.
Francis Laclé said…
Ha Thijs, correct me if I'm wrong but aren't these as secure as using standard $_GET variables? The only difference is that here you would get an extra $x variable besides just a $_GET['x']. If I don't have this implemented and I add something like &hack=bla in the URL, then PHP would generate $_GET['hack']=bla. Because I'm not doing anything with $hack or $_GET['hack'] in my own code nothing would happen to it. The only risk is when the value of 'hack' would get injected with another value, which isn't really a risk because sensitive data are either stored in POST or SESSION vars.

Popular posts from this blog

But Google what about mobile phones that do not support Javascript?

In the global device market, there are still between 0.2% and 5.4% of phones that do not support Javascript, at least in these set of countries according to this site. In case your mobile website falls within this set than what do you do when you want to optimize CSS delivery by deferring the loading of some CSS but still serving the complete CSS to non-Javascript websites?

Algorithm to sort edge list of simple polygon for 2D and 3D

Sometimes it is handy to sort an edge list. In this case I needed an algorithm to test for concavity of a simple 3D polygon with just one face. You can also apply the procedure on 2D because it just sorts an edge list that could contain either 2D or 3D vertices. The polygons were made in Blender v.2.67 , so the script had to be written in Python and executed via the Run Script button in the text editor. I didn't want to use fancy algorithms to sort edges because we're dealing with simple polygons, so I ended up writing my own. As a side note, the edge-angle checkbox in Blender, which can be used to see if a polygon is convex or concave didn't work for me, so I had no other choice but to first sort edges before I can apply angle calculations on consecutive vertices. Suggestions for improvements are welcome and hopefully it helps someone else who had to deal with the same (or similar) issues in Blender!

A scalable geometrical model for muscle and tendon units: An algorithmic solution on how to fit a template muscle on a high resolution muscle mesh

The reason for this blog post is to share a bit on a particular hard problem that I've encountered during my Master's thesis with a broader audience. I will try my best to write it in plain English, but as the problem is complex expect this to be a lengthy post with domain specific terminology.