Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parsing query string parameters. 2

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
Hi All,
Anyone have code to parse out the URI?

I need to break out the parameter values into an array I can loop through and test individually.
The page will look through the parameters for specific ones that are used to control display of the navigation menu and if not found will set default values. It then builds a new URL with the appropriate parameters and appends any additional parameters passed as well so they are not lost then it redirects to that new URL.

Essentially I intercept all requests to specific sub folders with an HTACCESS which rewrites the URL to go to the processing script above passing in the REQUEST_URI and QUERY_STRING values to the processing page.
I know how to pull off specific values from the URL so I can get the string containing all the parameters, I just need some code that will parse that string into an array where I can test each name/value.

I figure this is probably pretty easy but I am still a novice at PHP.
An example of what I need to do is this.
I pull in the parameter string that looks like this:
parm1=something&parm2=somethingelse&parm3=avalue

I need to loop through the parameter names to see if parm1 and parm2 exist. If they do then I add their name and value to a new string I am building, if they do not then I add them in with default values of 0. If the parameter name I read in is not one I am specifically looking for I add it to another string that will be appended to the end of the URL so they will be passed through.

So what I really need is either a method to split the values out into an array in name/value pairs and an example of looping and testing the values by name, or code that will work with the string directly doing the same thing and not using the array.

I could write this in Javascript or VBScript easily enough but just do not have the experience in PHP yet.

If anyone has sample code along these lines I can figure it out and adapt it to my needs. If it is simple enough and would not take too much time and you wrote out something specific to my needs I would greatly appreciate it.
This is a project I started for a local middle school web site which I am donating my time for. I have had too many interupptions and the project has sat for too long and they are anxious to get moving. I feel bad that I have held them up for so long and I even took today off of work as a vacation day just to focus on getting this ready so they can begin filling in the content.

Anyway, this is a long description for what should be a simple thing. I would appreciate any help or advice.

Thanks.

At my age I still learn something new every day, but I forget two others.
 
This is actually very simple: you already have the array:
Code:
foreach $_GET AS $key => value
{
[...]
}

I think this is what you are asking for?

Take Care,
Mike
 
I am not sure.
How is each value returned in the foreach loop?
Would the value returned be like "Parm1=something" or is there a way to access the name and value separately?

If it is a single string for the name=value pair I would have to split it at the = sign to test the name only.

I am not sure of the commands and syntax in PHP but the logic would be something like this.
Code:
myparm1 = "parm1=0"
myparm2 = "parm2=0"
theirparm = ""

foreach value in $_GET
  if left(value, 6) = "parm1=" then
    myparm1 = value
  else
  if left(value, 6) = "parm2=" then
    myparm2 = value
  else 
    theirparm = theirparm + value
  end if
next

newurl = "mypath/myfile.php?" + myparm1 + myparm2 + theirparm

The above is not valid code for any language, just a representation of the logic I am trying to follow to build the new url. If I cannot access the name and value of each independently then I have to read it out with left() or contains() or a regular expression, etc.


At my age I still learn something new every day, but I forget two others.
 
the query string is preparsed by php. it is in the $_GET superglobal.

so with a url like this somepage.php?parm1=val1&parm2=val2 you could get to where you wanted like this:

Code:
echo buildquerystring();
function buildquerystring() {
  $vars = array("parm1","parm2"); //the parameters you want to track
  $str = "";
  foreach ($vars as $var):
    $val = isset($_GET[$var]) ? $_GET[$var] : "0";
    $str .= "&$var=$val";
  endforeach;
  $str = "?" . ltrim($str, "&");
}
 
I see, comparing the superglobal against the an array of parameters I am looking for.
What happens if the parameters parm1 or parm2 do not exist in the passed in parameter string? I see the conditional statement sets the value to the one found or 0 if not found so does that automatically create the parm1 name as well as assigning the value 0?

I am trying to test but just found out my apache installationis not up to date with the new domain name and I have to figure out where the heck I set that value. My redirects are trying to go to the old domain which does not exist. :)


At my age I still learn something new every day, but I forget two others.
 
the parm name is created here:
Code:
$str .= "&$var=$val";
it is the $var

 
OK, I see. But this seems to leave out the rest of the parameters.
My goal is to put parm1=x&parm2=x at the front and then append the remaining parameters to the end of the string.
This way if apps are passing values I will not end up stripping them.

This loop only pulls out values that match the array rather than handling all values. So I would have to alter the loop to include all values and then test the value internal to the loop for a match of the name.

At my age I still learn something new every day, but I forget two others.
 
From reading you original post again, it seems that you are solely concerned about param1 and param2, and you still want to pass everything else along.

To do that:
Code:
<?php
if (!isset($_GET['param1']))
{
    $_GET['param1'] = 0;
}
if (!isset($_GET['param2']))
{
    $_GET['param2'] = 0;
}
?>
<script>
    document.location.href = "?<?php print($_GET); ?>";
</script>

Take Care,
Mike
 
Or all PHP:
Code:
<?php
if (!isset($_GET['param1']))
{
    $_GET['param1'] = 0;
}
if (!isset($_GET['param2']))
{
    $_GET['param2'] = 0;
}
header("Location: " . $s_next_page . "?" . $_GET);
?>

Take Care,
Mike
 
Well, I am close but the code above ends up appending the array not the text. It literally appended the word Array onto the end of the URL.

How can I access the name of elements in the array so I can just do a foreach loop that pulls each name and value?
I can get the values file and using isset will test for a specific name but I need the text of names not specifically tested for.


At my age I still learn something new every day, but I forget two others.
 
Oops, sorry. Do this:
Code:
<?php
if (!isset($_GET['param1']))
{
    $_GET['param1'] = 0;
}
if (!isset($_GET['param2']))
{
    $_GET['param2'] = 0;
}
$s_next_page = "[page name here]?";
foreach ($_GET as $key => $value)
{
    $s_next_page .= $key . '=' . $value . '&';
}
$s_nextpage = substr($s_next_page, 0, strlen($s_next_page) - 1);
header("Location: " . $s_next_page);
?>

Take Care,
Mike
 
and here is a slightly different approach that:
1. doesn't directly modify the superglobal
2. allows you to add other parameters that you are concerned about easily (by adding a parameter to the array - you could also change the function so that you would pass the array in as a paramter, perhaps with defaults as an associative array).

Code:
<?
echo buildquerystring();

function buildquerystring() {
  $vars = array("parm1","parm2"); //the parameters you want to track
  $str = "";
  foreach($_GET as $key=>$val):
	if ($srchkey = array_search($key, $vars) !== false):
		unset($vars[$srchkey]);
	endif;
	$str .= "&$key=$val";
  endforeach;
  foreach ($vars as $var):
    $str .= "&$var=0";
  endforeach;
  $str = "?" . ltrim($str, "&");
  return $str;
}
?>
 
oops - add some brackets as below to make this bulletproof:

Code:
if (  [COLOR=red]([/color]$srchkey = array_search($key, $vars) [COLOR=red])[/color] !== false):
 
That's got it. I just had to move the ampersand to the front rather than the back so I did not end up with an ampersand on the end.

The one thing I did not understand is here:
Code:
foreach ($_GET as $key [COLOR=red]=> $value[/color])

What is the signifigance of the => here?
I had been trying to echo out the value of $key and it always came out with a numerical value. Does the => $value actually split it into it's name/value? Just want to understand so I can make better use of it in the future.

Thanks for the help, this is going to get me much closer to the goal. :)


At my age I still learn something new every day, but I forget two others.
 
think of an array as storing the values in certain slots.

$value = $array[$key]

So, if you store 'test' in array['whats_this'], you get
$key = 'whats_this' and $value = 'test'.

Take Care,
Mike
 
and here is a slightly different approach that:
1. doesn't directly modify the superglobal
2. allows you to add other parameters that you are concerned about easily (by adding a parameter to the array - you could also change the function so that you would pass the array in as a paramter, perhaps with defaults as an associative array).

Well, my approach to the whole project keeps changing as I run into new issues and come up with solutions for them so modifying the parameters may no longer be much of an issue for this specific task but I think the function will come in very handy later on.

This is the same project I believe you were assisting me with using HTACCESS files for redirection.
I never did get it to work correctly for passing anchor tags but have decided that is a small issue that can be worked out later. parse_url does not work well on relative links and I suspect the problem is in that area but I will tackle that at a later date. I am in a push to get this ready for the school to begin using so I have to get it functional today or take tomorrow off work to bang my head on it some more. :)
I have to stop volunteering my time when I have so many other things going on.

Thanks once again for the help. And if you end up going the HTA route with that other problem you are working on I have some code I wrote for a similar purpose doing a lot of read/write to the local system. Some code is jscript and some vbscript.


At my age I still learn something new every day, but I forget two others.
 
I never did get it to work correctly for passing anchor tags

i worked on this some more and concluded that the browser does not pass the anchor to the server but retains it for its own purposes on reload.

the workaround is to write your links so that the internal anchor is a query parameter.

Then, in the redirect, recast the url with the internal anchor in place. then the browser picks it up.

thanks for the offer on the code for hta. i'll post back in the JS forum if i get stuck!
 
I have read varying information on this. The parse_url function takes anchors into account but possibly only when you are parsing a string not actually sent by the browser yet.
I have seen a CGI app that can be combined with PHP to get that information so the info does seem to submit to the server, it just fails to get enacted upon in any way and then is not passed back in the server values. If it were not then the CGI script would not be able to handle it.

Eventually I will figure it out but learning Apache and PHP is enough without piling CGI on top of it. :)


At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top