Jump to content

PHP and querystring problem


skb5525

Recommended Posts

I've inherited some PHP code that is trying to save a querystring to a variable:
 

$from_url = '';

if ((isset($_GET['from'])) && ($_GET['from'] != '')) { $from_url = $_GET['from']; }


The sample URL is: 

https://www.website.com/default.html?from=http://app.website.com/default.asp?utm_source=WelcomeEmail&utm_campaign=welcomeemail

What the code saves to $from_url is:

from=http://app.website.com/default.asp?utm_source=WelcomeEmail

It seems to stop at the "&" after the first querystring item and I understand why (because it parses the URL and runs into the next apostrophe "&" and assumes that it is not part of the "from" parameter).

How do I need to tweak the PHP code to have it save the entire querystring?

In other words, how do I take everything after ..."from=" (i.e. http://app.website.com/default.asp?utm_source=WelcomeEmail&utm_campaign=welcomeemail) and put it into a variable?

Link to comment
Share on other sites

The url in your posted example has two ? args.  That is most likely the problem.  Your script can't tell one url from the containing url so you will have to perhaps break apart the entire string by using an explode on the ? with a limit of 2 results.  Then you can pull the 2nd one apart on the ? in the same way again and then on the &.  The final result will have your "from" arg in the first element of this result.

 

I think.

Link to comment
Share on other sites

Do you have control over the building of the link? Because there should be an ampersand before the 'from' variable (not the question mark), and the value of from should be url-encoded. I'm pretty sure this would take care of your issue. If you don't have control then honestly I'm not sure exactly how you'd fix the issue - maybe parse_url()? Not entirely sure what it does with slightly malformed URLs such as yours, but there's a possibility you can use this and ginerjm's explode() suggestion to figure it out, I guess.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.