0

I'm using this code to get the current page URL:

<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>

On the website, the result is like you see in the image below.

enter image description here

But when I use this inside the Facebook app it gives me the same result instead of giving me the app URL, as you can see in the image below.

enter image description here

How do I get the Facebook app URL inside the app page?

Jacob Budin
  • 9,753
  • 4
  • 32
  • 35
Youssef Subehi
  • 2,790
  • 1
  • 18
  • 20

2 Answers2

0

Client-side

The more obvious method is to use JavaScript, since your Web server has no awareness of the <frame> itself. But unless you're accessing a frame whose parent frame is hosted on the same domain, the child cannot access (or manipulate) the parent frame. Allowing you to do so would violate JavaScript's same-origin policy.

If they're on the same domain, you can find the URL like so:

window.parent.location // Location object
window.parent.location.href // "http://..."

A similar question's answer recommends using document.referrer to see whether the request is from Facebook; but that doesn't tell you the full URL. So it's not useful if you had multiple app IDs pointing to the same Facebook app. In that case, add a query string parameter to the Facebook Page Tab URL and base your app's content on that query string parameter.

Server-side (PHP)

For example, in Facebook, add a query string ?referrer=appid123 to the Page Tab URL. In PHP, you can check against the query string parameter like so:

if($_GET['referrer'] == 'appid123'){
    // Do something for this visitor, who has come via my Facebook app
}
Community
  • 1
  • 1
Jacob Budin
  • 9,753
  • 4
  • 32
  • 35
0

i have solved it by using this code to check if the user whose viewing this website is inside facebook app page or inside the website it self.

<?php 

$is_fb =   ( $_SERVER["SERVER_PORT"] !== 80 ) ? "".$_SERVER["SERVER_PORT"] : "";

if ($is_fb == 80) {
    /// The User is inside the website
    $InsideFacebook = '0';
}else{
    /// The User Inside Facebook app page
    $InsideFacebook = '1';
}

?>

This code is doing the following

If the user inside website http://my-website.com/website/ the port will be 80

If the user is on Facebook App Page the port will be different than 80

Youssef Subehi
  • 2,790
  • 1
  • 18
  • 20