PHP header redirect not working

I did my research - and found some solutions on this, but for some reason mine still doesn't work!

Using IIS to host a local site redirect works fine, however, when I put it on my web server, the redirect does not work.

Below is a snippet of 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 this page is hosted on the net, I just get an empty "myaccount.php" ... redistribution does not occur. There is no space that should lead to an error.

So, I have reduced my code to this so that it is as simple and simple as possible. Error reporting is enabled, 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 browse the site at www.candlesoft.com.au/2ndassign/myaccount.php. This can help you help me - see what errors spit out.

My courage tells me this with my hosting provider.

CODE ON PASTEBIN is myaccount.php from www.candlesoft.com.au/2ndassign/myaccount.php. This is not an excerpt, this is the whole file. (Indicates that spaces do not cause errors) http://pastebin.com/FUHPpT5A


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

+4
source share
4 answers

The error describes the problem.

 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 sent to the browser, so php cannot redirect - even in the case of a space or an empty line, this will lead to this problem. The title should be one of the first sent to the browser.

+4
source

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(); ?> 

It will work

+3
source

the header instruction must be one of the first commands because there cannot be any HTML code in front of it

+1
source

Try this code, you can also check it 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. { } ?> 
0
source

Source: https://habr.com/ru/post/1481098/


All Articles