tags:

views:

91

answers:

3

Hi All,

I am facing a peculiar behavior in php header function.

The code below works fine,

 header("Location: https://www.facebook.com/logout.php?api_key=148814828480550&next=http%3A%2F%2Fmysite.com&session_key=2.HP0X33OURpJRDPfkvWYtAQ__.3600.1285146000-1039666914");

But when I pass URL as variable, it takes me to facebook page but not back to mysite.

$location =  $facebook->getLogoutUrl(array('next' => 'mysite.com'));
header("Location: $location");

I checked contents of $location, it is same url which is working when given as string.

Please advice.

Thanks

+2  A: 

Try to use the following code:-

$location = (string) html_entity_decode($facebook->getLogoutUrl(array('next' => 'mysite.com')));
header("Location: $location");
exit();

Also please check whether all the query string indexes are available as required along with the same values, when using the above code, which are:-

  1. api_key
  2. next
  3. session_key

When using "header()" function, it's necessary to make sure that you are using the "exit()" function just after the "header()" function call.
Also when using this "header()" function, it must be called before outputting any information to the browser (either from PHP or HTML), so it's also better to use the Output Buffering, by calling this function "ob_start()" at the very first line of the web page just after the PHP Start Tag (<?php).

Hope it helps.

Knowledge Craving
I've had similar problems in the past but exit() solved them.
justinl
A: 

It might be something to do with character encoding

Alex
A: 

Not peculiar at all, I forgot to do html_entity_decode($location);

I was echoing the $location, and when I var_dump()'ed it, I found that '&' was actually '& amp;'

Thanks for your answers guys.

Harsha