user1173169
user1173169

Reputation:

Redirect user to another page when he likes a page tab

I had this block of code working but not anymore...

Index.php

<!doctype html>
<?php
require 'php-sdk/src/facebook.php';
$facebook = new Facebook(array(
            'appId' => '453976824647366',
            'secret' => 'fe86f3b0b34b3ed6eXXXXXX',
            'cookie' => true,
        ));

$signedRequest = $facebook->getSignedRequest();
$liked = $signedRequest["page"]["liked"];

        if ($liked) {
            header('Location:youlike.php');
        } else {
            echo "you don't like";
        }
?>
<html lang="en" xmlns:fb="https://www.facebook.com/2008/fbml">
    <head>


    </head>
    <body>

    </body>

</html>

youlike.php

You like!

It always displays "you don't like", seems it never goes into youlike.php

Some help would be appreciated once again!

thx

Upvotes: 1

Views: 383

Answers (2)

Master Drools
Master Drools

Reputation: 738

Try this:

<!doctype html>
<?php

if (!empty($_POST['signed_request'])) {
    list($sig, $payload) = explode('.', $_POST['signed_request'], 2);
    $decoded = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
    $liked = $decoded["page"]["liked"];
}

if ($liked) {
    header('Location:youlike.php');
    exit;
}
else {
      echo "you don't like";
}
?>

Upvotes: 2

Lix
Lix

Reputation: 47976

Because you are in an iframe, you'll need to perform the redirect on the top most frame.

Try this JavaScript -

top.location.href = "youlike.php";

With php you could simply echo out the code like this -

echo '<script type="javascript">';
echo 'top.location.href = "'.$youlike.'";';
echo '</script>';
exit();

Depending on the complexity of your pre/post like pages, you might want to simply have all the content on one page and only display certain parts of it if the user "likes" the page.

if ($liked) {
  // place like post page HTML here
} else {
  // place like pre page HTML here
}

Upvotes: 0

Related Questions