terungwa Posted September 28, 2014 Share Posted September 28, 2014 (edited) I am trying to do a page redirect using the header() function. I am aware that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. and also that reading code with include, or require, functions, or another file access function, can create spaces or empty lines that are output before header() is called. thus causing a redirect failure. I have therefore used this output control function (ob_start(); ob_implicit_flush() right at the top of my page. I am then creating the redirect like this as part of my login() function: if($user_role == ADMIN_LEVEL || $user_role == EDITOR_LEVEL) { if (!headers_sent($filename, $linenum)) { return header("Location:http://mysite.com/dashboard.php"); exit; } else { echo "Headers already sent in $filename on line $linenum\n" . "Cannot redirect\n"; exit; } } Unfortunately I am still getting this error: Headers already sent in /home/steveoje/public_html/login.php on line 1 Cannot redirect And this is what I have at the top of the login.php page: <?php ob_start(); ob_implicit_flush(); require_once 'includes/config.php'; if(isset($_POST['submit'])) { login($_POST['username'], $_POST['password']); } require_once "includes/header.inc.php" ?> //other html content So why would the redirect fail in this instance? Thanks. Edited September 28, 2014 by terungwa Quote Link to comment Share on other sites More sharing options...
Frank_b Posted September 28, 2014 Share Posted September 28, 2014 (edited) First of all: Not all PHP features are a good practise to use. You will never find ob_start() and other ob_* functions inside my code files. First off all ob_* functions is just caching your output. But besides of that you would not need them if you follow one golden rule: Allways first handle your PHP logic without a single output. On the bottom of your script (i prefer in an other file) you start your output. <?php require_once 'include/config.php'; $message = 'Please login'; if($_SERVER['REQUEST_METHOD'] == 'POST') { $success = login(...); if($success) { header('Location: member.php'); exit; // do not forget to use exit after header('Location: ???'); } $message = 'Wrong credentials'; } include '/path/to/templates/loginform.html.php'; ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Login</title> </head> <body> <?php echo $message; ?> <!-- Login form here --> </body> </html> Edited September 28, 2014 by Frank_b 1 Quote Link to comment Share on other sites More sharing options...
Solution Frank_b Posted September 28, 2014 Solution Share Posted September 28, 2014 Sometimes it happens that you have a BOM (Byte Order Mark) on top of your file(s). http://en.wikipedia.org/wiki/Byte_order_mark You are able to check the files in an hex-editor like for example http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm. If there are a few bytes on top of the file before the content that you wrote this causes output and therefor the header() function will not work anymore. To get rid of it open a new php file in a php edittor, copy and past your code from the old file into the new file and save the new file. (overwrite the old one!) 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.