Page 1 of 1

Get/Post variables from a string?

Posted: Thu Jun 18, 2009 3:33 pm
by TheBrandon
Hello everyone,

I'm working on a statistics thing for a website, to track visitors, popular pages, etc. (It has to work offline, otherwise I'd use analytics)

Right now I have all of the URL's and Referrers being dumped into a database. Now it's not really a full "website" its just a little program, and it displays information based on the get variables.

For example, every page is index.php.

Here's a few examples:
http://localhost/kioskdb/index.php?idle=1
http://localhost/kioskdb/index.php?view_category=6
http://localhost/kioskdb/index.php?viewnorm=149&back=4

How do I extract just the "149" or just the "4"?

I've got the first 2 worked out. Doing a split on the ? and looking for the "view_category" tag, but how do I do that on the 3rd line? I'd end up with 149&back=4

I suppose I could do another split on the & sign, but is there a more efficient way? Maybe something in PHP that can read those "get" variables from a string without me having to break it into 10 pieces?

Re: Get/Post variables from a string?

Posted: Thu Jun 18, 2009 4:04 pm
by andym01480
Adapted from http://www.php.net/manual/en/function.urldecode.php

Code: Select all

<?php
$QUERY_STRING='http://localhost/kioskdb/index.php?viewnorm=149&back=4';
$c=explode('?',$QUERY_STRING);
$a = explode('&', $c[1]);
$i = 0;
while ($i < count($a)) {
    $b = split('=', $a[$i]);
    echo 'Value for parameter ', htmlspecialchars(urldecode($b[0])),
         ' is ', htmlspecialchars(urldecode($b[1])), "<br />\n";
    $i++;
}
?>