Jump to content

PHP and querystring problem

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: 


What the code saves to $from_url is:


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?

Share this post

Link to post
Share on other sites

Execute the following.  Isn't utm_source and utm_campaign defined?


Also, try the following, and you might be able to use one of the values.


PS.  A shortcut for you if you are using PHP7.



Share this post

Link to post
Share on other sites
Posted (edited)

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

Share this post

Link to post
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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


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.