-2

I am getting the following error:

Warning: Cannot modify header information - headers already sent by..

Apparently the output is started from line 3 and cannot modify from line 11.

<html>
<body>
<?php
use google\appengine\api\users\User;
use google\appengine\api\users\UserService;
$user = UserService::getCurrentUser();
if ($user) {
echo 'Hello, ' . htmlspecialchars($user->getNickname());
}
else {
header('Location: ' . UserService::createLoginURL($_SERVER['REQUEST_URI']));
}
?>

Any ideas how to solve this?

Tom
  • 41
  • 5

1 Answers1

0

You can't have any output before setting headers. In Your case <html> and <body> tags are already sent when you are trying to set

header('Location: ' . UserService::createLoginURL($_SERVER['REQUEST_URI']));

Your code should be something like:

<?php
use google\appengine\api\users\User;
use google\appengine\api\users\UserService;
$user = UserService::getCurrentUser();
if ($user) {
echo '<html>';
echo '<body>';
echo 'Hello, ' . htmlspecialchars($user->getNickname());
}
else {
header('Location: ' . UserService::createLoginURL($_SERVER['REQUEST_URI']));
}
?>
Bogdan Kuštan
  • 5,427
  • 1
  • 21
  • 30
  • Thank you that does work, kind off. Now when i load the page it goes to a login page (from a previous application maybe?) before I go to this application. The login page isn't part of this application (all of this is in development server). Any ideas? – Tom Mar 01 '15 at 16:33