0

I have this confusing warning massage that I cannot figure out how to solve because I dont know what is the problem. I have my videoator-a.php file where this line of code:

<?php require_once "esse.php";?>
<?php 
  require("common.php"); 
  if(empty($_SESSION['username'])) 
  {    
 header("Location: index.php");    
 }    
?> 

Gets in conflict with the line of code thats in the included file esse.php:

 <option><?php if(isset($_GET['lang'])){echo $_GET['lang'];}else{?>Language<?php } ?></option>

Note that in esse.php there's a line:

session_start();

(Just think this can help) And when I open videator-a.php page it throws a warning:

Warning: Cannot modify header information - headers already sent by (output   started at D:\wamp\www\pollo\esse.php:118) in D:\wamp\www\pollo\videator-a.php on line 6

On line 6 there is:

 header("Location: index.php");

and on line 118 in esse.php there is:

 <option><?php if(isset($_GET['lang'])){echo $_GET['lang'];}else{?>Language<?php } ?></option>  

And if I get modify my videator-a.php file like this:

 <?php 
 require("common.php"); 

header("Location: index.php");    

?> 
 <?php require_once "esse.php";?>

The warning gets away. Why cant I check if a session exists in both php files? What's wrong here? (Sorry, I am a bit of a boiling kettle)

My question is - why the error ever happens. Because it says - header already sent - but I didint sent ny header in esse.php file. Only checked if a session exists and id so - echo it. PLease, help me to solve the problem. Thanks in advance.

Donatta
  • 25
  • 6
  • `header already sent` means an output was send (eg echo). and therefore the headers (default) where set. only possible solution would be using ob_start() functions, because `header("Location: index.php");` works only if no output was released with echo or else. And you should do `exit;` after redirect. – JOUM Sep 18 '16 at 19:46
  • test this file: ` – JOUM Sep 18 '16 at 19:50
  • That helped! I just added the ob_start() function before cehking if session esists. Thank you! – Donatta Sep 18 '16 at 19:55
  • Using `ob_start()` isn't the "*only possible solution*". As the dupe reads, "*it shouldn't substitute for proper application structuring and separating output from control logic*". In other words, if you structure your code properly. you won't need it. And if you're going to use a redirect, why do you need to output anything? They'll be redirected away anyhow. – Qirel Sep 18 '16 at 20:10

1 Answers1

0

Solving the issue with ob_start();

Basiclly place in each file at first

if(!ob_get_level()) { 
   ob_start(); 
   register_shutdown_function(function(){
       if(ob_get_level()) ob_end_flush();
   });
}

But that topic is little more compelx read more here

What's the use of ob_start() in php?

whats the difference between ob_flush and ob_end_flush?

Community
  • 1
  • 1
JOUM
  • 269
  • 2
  • 3
  • The best way I think to solve the problem is to use : echo ""; istead. – Donatta Sep 20 '16 at 09:56
  • @Donatta Not really, its an dirty option, but not a best way in modern programing. Maybe its the best/simplest way for you know. But thing about what first brings you into the situation to use that 'workaround'. You should read about php framworks and how they handle something like this and other stuff – JOUM Sep 20 '16 at 18:43