Jump to content

Retrieve a whole URL via GET (or is there a better way?)


closet geek

Recommended Posts

Hi,

I have searched but I can't find the answer to this, apologies if it has been asked before. I have urls such as this:

mysite.com/goto.php?id=http://theirsite.com/file.php?test123&move=123?123

how do I use PHP to retrieve all of: http://theirsite.com/file.php?test123&move=123?123 - it seems to trip up over ? and & and therefore using $_GET['id'] I can only receieve http://theirsite.com/file.php

Thanks,

cg
[quote author=kc9ddi link=topic=116598.msg475138#msg475138 date=1164733700]
A couple things you might try:

1) Escape the ? and & signs in the URL
2) Use the REQUEST_URI or other variable in $_SERVER and regexp-type functions to extract the correct portion of the URL
[/quote]

Thanks for your reply.

1) I can't really do this, the first time PHP comes across the URL is via my $_GET command and by then it has already lopped off some of it! That's really my whole problem :)

2) I don't want a portion of the URL, I want the whole thing, http and all - using my example I want *everything* after id= to be available to me in PHP as one variable.

cg
[quote author=doni49 link=topic=116598.msg475172#msg475172 date=1164737067]
Loop through the $_GET array and rebuild it (be sure and check it for dangerous/malicous code).
[/quote]

Don,

Could you hold my hand a bit more - I really don't know my PHP from my elbow. Could you give me an example of how to do what you have suggested?

Thanks,

cg
heres what i use to return the full url... its quick... and simple... also listens to querys...
[code]
<?
function get_url(){
if(!empty($_SERVER[QUERY_STRING])) return 'http://'.$_SERVER[SERVER_NAME].$_SERVER[PHP_SELF].'?'.$_SERVER[QUERY_STRING];
else return 'http://'.$_SERVER[SERVER_NAME].$_SERVER[PHP_SELF];
}

echo get_url();
?>
[/code]
[quote author=Orio link=topic=116598.msg475205#msg475205 date=1164738930]
@The Little Guy
@taith
But that's not what he/she was asking for...

Orio.
[/quote]
Are you sure?

It seems like it to me.

This url doesn't "trip up", and it is displayed on the page. To me, he/she is asking if there is a way to get the whole URL, and that is what I posed does, and you can see from the link below:
http://tzfiles.com/phpcode/echo_url.php?123123=2383&name=123?123
[quote author=Orio link=topic=116598.msg475181#msg475181 date=1164737477]
Can you show some code? That'll be easier for everyone...

Orio.
[/quote]

My code is quite simply:

[code]
$domain = $_GET['id'];
echo $domain;
[/code]

The Little Guy: I tried your code, it seems to return whatever is in my url bar, it might be possible to hack this solution to work. The URL I actually want to capture however is everything after id= (as I mentioned in my original post) the problem with your solution is that it simply captures what is in the URL bar however I am using mod_rewrite behind the scenes. Basically what appears in the URL bar doesn't contain id=, I hide this from the end user.

taith: Your solution grabs the rewritten URL (but + my site's url which I don't want) which is nice, but very strangely it changes only one ? into a & - however it does this only once!! If I had more ? into the URL in random places it doesn't change those into &. I don't fancy debugging this (you can see with my skill level this could be nigh on impossible).

Orio is right, I actually only want to retrieve everything that is after id=

Thanks for all the replies so far!

cg
@little guy-
Look again-
mysite.com/goto.php?id=http://theirsite.com/file.php?test123&move=123?123
The domain is mysite.com, but the passed value's domain is theirsite.com

@closet geek- The problem is how you pass the value. You cant pass something like that because the server misunderstands you. You should have a look into [url=http://www.php.net/manual/en/function.urlencode.php]urlencode()[/url]. The value after id= must be encoded.
Example-
1) Before encode (server won't understand):
[code]http://theirsite.com/file.php?test123&move=123?123[/code]
2) After encode (server will understand and decode automaticly):
[code]http%3A%2F%2Ftheirsite.com%2Ffile.php%3Ftest123%26move%3D123%3F123[/code]


Orio.
Orio,

I'm confused by your suggestion in two ways:

1) If that's the case, how did The Little Guy's code manage to retrieve the whole URL correctly (although including the bit I didn't want!)?

2) Would this mean when a user sees the link it would show the encoded URL? This is not desirable from my pov...

Thanks,

cg
[quote author=craygo link=topic=116598.msg475252#msg475252 date=1164741923]
Are these some strings you have and want to break them up or are you doing this on the fly while browsing with IE or firefox.

Ray
[/quote]

On the fly, when a user clicks on a link on my site it gets outputted as mysite.com/goto=http://linktheyclickedon.com, that gets rewritten by mod_rewrite to: mysite.com/goto.php?id=http://linktheyclickedon.com I then have a php script to retrieve http://linktheyclickedon.com (or in other words eveerything after id= in the rewritten url) however this is where I am becoming unstuck as I can't retrieve URL's after id= that contain & ? and other symbols.

cg
That's what I was explaining...

[quote author=Orio link=topic=116598.msg475225#msg475225 date=1164740213]
The problem is how you pass the value. You cant pass something like that because the server misunderstands you. You should have a look into [url=http://www.php.net/manual/en/function.urlencode.php]urlencode()[/url]. The value after id= must be encoded.
Example-
1) Before encode (server won't understand):
[code]http://theirsite.com/file.php?test123&move=123?123[/code]
2) After encode (server will understand and decode automaticly):
[code]http%3A%2F%2Ftheirsite.com%2Ffile.php%3Ftest123%26move%3D123%3F123[/code]
[/quote]

If the URL has the & sign in it, it must be encoded!

Orio.
[quote author=craygo link=topic=116598.msg475258#msg475258 date=1164742351]
So your url will always be id=xxxxxx and thats it. You won't have other parameters in there??

Ray
[/quote]

I don't fully understand what you are asking? Yes, the URL will always be of the form:

http://mysite.com/goto.php?id=http://anotherurl.com/couldbesomethinghere/mightbeafilehere.php?blah&123

Of course the vast majority are simply:

http://mysite.com/goto.php?id=http://anotherurl.com/

which my code of:

$domain = $_GET['id']

has no problems with dealing with, it's those longer more complicated URL's with £ and &'s in them that don't work with the $_GET method I currently use.

Thanks,

cg
Here is what you need to do:

convert all & to: %38 (after the [b]id=[/b])
convert all ? to: %63 (after the [b]id=[/b])
then you can send it to the address bar, then you can retrieve it and do what you want:

[code]<?php
$page = $_GET['id'];

header("Location:".$page);
?>[/code]
[quote author=The Little Guy link=topic=116598.msg475269#msg475269 date=1164742747]
Here is what you need to do:

convert all & to: %38 (after the [b]id=[/b])
convert all ? to: %63 (after the [b]id=[/b])
then you can send it to the address bar, then you can retrieve it and do what you want:

[code]<?php
$page = $_GET['id'];

header("Location:".$page);
?>[/code]
[/quote]

I guess if this is the only way I will have too - however in your first code example I was able to successfully retrieve everything after id= without having to do what you have just suggested. In a way the only problem with your code was that it also retrieved http://mysite.com/goto.php?id= also! Surely there is a way to cut that part off in PHP?

Much obliged,

cg
I made this form to test and it worked fine for me
[code]<?php
if(isset($_GET['id'])){
$domain = "".$_GET['id']."";
header("Location:$domain");
} else {
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method=GET>
<input type=text name=id />
<input type=submit value=submit />
</form>
<?php
}
?>[/code]
now if I put my url in
http://hlstats.theelders.net/index.php?mode=players&game=css2
I get redirected no problem.

Ray
I changed the ? in the url to three pipes (since that isn't common in a url), then I replace it with a question mark.
Click Link:
http://tzfiles.com/phpcode/entities.php?id=http://d-top.org/display.php|||artist=orgy
<?php
$page = $_GET['id'];
$replace = array('|||', '||||');
$value = array('?','&');
echo str_replace($replace,$value,$page);
?>
Ray,

I'm not sure how to apply what you've given to my situation? I copied your code exactly and when I go to:

http://mysite.com/goto.php?id=http://test.com/test.php?123&22

I simply get redirected to:

http://mysite.com/test.php

Which isn't what I want at all!  :-[

cg
I changed the ? in the url to three pipes (since that isn't common in a url), and changed & to thee ^^^ then I replace it with a question mark.
Click Link:
[url=http://tzfiles.com/phpcode/entities.php?id=http://d-top.org/display.php|||artist=orgy^^^title=candyass]http://tzfiles.com/phpcode/entities.php?id=http://d-top.org/display.php|||artist=orgy^^^title=candyass[/url]
[code]<?php
$page = $_GET['id'];
$replace = array('|||', '^^^');
$value = array('?','&');
echo str_replace($replace,$value,$page);
?>[/code]
[quote author=The Little Guy link=topic=116598.msg475290#msg475290 date=1164744777]
I changed the ? in the url to three pipes (since that isn't common in a url), and changed & to thee ^^^ then I replace it with a question mark.
Click Link:
[url=http://tzfiles.com/phpcode/entities.php?id=http://d-top.org/display.php|||artist=orgy^^^title=candyass]http://tzfiles.com/phpcode/entities.php?id=http://d-top.org/display.php|||artist=orgy^^^title=candyass[/url]
[code]<?php
$page = $_GET['id'];
$replace = array('|||', '^^^');
$value = array('?','&');
echo str_replace($replace,$value,$page);
?>[/code]
[/quote]

The problem with your solution is that I'd have to change the URL's so people would see ||| and ^^^ etc I'd like to concentrate on your initial solution:

[code]
$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
[/code]

This successfully echo's the full url, not matter what characters it has in it. Now how do I remove my url from this variable so I'm only left with what is after id= (or in another scenario what is left after transfer= ).

Thanks!

cg

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.