pass variable from one site to another - php

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.

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.

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.

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

Related

Need tp find Request source URL in PHP

I want to determine the website from which the request to the particular website has been called. For Example..I have a website www.ex.com. Now this website link has been there on three websites www.a.com, www.b.com, and www.c.com. Suppose one user has clicked the link from www.a.com to go to www.ex.com. Now I want to determine that the request has come from the www.a.com and then the page in www.ex.com will be displayed accordingly. Similarly if the user has clicked the www.ex.com link from www.b.com then the page will be displayed accordingly.
So how can I determine this request source, means from which website www.ex.com has been called? It is good if you will explain this using code in PHP.
Thanks
Look here for HTTP_HOST and HTTP_REFERER.
Since both global variables are populated from HTTP headers, look also here for Host and Referer.
That's how you use them:
$host = $_SERVER['HTTP_HOST'];
$referer = $_SERVER['HTTP_REFERER'];
The 1st is the original host name (like 'www.a.com') referring to your page. The 2nd is the full URL (like https://www.a.com/some/path/to/some.html).
Then, based on some conditional logic, you may return to your users appropriate HTML.
There are two different websites.
Say from a.com, you click on ex.com
So, for the link in a.com, add some parameter:
Click Here
Now, your from parameter will be
1) a.com
2) b.com
3) c.com
If none of the above, then its clicked from ex.com itself.
You can also add some encryption for this link to add security.
You can use this
$_SERVER['HTTP_REFERER']
for referral url

How to create a redirect?

How do I create a php which redirects with use of the URL. I.e. - Redirect-To.com/Change.php?=Google.com Then goes to google etc
I'm wanting to create a php page where a user goes to
Redirect-To.com/Change.php?=Google.com
or
Redirect-To.com/Change.php?=Youtube.com
or
Redirect-To.com/Change.php?=Yahoo.com
Then the visited page redirects to Google or whatever page is after ?=
This is basically what URL shorteners do, except they lookup the destination from a database and probably store some tracking information.
Check out the header function that lets you set HTTP headers. You want to set a 302 redirect, which means moved temporarily, and a Location header to set the new location.
http://php.net/manual/en/function.header.php
header('HTTP/1.1 302 Moved Temporarily');
header('Location: ' . $newLocation');
301 is another common redirect code, but it means "Moved Permanently" and a lot of browsers will cache that status and not hit your website if the person clicks your link again.
The HTTP spec says you should give the full URL with the redirect, though most browsers will work without it.
You may also want to white list destinations; a user might follow a link to your site and be redirected to a site owned by a a malicious third party. Thinking they were in the confines of your site, they may enter information or perform actions allowing the third party to gain access to their data.
In the URL, values without keys will be ignored, so you should write:
...change.php/?redirect=http://www.google.com
Then you can access the "redirect to" address with $_GET['redirect'].
To redirect to a given address, you can use to Location header (make sure nothing is sent to the output before calling the function):
header('Location: ' . $_GET['redirect']);
You have to start the redirecting URL with "http(s)://" like I wrote above.
If you want to stop the script (as it does not stop immediately after the redirect function), you have to call the die() or the exit() function.
(If a shorter URL is better for you, the URL can be just ...change.php/?http://www.google.com, then you can access the address with $_SERVER['QUERY_STRING'].)

Get url of the page in php

I am working on "Email this page" Popup page. I want to send url of base page as an email, but it should be a popup window.
I have used HTTP_REFERER, it is working fine on Firefox, but not working on Internet Explorer.
I am getting the url of current page but I want that url in new popup window page.
Is there any alternative than HTTP_REFERER.
On the page you wish to grab the URL of, you can use $_SERVER['REQUEST_URI'] to get the requested URI (except the scheme & hostname; in other words, you get the path and query string). Pass this to your other page either using a query string or sessions. The former is preferable, as the latter isn't RESTful. There may be times when it's OK to break REST's rule against server side state, but this probably isn't it.
There is no way unless you store it or send it yourself. This page has one example of how to do it, but only really if you set it beforehand. If the site is your own then you should be ok. If not then you will struggle.
That happens because the HTTP_REFERER is sent by the client browser, which means that it's value can be totally manipulated or can even be null. This means that this variable isn't very reliable. But if the site is yours, there are other solutions.
You can send the url or any other identification like an ID by QueryStrings. So you'll have the link URL like this the_send_page_name.php?ref=index.php
Be aware that this method only works if you're opening the Pop-up in a site that's yours.

POST in PHP AND redirect the user to that page?

I'm a bit confused. How do I POST data to a URL and then redirect the user's browser to that location - all in one operation?
I see
header('Location: page.php?' . http_build_query($_POST));
but that is GET, not POST and ppl think thats really bad practice - PHP open another webpage with POST data (why?)
My kludgy workflow involves setting up a form and then submitting it via javascript - anything has to be better than that...
I think I can do a set of header() stmts but this action happens for the user way after the page has been geenrated, so i dont think that would work
You cannot redirect POST requests. As simple as that. Any redirect will always turn into a GET request.
If you want to receive POST data, then send that data to another page, you have two choices:
if both pages are on the same server, use sessions to save the data server-side, don't make the client carry it over
if the destination is on another server and you need to send the client there together with the data, set up another intermediate form like you are
Use AJAX to save the data before you leave the page. Use the answer you get back to fire a redirection to the new url right within the current page. Don't be affraid of Javascript and ajax. Try this light AJAX library: http://www.openjs.com/scripts/jx/

PHP 301 Redirect - collect referrer

I'm doing a 301 redirect on site A to site B - when the user arrives at site B it needs to find the page the user came from. This doesn't seem to be working though:
$_SERVER['HTTP_REFERER']
whereas if I do a link to the page
go
I get the referrer through. Is there a reason it doesn't come through after the redirect? If so can anyone offer any advice on how to do this. I want to avoid at all costs having a query string on the redirect.
Is there maybe another header I need to send with the page that redirects?
Thanks for any advice!
The thing is, the HTTP_REFERER is site A. That's just how a 301 works.
That said, the easy way to do this is to take the url of the referrer to site A onto the end of site B's URL as a variable. Then, at site B, any time you have a referral from site A, you can have it.
<?php
header("Location: http://site-b.com/?ref="
.urlencode($_SERVER['HTTP_REFERER']),TRUE,301);
?>
Then of course at site B, access urldecode($_GET['ref']) for your referrer.
However... if you're looking to avoid _GET variables, you have a few options.
A) Collect the _GET request, then re-munge the URL -- IE have site B redirect to a "clean" version of itself.
B) Have your redirecting page make a curl or stream_get_contents over to a "collection" page prior to issuing a header(), where you collect and store the any session information (like the refererer) and have it prepared to be processesed when they redirect.
You could try adding a CGI query string to the end of your URL when doing the redirect -- eg
http://www.site-b.com?redirectfrom=www.site-a.com
site-b.com would ignore the URL parameter, but it would be recorded in the logs and would be accessible from within PHP.
You can do it with javascript. Use the following script, but it between <head> and </head>
<script type="text/javascript">
location.href='http://www.site-b.com';
</script>
This will of course not make a proper HTTP 301 redir, but I just tested it, and it will send the referer (the referer being site-a).

Categories