Jump to content

Passing variable from one site to another


V

Recommended Posts

I'm starting to fear that this is impossible.. :-\ I will severely appreciate any kind of advice!

 

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 code 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)

 

Can passing a variable between two sites be achieved with js.. or XML? I'm willing to use anything as long as I don;t have to add the variable in the URL.

Link to comment
Share on other sites

I'm starting to fear that this is impossible.. :-\ I will severely appreciate any kind of advice!

 

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 code 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)

 

Can passing a variable between two sites be achieved with js.. or XML? I'm willing to use anything as long as I don;t have to add the variable in the URL.

 

Have you looked into sessions?

Link to comment
Share on other sites

You can post it, but it requires js.  Here's something a cooked up a while back (may need error checking and cleanup):

 

function http_post_redirect($url='', $data=array(), $doc=false) {

$data = json_encode($data);

if($doc) {
	echo "	<html><head></head><body>";
}
echo "
<script type='text/javascript'>
	var data = eval('(' + '$data' + ')');
	var jsForm = document.createElement('form');

	jsForm.method = 'post';
	jsForm.action = '$url';

	for (var name in data) {
		var jsInput = document.createElement('hidden');
		jsInput.setAttribute('name', name);
		jsInput.setAttribute('value', data[name]);
		jsForm.appendChild(jsInput);
	}
	document.body.appendChild(jsForm);
	jsForm.submit();
</script>";

if($doc) {
	echo "</body></html>";
}
exit;
}

 

http_post_redirect('http://www.example.com', array('var_to_pass'=>'something'));

Link to comment
Share on other sites

The only information that any site receives with a HTTP request is the information that is contained in that HTTP request, this includes -

 

1) The ip address (taken from the TCP/IP data packets.)

2) The URL (including any folder path, file name, or GET parameters)

3) Any headers in the http request, like cookies.

4) Any data within the request, such as POST and FILES data.

 

If you are redirecting the visitor, then ALL this information would come from the visitor's browser anyway (a header() redirect tells the browser to request the URL in the redirect) and about the only simple/clean thing you can use is a GET parameter as part of the URL. Cookies are out because they are domain specific and javascript on one site cannot set or modify a cookie for a different domain. You could cause a form to be submitted (the action="" attribute would actually cause the new URL on the second site to be requested.)

 

Why don't you want to use a GET parameter?

 

Short answer: Anything you do involving a redirect is through the browser and if your goal is keeping the value of the variable out of the hands of the visitor, no there is no way if you are doing this through the browser.

Link to comment
Share on other sites

I totally agree with this

Anything you do involving a redirect is through the browser and if your goal is keeping the value of the variable out of the hands of the visitor, no there is no way if you are doing this through the browser.

 

If you wish to keep it private then your server will need to make the request, via sockets/cURL etc then return you can display it on your site

Link to comment
Share on other sites

@PFMaBiSmAd thanks for the details! I don't want to do it using GET because the full URL for site B varies. So for example if I try to pass

some_var=4j8

 

via a redirect to site B, site B's URL might be

...siteb.com/?id=334&another_var=189

 

and the result will be

...siteb.com/?id=334&another_var=189?some_var=4j8

 

so I'm not sure if that might cause an error, plus if someone want to share the website's link for instance, I don't want them copying ?some_var=4j8 as well  :-\

 

 

@AbraCadaver thanks for sharing!! Will the script send the var_to_pass to the second site without including it in the url?  :D

Link to comment
Share on other sites

lol sorry MadTechie. If I had a slight idea how to achieve this I would probably know how to better explain it.

 

So I have a link that goes to site A and on site A I have a script that redirects to site B. In the redirect script I have a variable which I wish to send to site B but without putting it in the URL like, www.siteB.com?var=blabla

 

So the first link that triggers everything is a sort of affiliate or shortURL link, thus instead of going directly to site B, it first goes to site A where I have a script and can further manipulate the link.

Link to comment
Share on other sites

okay let me see if i have all the facts correct

You have a 2 sites, Site A and Site B

Now you wish redirect from Site A to Site B while passing some details

 

1. do you have full control over both of them ?

2. are the details sensitive ?

3. how are you getting those details to be sent ?

4. can you give a basic work-flow for example,

5. why can't you put it in the url ?

6. does the visitor end up on site b or stay on site a ?

Link to comment
Share on other sites

@MadTechie

 

1. Yes I fully control site A and just a part of site B that retrieves the variable from site A.

2. Are you referring to the variable? It's just a code from a DB table. Like, "45jk8". Once I register that in site B, I can query WHERE id=$var_from_site_A..

3. The variable from site A to B should be sent in the redirection process.

4. I have a sort of affiliate link, http://www.siteA.com/jkli. On click, using htaccess it runs a redirect.php script which redirects to http://www.siteB.com/blabla..

 

"jkli" from site A is the variable I'm sending to site B (the code is longer but I modified htaccess to hide query strings.)

 

5. I am not putting it in the URL because I want to hide the variable.

6. The visitor ends up on site B after being redirected via site A.

Link to comment
Share on other sites

The only thing i can think of would require you sending a token via the URL on the redirect page,

 

for example:

User is on site A and, and click a link that jumps to a redirect page, this uses fopen or curl or sockets whatever! to make a call to site B with the data to be passed and a token i.e.

<?php
fclose(fopen("http://www.siteB.com/getData.php?token=$token&Details=$Details&ect=$etc", "r"));
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.siteB.com/welcome.php?token='.$token.'">
<title>Re-directing</title>
</head>
<body>
<p>please wait</p>
</body>
</html>';

 

Now getData.php gets the data and saves it to a database/file whatever..

Now when the user is re-directed they are only passing a token so it means nothing to them and site B already has the data

 

 

I hope that makes sense

 

--MadTechie

Link to comment
Share on other sites

MadTechie thank you for the code! I think I'll understand it once I try. :) Right now I was using js, and a hidden form in redirect.php to post the variable to site B

 

 

<body onLoad="submit_form();">
<form name="myform" action="http://www.siteB.com" method="POST">
  <input type="hidden" name="var_to_send" value="some data from the DB"> 
  <p>Redirecting...</p>
</form>

<script language="javascript">
  function submit_form() 
  {
document.myform.submit()
  }
</script>

 

and in site B I just use $_POST['var_to_send'];

 

It seems to work but maybe your technique is better.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.