4

I have done my research - and have found multiple solutions to this question, but for whatever reason mine is still not working!

Using IIS to host the site locally redirection works fine, however when I place it on my web server the redirection doesn't work.

Below is a segment of the code from my 'myaccount.php' page.

<?php
session_start();
if ($_SESSION['LoggedIn'] == true)
{    
    // ... Display page... lots of code here
}
else
// Session not active.
{
    // Redirect to login page.
    header("Location: http://{$_SERVER['HTTP_HOST']}/2ndassign/login.php");
}
?>

When that page is hosted online I am simply presented with a blank 'myaccount.php'... no redirection takes place. There is no whitespace present which should cause it to have an error.

So, I've cut my code down to this, so that it's as bare and simple as possible. Error reporting is on so it no longer presents a blank page, but spits out any errors.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
header("Location:http://{$_SERVER['HTTP_HOST']}/2ndassign/login.php");
?>

You can view the site at www.candlesoft.com.au/2ndassign/myaccount.php This might help you in helping me - to see what errors are being spat out.

My guts tell me it's a setting with my hosting provider.

CODE ON PASTEBIN - This is myaccount.php from www.candlesoft.com.au/2ndassign/myaccount.php - It is not an excerpt it is the entire file. (Shows whitespace isn't causing the error) http://pastebin.com/FUHPpT5A


Turns out the fault was that I needed to save the file as UTF-8 (without BOM)... PHP appears to interpret certain characters that BOM generates as whitespace - better explained here: How to fix "Headers already sent" error in PHP Thanks to Billy2mates for the link!

Community
  • 1
  • 1
user2388486
  • 41
  • 1
  • 1
  • 3
  • 1
    `Warning: Cannot modify header information - headers already sent by (output started at /home/candleso/public_html/2ndassign/myaccount.php:1) in /home/candleso/public_html/2ndassign/myaccount.php on line 4` – Mr. Alien May 16 '13 at 05:50
  • make sure that the code is before the `` section – Sumit Bijvani May 16 '13 at 05:53
  • I think you printf("somthing") or echo, you must not output any text before the location header can you show your whole page code till header location? it may be help full. – Nilesh patel May 16 '13 at 05:53
  • 3
    Take a look this answer [stackoverflow headers already sent - solved][1] [1]: http://stackoverflow.com/questions/8028957/headers-already-sent-by-php – Billy2mates May 16 '13 at 05:54
  • Code from pastebin: http://pastebin.com/FUHPpT5A There's no whitespace.. I just don't get it :-(. – user2388486 May 16 '13 at 07:51
  • Billy2Mates, that link solved it. Turns out my problem was UTF-8 With BOM encoding... thanks a heap! – user2388486 May 16 '13 at 08:05

4 Answers4

4

The error describes the issue

Warning: Cannot modify header information - headers already sent by (output started at   /home/candleso/public_html/2ndassign/myaccount.php:1) in /home/candleso/public_html/2ndassign/myaccount.php on line 4

Something is being sent to the browser therefore php can not redirect - even a white space or blank line will cause this issue. Header must be one of the first things sent to the browser.

Eddie Jaoude
  • 1,698
  • 15
  • 23
3

Try ob_start(); and ob_flush();

<?php
ob_start(); 
error_reporting(E_ALL);
ini_set('display_errors', 'On');
header("Location:http://{$_SERVER['HTTP_HOST']}/2ndassign/login.php");
ob_flush();
?>

This will work

1

the header instruction needs to be one of the first instructions because it can't have any html code before it

Tynamo
  • 149
  • 10
  • so you can switch the contents of the if else instructions and change the condition with `$_SESSION['LoggedIn'] == false` – Tynamo May 16 '13 at 06:00
0

Try this Code ,you can check like this also.......................................................

<?php 
session_start();
if ($_SESSION['LoggedIn'] != true)
{    

    // Redirect to login page.
    header("Location: http://{$_SERVER['HTTP_HOST']}/2ndassign/login.php");
}
else
// Session not active.
{

}
?>
user1696874
  • 13
  • 1
  • 4