Solved

$_GET URL variable not working

Posted on 2006-06-15
7
355 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now