Score:0

RuntimeException: Failed to start the session because headers have already been sent by

cn flag

New to Drupal, I just created a new custom module in Drupal 9 to see how it works I was trying to test with a minimal piece of code. I just enable this module and got this error, now I can't open anything and getting this error on every page nothing is happening even if I change anything in code. This is my custom module code.

check_hookcalls.info.yml

name: hook calls checking
description: module for testing hooks
core_version_requirement: ^8 || ^9
type: module

check_hookcalls.module

<?php

function check_hookcalls_node_presave() {
  die;
}

?>

The error I get is the following one.

[13-Jul-2021 15:34:49 Europe/Berlin] RuntimeException: Failed to start the session because headers have already been sent by "C:\softwares\xampp\htdocs\d9_firstexperience\2\modules\check_hookcalls\check_hookcalls.module" at line 10. in C:\softwares\xampp\htdocs\d9_firstexperience\2\vendor\symfony\http-foundation\Session\Storage\NativeSessionStorage.php on line 152

sonfd avatar
in flag
Remove the closing php tag
cn flag
Yes, I just removed the tags, and restarted apache it start working but it's strange. You can post it as an answer I will accept.
apaderno avatar
us flag
Drupal coding standards say not to add the closing PHP tag exactly to avoid that issue.
cn flag
Okay, thank you I have to learn standards.
apaderno avatar
us flag
The [Coding standards](https://www.drupal.org/docs/develop/standards) guide and its sub-guides are a start.
Score:1
in flag

Per the Drupal coding standards, the closing php tag should be omitted.

The PHP Code Tags section of the coding standards (as well as this stackoverflow answer) give a good overview of why it's a good practice to omit them in general:

  • Removing it eliminates the possibility for unwanted whitespace at the end of files which can cause "header already sent" errors, XHTML/XML validation issues, and other problems.
  • The closing delimiter at the end of a file is optional.
  • PHP.net itself removes the closing delimiter from the end of its files (example: prepend.inc), so this can be seen as a "best practice."
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.