How do I redirect links? - php

I have created one application for my portfolio.
Let's say the URL is http://fb.domain.com/about/ and my FB app is http://apps.facebook.com/myap/about/.
A user can access http://fb.domain.com/about/ directly from my site.
If the user types http://fb.domain.com/about/ or clicks http://fb.domain.com/about/, how do I redirect them to http://apps.facebook.com/myapp/about/
Here's an example from http://www.oodle.com.
If you go to this link, you will be redirected to here.
How is that done? Let me know. I currently use php for my site.

You can use ModRewrite in your .htaccess to redirect based on the rules set, which is probably the cleanest solution without involving hardcoding redirects in your code.

Something like this should work.
// Define current URL
$host = $_SERVER['HTTP_HOST'];
$self = $_SERVER['PHP_SELF'];
$query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
$url = !empty($query) ? "http://$host$self?$query" : "http://$host$self";
// If current URL is not Facebook's, redirect to Facebook
if($url == 'http://fb.domain.com/about/'){
header("Location: http://apps.facebook.com/myapp/about/");
exit();
}

Assuming all you need is the simple redirect, no page loading, then this should do it...
header("Location: http://apps.facebook.com/myapp/about/");
exit();

Related

How to retrieve original url in a redirected page using PHP

Let say I have redirect rule on url example.com which redirect to newurl.net.
How can I get original url on newurl.net in order to be used and handled with some details or just to be printed on new destination.
Thank anyone for help.
If it's not external You can use
$_SERVER['HTTP_REFERER']
And it will give you the referer which is what you want.
See Acquiring referral after a PHP redirect for more informations.
But if you are trying to get to another website, the browser will overwrite the headers so you need to do it some other way.
You can store it in session.
//Save it in your first website
$_SESSION['REFERER'] = $_SERVER['HTTP_REFERER'];
And then in the other website :
//Use it in the other
$referer = '';
if (isset($_SESSION['REFERER'])) {
$referer = $_SESSION['REFERER'];
unset($_SESSION['REFERER']);
}
You could also pass the referer as a parameter:
header('Location: http://newurl.net?original_referer=' .$_SERVER['HTTP_REFERER']);

PHP in Google App Engine, how to detect the url of the incoming redirect?

I am not sure if this is possible but was wondering if we can detect the url from where header('Location: ') initiated.
I have a couple of scripts with header('Location: /main.php') and now when main.php is executed I want to find out from which script or URL did the redirect initiate from.
script.php
header('Location: /main.php');
main.php
//detect the source url.. ie /script.php or website.com/script.php
// do some action
header('location: /script.php');
Is this possible?
I think you could use something like this on main.php.
$referer = $_SERVER["HTTP_REFERER"];
I got it to work for me with the following code
script.php
$document = $_SERVER['REQUEST_URI'];
$host = $_SERVER['HTTP_HOST'];
$fullPath = $host.$document;
setcookie('referer', $fullPath,time()+86400); // cookie is destroyed on redirect
header('Location: /main.php?referer='.$fullPath);
and the other one is as below
main.php
$referer = $_REQUEST['referer'];
if($referer != null){
header("location: http://".urldecode($referer));
}
The cookie I set got deleted when main.php was executed, hence I had to send the info over via POST.
Also I had to use urldecode on $referrer.
This may not be the best way to do this but it worked for and if any one can improve this I would be happy to learn from it.

PHP - referred redirect script

I have a tracking link (poly.wtf) .
I'm trying to make a php page that check the referred if and to make this condition if the visitor came from my tracking link (poly.wtf) it will redirect him to page X
if the visitor didn't come from my tracking link (anywhere else) or just type the url through the browser it will redirect him to page Y
I'm tried to use this script PHP :
<?php
$domain = 'poly.wtf';
$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match($domain ,$referrer)) {
header('Location: https://www.google.com/');
} else {
header('Location: https://www.yahoo.com/');
};
?>
but it doesn't work because for some reason it doesn't capture for me the refer here is example of redirect from poly.wtf link to the PHP page :
http://poly.wtf/5d9e31ce-bd7d-4500-bc4e-b7e7f7ddcacc
^^ in this case it should redirect me to google but it doesn't , what do you think is the problem?
You shoul enclose your pattern in some delimiter like:
$domain = '/poly.wtf/';

HTTP_REFERER not working when user comes from HTTP code 301/307

I've written some simple code in PHP which save slug from my 301 redirect URL so I know how many visitors come from that advertisement. However, it's not working when user comes from 301/307.
Example: if user comes from 301 redirect url example.com/example to redirect url to example.com/example1 now in example1 page not showing referral from this link example.com/example:
$url = example.com/example;
$data = "$url";
$j=0;
foreach (count_chars($data, 1) as $i => $val) {
$j=$j+$val;
}
$result = substr(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',$j);
echo $result;
Hope this makes sense.
Dont rely on $_SERVER['HTTP_REFERER'] it is purely optional for a browser to send it, and some security software blocks it in requests. Read more here
Thats a normal problem of Referer Tracking. You should use other methodes to track ads.
The most used option are Get Parameters (offer.php?ref=sitexy)

problem getting redirect to work properly php

I have a little problem with redirecting. Registered users follows this link site.com/reg.php?passkey=1234 but the first the user get redirected to the correct language based on a cookie. I need to keep the passkey variable when the user is redirected. like this ?lang=en_US&passkey=1234
My code so far look something like this:
if (!isset($_GET['lang']))
{
if (isset($_COOKIE['country']))
{
$country = $_COOKIE['country'];
(...)
elseif ( $country == "US" ){
$variables = $_GET;
$variables['lang'] = "en_US";
header('Location: ?' . http_build_query($variables));
exit();
}
This works:
reg.php
reg.php?lang=en_US
reg.php?lang=en_US&passkey=test
reg.php?passkey=test&lang=en_US
but this gives an The page isn't redirecting properly error
reg.php?passkey=test
I don't understand why this doesn't work when all the other combinations seem to work perfectly.
The HTTP 1.1 Specification requires that the location needs to be a Absolute URI
(See RFC2616 14.30 Location)
The location header('Location: ?' . http_build_query($variables)); does not contain an absolute URI.
You need something like:
header('Location: /folder/file.php?'.http_build_query($variables));
If you need to do this on different locantions you can use $_SERVER['PHP_SELF'] to set the current file as redirect location. For example
header('Location: '.$_SERVER['PHP_SELF'].'?'.http_build_query($variables));
I think, you should change the http_build_query($variables) to http_build_query($variables, null, '&')
I hope my answer is useful.

Categories