Solved

$_GET URL variable not working

Posted on 2006-06-15
7
381 Views
Last Modified: 2008-02-01
Greetings...

I've got a weird issue here.  I'm passing a URL in a variable called, URL to another page to be redirected.  For some reason the $_GET function isn't picking up all of the variables.  Here is the URL that is being passed:

URL=http://www.mydomain.com/asp/detail.asp?1=&p=297&o=1

Now here is my code to grab the variable:

$redirect = $_GET['URL'];
header ('Location:'.$redirect);

When you echo $redirect all you get back is:
http://www.mydomain.com/asp/detail.asp?1=

It seems to stop right after the = sign.  Any ideas?  I'm also passing some other variables such as an ID and I can get that just fine.
0
Comment
Question by:cwilkey
  • 3
  • 3
7 Comments
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 16912960
You'll need to urlencode the value for URL before you pass it on to this page.  Otherwise, how does PHP know that those variables belong with the URL variable, and not just a part of the rest of the variables?
0
 

Author Comment

by:cwilkey
ID: 16912990
Yikes...so here in lies the problem.  Not every URL passed will have variables in them.
0
 
LVL 7

Expert Comment

by:aib_42
ID: 16913046
What if you also wanted to pass p=296?

something.php?URL=http://www.mydomain.com/asp/detail.asp?1=&p=297&o=1&p=296 ? Does this look OK to you? Where does the URL end and p start?

You cannot have '&' or other special URL characters inside a variable being passed via GET.
You can make a simple form with
<form method="get" action="something.php"><input type="hidden" name="URL" value="http://www.mydomain.com/asp/detail.asp?1=&p=297&o=1"><input type="submit"></form>
and see how the browser passes the URL to the PHP script.

It will be URL-encoded, as per urlencode(). So if you're manually passing the URL, you should URL-encode it as well.

$foo = urlencode("http://www.mydomain.com/asp/detail.asp?1=&p=297&o=1");
$link = "something.php?URL=$foo&p=296";
echo "<a href=\"$link\">...</a>";

will yield a URL like:

something.php?URL=http%3A%2F%2Fwww.mydomain.com%2Fasp%2Fdetail.asp%3F1%3D%26p%3D297%26o%3D1&p=296

And this time you will be able to use $_GET['URL'] properly.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:cwilkey
ID: 16913132
Creating a form will isn't an option.  I've created a banner management system that passes it's URL to a page so the database can record it's impression/clicks.  It then redirect to the URL based on the domain passed.

So, I tried:

$redirect = urlencode($_GET['URL']);
header ('Location:'.$redirect);

And I got: http%3A%2F%2Fwww.spiritlakehomes.com%2Fasp%2Fdetail.asp%3F1%3D

0
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 125 total points
ID: 16913149
er, no.  You'll need to call urlencode on the page before.  The one that generates URL in the first place.
0
 

Author Comment

by:cwilkey
ID: 16913188
That's it.  That did it.  Thank you sir...or mam!
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 16913253
You're welcome.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question