views:

60

answers:

3

I'm having difficulty trying to figure this out. I'm not even sure if it's possible.. I will appreciate any sort of help!!

On site A, I have a link (an affiliate type link) that redirects to site B. When clicking the link on site A, I use this script to redirect..

header('HTTP/1.1 301 Moved Permanently');
header('Location: ' .  $url);
exit;

the $url var is just site's "A" URL. What I can't figure out is how to pass a variable from the redirection script onto site B without using a query string in the URL itself (for example, http://www.siteB.com/?var_to_pass=something)

Also, both sites are on a different server so I'm not sure if sessions will work. But between sites I have a script which I hope I can use someone to achieve what I need.

+2  A: 

There's only one way to pass data between sites via a redirection, just as you're doing, in the URL via query vars. You can't make a browser redirect via POST, so GET's your only option.

Marc B
Sure you can... All you need is a bit of javascript... Render the javascript which will then issue the post request.
ircmaxell
True, but as usual, what if javascript isn't enabled?
Marc B
@ircmaxell Awful hackish idea which fails if the user doesn't have JS
meagar
Well, what I would do is put a form on the page (visible). Then with JS in the head perform the post request. If JS isn't enabled, it'll just show the form (or at least the submit button) and say "Please click here to finish processing" or something like that... Oh, and @meagar I didn't say it wasn't hackish, I just said it was possible...
ircmaxell
You could have a meta-refresh in there to redirect after a couple of seconds if the JS fails. Argh, doesn't this just smack of the netscape days of the internet? :)
Aidan Kane
@ircmaxell will I have to put the js in the header()?
Cyber Junkie
Of course, if JS isn't enabled, then there's no way to do the post. the meta refresh would be a get anyways.
Marc B
@ircmaxell No, you didn't say it was hackish. My point is that you should have. You're offering up bad advice without a "this is bad advice" disclaimer.
meagar
@meagar: It's not bad advice. It's conditional upon the use case. If you REALLY need to post data to an external site through the browser, you can do it. That's the point. There is more than one way to skin a cat. Sure, for 98% of situations one way may be "best", but that doesn't mean the other ways are "bad". They just are dependent on your use case. And considering I can think of a few use cases off the top of my head that would require such a post-back system, I can't see how it's bad advice...
ircmaxell
@ircmaxell You're suggesting a solution that is overly complex and won't work in all browsers. You're moving the simple act of redirecting from the HTTP layer into the HTML layer, and beyond even that into the JS layer. HTTP provides a means of redirecting. Rolling your own is an *awful* idea.
meagar
+1  A: 

You can do it with a POST and javascript but it's not as pretty (or as reliable). Simply have a form that's submitted by the body onLoad event.

You just want to hide the variable from people? You can do this:

www.sitea.com -> (redirect) -> www.siteb.com/incoming?var=blah
# then
www.siteb.com/incoming?var=blah -> (redirect) -> www.siteb.com/

It will happen so quickly that the user won't even see and the end result is that siteb gets the variable from sitea and the user ends up on a clean looking url.

Aidan Kane
That's neat! Thanks :) But will I be able to store `blah` once site b redirects as well?
Cyber Junkie
You'll need to store it manually on your incoming page (in the session or something). That way site b will be able to access it whenever it needs.
Aidan Kane
A: 

Alternatively you can use curl (but note, it won't redirect, if you want to redirect you have to use solutions descrbed above), to send and receive variables from one server to another, just when receive make sure to save your variables somewhere(database)..

Centurion