URL with multiple successive parameter delimiters?

H

Harlan Messinger

Greg said:
> I've got a few php pages that are invoked like this:
>
> http://...domain.../sample.php?parm1=xxx&parm2=yyy&parm3=zzz
>
> Sometimes though, I end up with this:
>
> http://...domain.../sample.php?&parm2=yyy

What do you mean by, "I end up with"?
> ^^
> Although this seems to work as far as I could test it, it is maybe not
> as clean as it should be.
Right.

> I could add some more code to clean it up. My question is, do I have
> to? Why?

It's kind of hard to guess what sort of explanation will convince
someone to fix broken code who doesn't think it's obvious why it's a
good idea! Especially when the fix is as simple as replacing "?&" with
"?". Or, even better, as simple as not building the error in in the
first place. I'm guessing that your original is something like the
following pseudo-code:

url = "http://example.com/sample.php?"
if (shouldInclude(parm1))
url = url + parm1.name + "=" + parm1.value;
if (shouldInclude(parm2))
url = url + "&" + parm2.name + "=" + parm2.value;
if (shouldInclude(parm3))
url = url + "&" + parm3.name + "=" + parm3.value;

It doesn't take much more work to produce:

iIncludedParms = 0;
url = "http://example.com/sample.php"
for each parm in possibleParms
{
if (shouldInclude(parm))
{
if (iIncludedParms == 0)
url = url + "?" + parm.name + "=" + parm.value;
else
url = url + "&" + parm.name + "=" + parm.value;
iParms = iParms + 1;
}
}

This coding pattern comes up again and again (example: insertion of
"AND" when dynamically adding criteria to the WHERE clause in a SQL
command based on a user's filter selection, or, likewise, insertion of
commas when dynamically adding sort columns to the ORDER BY clause), so
you ought to be familiar with it.

By the way, if this URL you are generating is meant to be the value of
an HREF or SRC attribute in the generated HTML, use "&" instead of "&".
 
G

Greg N.

Harlan said:
It's kind of hard to guess what sort of explanation will convince
someone to fix broken code who doesn't think it's obvious why it's a
good idea!

The question is, is it broken?

If you can point to an official specifications document (HTML, Apache,
PHP, or whatever else is relevant here) that explicitly says, "null
strings are not allowed within URL parameters" or something like that,
allright, that would motivate me to mess with it, though the code is not
all mine.

If you can't, I'd prefer to leave it alone as it works nicely as it is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top