heinkasner Posted June 26, 2012 Share Posted June 26, 2012 I have this code snippet: echo "<script language='javascript'>alert('FreeExam database backed up successfully');</script>"; header('Location: Admin.php'); exit(); When I take the first line out, it works fine, but when running this, it gives me the following error: 'Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\FreeExam\Selector.php:209) in C:\xampp\htdocs\FreeExam\Selector.php on line 210' . I need to display a message to the user before redirecting to the Admin.php page. Any help would be greatly appreciated!! Quote Link to comment https://forums.phpfreaks.com/topic/264821-headers-already-sent/ Share on other sites More sharing options...
Pikachu2000 Posted June 26, 2012 Share Posted June 26, 2012 See the sticky topic at the top of this forum titled, "HEADER ERRORS - READ HERE BEFORE POSTING THEM". Quote Link to comment https://forums.phpfreaks.com/topic/264821-headers-already-sent/#findComment-1357139 Share on other sites More sharing options...
jcanker Posted June 26, 2012 Share Posted June 26, 2012 Long story short: You can't use headers if you output ANYTHING first. That echo statement in the beginning is output. (And Pikachu2000 has helped me a million times on this board, it seems. Best to listen to his advice ) Quote Link to comment https://forums.phpfreaks.com/topic/264821-headers-already-sent/#findComment-1357146 Share on other sites More sharing options...
The Little Guy Posted June 26, 2012 Share Posted June 26, 2012 (And Pikachu2000 has helped me a million times on this board, it seems. Best to listen to his advice ) And yet you only have 204 posts You could try something like this: header("Refresh: 5; Admin.php"); echo "<script language='javascript'>alert('FreeExam database backed up successfully');</script>"; Quote Link to comment https://forums.phpfreaks.com/topic/264821-headers-already-sent/#findComment-1357172 Share on other sites More sharing options...
Andy-H Posted June 26, 2012 Share Posted June 26, 2012 (And Pikachu2000 has helped me a million times on this board, it seems. Best to listen to his advice ) And yet you only have 204 posts You could try something like this: header("Refresh: 5; Admin.php"); echo "<script language='javascript'>alert('FreeExam database backed up successfully');</script>"; Post count is not directly proportional to skill, knowledge or number of times a user has been helped, or has helped others. As you've proven time and time again Quote Link to comment https://forums.phpfreaks.com/topic/264821-headers-already-sent/#findComment-1357173 Share on other sites More sharing options...
Andy-H Posted June 26, 2012 Share Posted June 26, 2012 Personally, I wouldn't do it that way, I'd do this: // do whatever you need to do header('location: Admin.php?backup=true'); exit; //Admin.php <?php if ( isset($_GET['backup']) ) { // do whatever? $backupMessage = 'FreeExam database backed up successfully'; } ?> // html <?php echo isset($backupMessage) ? $backupMessage : ''; ?> Quote Link to comment https://forums.phpfreaks.com/topic/264821-headers-already-sent/#findComment-1357176 Share on other sites More sharing options...
heinkasner Posted June 27, 2012 Author Share Posted June 27, 2012 The Little Guy, thank you for this piece of code. I understand why the header is above the echo statement, but it still does not redirect to the Admin page. I don't get an error, but rather just a blank page which is the page I redirect from. Quote Link to comment https://forums.phpfreaks.com/topic/264821-headers-already-sent/#findComment-1357327 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.