r/PHPhelp • • Dec 01 '25

Json encode dando header already sent

Hello, I'm developing a simple CRUD project with PHP, in the part I deliver a Json to the frontend and it displays the data, the Json goes with all the fields as mentioned and the error header already sent the source code of this specific part is this

<?php namespace App\Controller; use App\config\Database; use App\controller\LoginController; use function App\src\connect; if(session_status() === PHP_SESSION_NONE) { session_start(); } class adminController { public function admin() { require __DIR__ . '/../../public/Pagina-adm/admin (1).html'; $pdo = connect(); $sec_control = new LoginController(); $controller = new Database(); $view = $controller->search_id($pdo, "administrator", "adm_id", $_SESSION["adm_id"]); $key = $controller -> fetch_key($pdo, "adm_key", $_SESSION["adm_id"], "adm_id"); header('Content-Type: application/json'); $data = array( 'name' => $sec_control -> decrypt($view['name'], $key['crypto_key']), 'email' => $sec_control -> decrypt($view['email'], $key['chave_crypto']), 'telephone' => $sec_control -> decrypt($view['telefone'], $key['crypto_key']), ); echo json_encode($data); } Does anyone know how to solve it?

2 Upvotes

6 comments sorted by

View all comments

Show parent comment

0

u/Friendly_Nothing_546 Dec 01 '25

Obrigado, agora eu parei pra prestar atenção no erro e ele indica localização na linha 1 do HTML, como eu resolvo isso considerando que quando eu mudo o require de lugar ele não renderiza o HTML e só exibe ele como texto?

3

u/jefrancomix Dec 01 '25

You need to be clear about the output of your controller. HTML or JSON. Requiring html before sending the Content Type header is the root cause of this error.

1

u/Big-Dragonfly-3700 Dec 01 '25

We don't know what your full code is or what output you are trying to produce. You will need to post all the code necessary to reproduce the problem and what the intended output/result should be.