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.

Edited by ginerjm
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.