views:

59

answers:

3

First off, I do not want what is in the URL query. I want what PHP see's in the$_GET array.

This is because the URL query will not show all the params if mod_rewrite has been used to make pretty URLs

So is there a way to get the query string that would match exactly what is in the php $_GET array?

--

I came up with a way myself using PHP and JavaScript like so:

function query_string()
{
    <?php
        function assoc_array_to_string ($arr)
        {
            $a = array();
            foreach($arr as $key => $value)
            {
                $str = $key.'='.$value;
                $a[] = $str;
            }
            return implode("&",$a);
        }
    ?>
    return '<?=urlencode(assoc_array_to_string($_GET))?>';
}

...but I need to do this with just javascript if possible because I can't put PHP code in a .js file.

+3  A: 

Won't JavaScript "only see" the query string? How would client-side script know about any rewrite rules?

The only way I can think of is to use PHP -- echo it into a variable in an inline script in your main page rather than the JS file.

Jhong
+1  A: 

The query string is found in window.location.search, but that's the raw query string. So if you run something like this:

(function () {
    QueryStr = {}
    QueryStr.raw = window.location.search.substr(1);
    var pairStrs = QueryStr.raw.split('&');
    QueryStr.val = {}
    for(var i=0,z=pairStrs.length; i < z; i++) {
        var pair = pairStrs[i].split('=');
        QueryStr.val[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
    }
})();

You'd have something very much like $_GET in QueryStr.val.

Of course, you mention that you've mixed things up a bit using mod_rewrite, which is cool, but since we don't know your rewrite scheme, we can't help specifically with that.

However... you know your rewrite scheme, and you could probably modify the code I gave above to operate on some other part of window.location. My bet is that you'd want to split window.location.pathname on the / character instead of &.

Weston C
Thanks, I could possibly (since I know the mod_write scheme) extract it from the URL using regex or something. However, I wanted a less ad-hoc way since I am panning on reusing this on multiple pages with different schemes. I think I may do something like Jhong suggests and just require a parameter in a function that can be passed in from outside the .js file with php. But thanks for the suggestion it is an idea I hadn't even thought of!
John Isaacks
+3  A: 

In your page <head>:

<script type="text/javascript">
var phpQueryParams = <?php print json_encode($_GET); ?>
</script>

Assuming at least PHP 5.2, otherwise use an external package

Nick
Although the returned JSON should be safe to use in terms of possible malicious strings, you should perhaps wrap the script content in <![CDATA[...]]>, combined with a str_replace to turn ]]> into ]"+"]> so that a string containing ]]> doesn't end the CDATA block.If you decide not to use a JSON encoder you should be even more alert to the ways that a malicious query string could inject script into your page.
Nick