0

I am getting this error:

An error Occured in script '/home/path/to/file/php' on line 35:Cannot modify header information - headers already sent by (output started at /home/path/to/file/to/header.html:7)

I understand that I am getting the error when I try to set a cookie, and I know that the setcookie can't have any output before it... the problem is there, I have to add the include file for database connection and redirection of file. So I have added the setcookie after the include file. I also tried putting the setcookie above the include file, but it doesn't also help.

My Code

<?php # Script 2.5 - main.inc.php 2 3        

// Redirect if this page was accessed directly:     
if (!defined('BASE_URL')) {       

// Need the BASE_URL, defined in the config file:       
require('../includes/config.inc.php');       

// Redirect to the index page:       
$url = BASE_URL . 'index.php?p=vote';      
header ("Location: $url");       
exit;    

}   

$vote = new Vote;
//get vote and options data
$voteData = $vote->getVotes();


//if vote is submitted
if(isset($_POST['voteSubmit'])){
$votesData = array(
    'vote_id' => $_POST['voteID'],
    'vote_option_id' => $_POST['voteOpt']
);
//insert vote data
$voteSubmit = $vote->vote($votesData);
if($voteSubmit){
    //store in $_COOKIE to signify the user has voted
    setcookie($_POST['voteID'], 1, time()+60*60*24*365); //Error Occurs here

    $statusMsg = 'Your vote has been submitted successfully.';
}else{
    $statusMsg = 'You cannot vote more than once.';
}


}
?>

//The html form code goes here

I have also tried

setcookie($_POST['voteID'], 1, time()+60*60*24*365, '\');

and

setcookie($_POST['voteID'], 1, time()+60*60*24*365, '\', domainName);

But these also didn't solve my problem.

Also I have created a new file named it as test.php and tested the following code.

<?php 

    $cookie = 'test';
    $cookie_name = "cookieName";
    $cookie_value = $cookie;
    setcookie($cookie_name, $cookie_value, time() + (86400 * 365));

    if((isset($_COOKIE[$cookie_name])) && ($_COOKIE[$cookie_name] != "")) {
     echo 'yes';
    }
    else{
     echo 'no';
    }

    phpinfo();

?>

It prints the yes

And the same code when I tried in my main php file than it displays the error and also prints the yes. I didn't know what's wrong with my code.

I have also tried googling but didn't got any luck.

ADyson
  • 57,178
  • 14
  • 51
  • 63
Sushank Pokharel
  • 869
  • 7
  • 15
  • read the error, it says output started at `/home/path/to/file/to/header.html`. You can't set headers or cookies after outputting content, you need to change the structure of your code. – Lawrence Cherone Dec 06 '19 at 07:13
  • please check the php version of live server it has lower version please update it – PHP Geek Dec 06 '19 at 07:16
  • @PHPGeek how would the PHP version affect this error, specifically? It's unclear, from your comment. If you know something about it, please explain why it's an issue. Then we can all learn from it. Or is it just a guess? – ADyson Dec 06 '19 at 07:30
  • Sushank: header.html on line 7 is where it starts outputting data to the request body. Once you start outputting data, you can't output any more headers (setting a cookie is done initially by setting a header in the response, which the browser then uses to get the cookie information). It's unclear whether the code you've shown comes from header.html, or from another file, or where header.html is included in your script. But you need to ensure that you set all your cookies (and other headers) before you output any other content. – ADyson Dec 06 '19 at 07:34
  • Failing that, follow the instructions in the link I gave above, to look for other possible causes of the problem. Often it's something unintended like an accidental blank space at the start of a file, or something like that. – ADyson Dec 06 '19 at 07:34
  • @ADyson my index page decides where to redirect so i ma calling my index page. Also the header page contains only the header part and config file will contain the database connection part. So I have to include both the header and config file on index file. And I am calling my index file on page so if page is called directly it will go to index page and call all the header and database part. So i am checking the link you have provide. I will let you know if it will solve my problem or not. – Sushank Pokharel Dec 06 '19 at 07:40
  • 1
    @SushankPokharel you can make use of **output_buffering** in php.ini. as headers are already sent above you need to hold output before sending it to client. – jit Dec 06 '19 at 07:57
  • @jit Thank you. output buffering solve my problem. I don't know much about the output buffering. I will take a look about it now. – Sushank Pokharel Dec 06 '19 at 08:49
  • 1
    @ADyson Thank you for the link. Your link also contain much more but the output buffering solve my problem. I will take a look and learn more about the output buffering. – Sushank Pokharel Dec 06 '19 at 08:53

1 Answers1

0

you can make use of output_buffering in php.ini. as headers are already sent above you need to hold output before sending it to client.

jit
  • 1,616
  • 3
  • 21
  • 49