how to find out new location from 302 redirection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

how to find out new location from 302 redirection

Post by infomamun »

Hi
Previously I used curl to grab a page of a website. Suppose the url of that page was like this:

http://www.example.com/pageA.php?file=onefile


Recently that site applied a 302 redirection in pageA.php, so that when I try to grab the page from that url it shows me a 302 redirection with new location like this:

Location: /pageA.php?file=onefile?230562

As far I know, if I type this new location into browser's address field or grab that new location by curl, it should be shown by browser or curl should grab it from new location. But watch on the new location string. It has double "?" marks, one as a GET parameter and second one is created as a redirection to the new location. So if I type-

http://www.example.com/pageA.php?file=onefile?230562

on browser's address field or set it as a CURLOPT_URL value, neither the browser shows it nor the curl can grab it as two "?" marks is invalid in a same url. But if I browse the http://www.example.com/pageA.php?file=onefile (without ?230562), browser shows me the content from the new location. So the new location is not invalid actually and browser can identify the new location from header. I think there is a replacement or special meaning of second "?" mark in the new location of 302 message.

Does anyone have any idea what should I write in the browser's address field or as a value of the CURLOPT_URL to grab the page from new location?

Regards
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: how to find out new location from 302 redirection

Post by John Cartwright »

You can simply use

Code: Select all

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
to have cURL automatically follow redirections.
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

Re: how to find out new location from 302 redirection

Post by infomamun »

If I can, it would be best. But unfortunately CURLOPT_FOLLOWCATION can not be activated when safe mode is on or open_basedir is set on and most of the shared and free hosted have either one of those option enabled. That's why I was seeking the new location manually.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: how to find out new location from 302 redirection

Post by John Cartwright »

Ah I didn't even know about cURL's vulnerability with location redirects.

Heres a snippet of code from one of the users comments to give you an idea how deal with this issue. See http://php.net/manual/en/function.curl-setopt.php#71313
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

Re: how to find out new location from 302 redirection

Post by infomamun »

Ow.....You are a very nice helpful guy! You already have searched out a example for me within a quickest time. Thanks for that buddy. Actually I have already read those replies/articles before posting here. But what the example stated in there, do the same thing as I want to do. It extracts header string and there must be a "Location: (new url)" for 301/302/303 redirection from which it will extract the same new location url as I mentioned here. The examples just do that automatically by the regular expression. But here the Location url is little bit more tricky. The 2nd "?" mark is creating problem here and the example will extract the same url as I mentioned here and ultimately will return false/blank page.

Anyway thanks for your quick try for helping me. If anyone know the meaning of 2nd ? mark please let me know. I am sure it has some special meaning otherwise browser couldn't follow the location of new url.

Regards
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: how to find out new location from 302 redirection

Post by John Cartwright »

Can you post a complete set of headers you are trying to parse? There generally should never be 2 "?" in a url, as it has a special meaning to signifiy the beginning of the query string.
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

Re: how to find out new location from 302 redirection

Post by infomamun »

Unfortunately server of the remote site is down today. If it will be Okay, I will give you the header.

Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: how to find out new location from 302 redirection

Post by John Cartwright »

infomamun wrote:Unfortunately server of the remote site is down today. If it will be Okay, I will give you the header.

Thanks
Feel free to post it whenever you want, I'm certainly in no rush :D
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

Re: how to find out new location from 302 redirection

Post by infomamun »

Hi john

I am finding this header from the remote site:
HTTP/1.1 302 Moved Temporarily Content-Length: 0 Location: /?a126e2f0

Here "?a126e2f0" is creating confusion to me? What does it mean here after "/" ? If I write the new location on the browser's address field browser can't go to new location. Same thing happens if I try to grab the new location by curl. But if I put the original address (main address) then browser can go to the new location directly and header comes as 200 OK.

So what secrets lies in ?a126e2f0. Another note is that this value changes after some time and get a new value. but the "?" sign still remains.
Post Reply