{"id":35148,"date":"2024-05-23T10:20:31","date_gmt":"2024-05-23T13:20:31","guid":{"rendered":"https:\/\/www.locaweb.com.br\/ajuda\/?post_type=ht_kb&#038;p=35148"},"modified":"2025-07-15T11:00:54","modified_gmt":"2025-07-15T14:00:54","slug":"header-php","status":"publish","type":"ht_kb","link":"https:\/\/www.locaweb.com.br\/ajuda\/wiki\/header-php\/","title":{"rendered":"Header PHP: como usar corretamente a sua fun\u00e7\u00e3o"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">A <\/span><b>fun\u00e7\u00e3o header do PHP<\/b><span style=\"font-weight: 400;\"> \u00e9 uma poderosa ferramenta que permite aos desenvolvedores enviar cabe\u00e7alhos HTTP brutos para o cliente. \u00c9 essencial para controlar o fluxo de dados entre o servidor e o navegador, e pode ser usada para diversas finalidades, como redirecionamento de URLs, defini\u00e7\u00e3o de cookies, ou controle de cache.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Neste<\/span> <span style=\"font-weight: 400;\">artigo, exploraremos como usar corretamente a fun\u00e7\u00e3o header para garantir que seu site ou aplica\u00e7\u00e3o web se comporte da maneira esperada, evitando problemas comuns e aproveitando as melhores pr\u00e1ticas de programa\u00e7\u00e3o.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Vamos mergulhar nos detalhes t\u00e9cnicos e pr\u00e1ticos para dominar o uso dessa fun\u00e7\u00e3o crucial no desenvolvimento web com <\/span><a href=\"https:\/\/www.locaweb.com.br\/blog\/temas\/codigo-aberto\/php\/\"><b>PHP<\/b><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">O que \u00e9 &#8220;header location&#8221; e como funciona?<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">O \u201cheader location\u201d \u00e9 um cabe\u00e7alho <\/span><a href=\"https:\/\/www.locaweb.com.br\/blog\/produtos\/registro-de-dominio\/http-ou-https-entenda-as-diferencas-e-beneficios-entre-os-dois\/\"><b>HTTP<\/b><\/a> <span style=\"font-weight: 400;\">utilizado na fun\u00e7\u00e3o header do PHP para redirecionar o navegador para uma nova URL. Quando voc\u00ea envia este cabe\u00e7alho para o navegador, ele automaticamente redireciona o usu\u00e1rio para o endere\u00e7o especificado. A fun\u00e7\u00e3o header deve ser chamada antes de qualquer sa\u00edda de conte\u00fado, como HTML ou texto, para evitar erros.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Aqui est\u00e1 um exemplo de como usar o \u201cheader location\u201d:<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">PHP<\/span><\/h3>\n<pre><span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ Redireciona o navegador para a p\u00e1gina 'nova_pagina.php'<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Location: nova_pagina.php');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">exit; \/\/ Encerra a execu\u00e7\u00e3o do script para garantir o redirecionamento<\/span>\r\n\r\n<span style=\"font-weight: 400;\">?&gt;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">No exemplo acima, a fun\u00e7\u00e3o header envia o cabe\u00e7alho \u201cLocation: nova_pagina.php\u201d para o navegador, que ent\u00e3o redireciona o usu\u00e1rio para \u2018nova_pagina.php\u2019. O exit \u00e9 usado ap\u00f3s o header para interromper a execu\u00e7\u00e3o do script e garantir que o redirecionamento ocorra sem enviar mais sa\u00eddas ao navegador.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00c9 importante notar que se o cabe\u00e7alho \u201cLocation\u201d for enviado ap\u00f3s a sa\u00edda de conte\u00fado, um erro ser\u00e1 gerado, pois os cabe\u00e7alhos HTTP devem ser enviados antes de qualquer conte\u00fado.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Qual a sua fun\u00e7\u00e3o?<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">A fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header<\/span><span style=\"font-weight: 400;\"> no PHP \u00e9 usada para enviar cabe\u00e7alhos HTTP brutos para o navegador do cliente. \u00c9 uma parte essencial do protocolo HTTP, que permite ao desenvolvedor controlar aspectos como redirecionamento de p\u00e1ginas, defini\u00e7\u00e3o de cookies, controle de cache, entre outros.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Por exemplo, o cabe\u00e7alho <\/span><span style=\"font-weight: 400;\">Location<\/span><span style=\"font-weight: 400;\"> \u00e9 frequentemente usado para redirecionar o usu\u00e1rio para uma nova URL.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00c9 importante lembrar que a fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header<\/span><span style=\"font-weight: 400;\"> deve ser chamada antes de qualquer sa\u00edda de conte\u00fado, como HTML ou texto, para evitar erros de cabe\u00e7alho j\u00e1 enviados.<\/span><\/p>\n<ul>\n<li aria-level=\"1\"><b>Leia tamb\u00e9m: Como corrigir o <\/b><a href=\"https:\/\/www.locaweb.com.br\/ajuda\/wiki\/erro-400\/\" target=\"_blank\" rel=\"noopener\"><b>erro 400<\/b><\/a><\/li>\n<\/ul>\n<h2><span style=\"font-weight: 400;\">Como utilizar de forma correta? &#8211; Passo a passo<\/span><\/h2>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Para utilizar a fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header<\/span><span style=\"font-weight: 400;\"> do PHP corretamente, especialmente para redirecionamentos com o cabe\u00e7alho <\/span><b>Location<\/b><span style=\"font-weight: 400;\">, siga este passo a passo:<\/span><\/p>\n<ol>\n<li><b>Antes de tudo<\/b><span style=\"font-weight: 400;\">: Certifique-se de que n\u00e3o houve nenhum envio de conte\u00fado ao navegador antes da chamada da fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header<\/span><span style=\"font-weight: 400;\">. Isso inclui espa\u00e7os em branco, tags HTML, ou qualquer sa\u00edda de texto.<\/span><\/li>\n<li><b>Chamada da fun\u00e7\u00e3o<\/b><span style=\"font-weight: 400;\">: Use a fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header<\/span><span style=\"font-weight: 400;\"> com o cabe\u00e7alho <\/span><span style=\"font-weight: 400;\">Location<\/span><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"> para especificar a URL de destino para o redirecionamento.<\/span><\/span>\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ Redireciona o navegador para 'pagina_destino.php'<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Location: pagina_destino.php');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">?&gt;<\/span><\/pre>\n<\/li>\n<li><b>Terminar a execu\u00e7\u00e3o<\/b><span style=\"font-weight: 400;\">: Ap\u00f3s a chamada da fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header<\/span><span style=\"font-weight: 400;\">, use <\/span><b>exit<\/b> <span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">para encerrar a execu\u00e7\u00e3o do script. Isso evita que o script continue a executar e enviar mais sa\u00edda ao navegador, o que pode interferir no redirecionamento.<\/span><\/span>\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Location: pagina_destino.php');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">exit; \/\/ Encerra a execu\u00e7\u00e3o do script<\/span>\r\n\r\n<span style=\"font-weight: 400;\">?&gt;<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>C\u00f3digos de status HTTP<\/b><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">: Se necess\u00e1rio, voc\u00ea pode especificar um c\u00f3digo de status HTTP para o redirecionamento, como 301 para redirecionamentos permanentes ou 302 para tempor\u00e1rios (o padr\u00e3o \u00e9 302).<\/span><\/span><\/span>\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ Redirecionamento permanente para 'pagina_destino.php'<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Location: pagina_destino.php', true, 301);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">exit;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">?&gt;<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Teste<\/b><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">: Teste o seu script em diferentes navegadores para garantir que o redirecionamento est\u00e1 funcionando como esperado.<\/span><\/span><span style=\"font-weight: 400;\">Lembre-se de que a ordem e a l\u00f3gica s\u00e3o cruciais ao trabalhar com cabe\u00e7alhos HTTP. Qualquer sa\u00edda antes da chamada da fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header<\/span><span style=\"font-weight: 400;\"> pode resultar em erros e comportamentos inesperados.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><b>Evite sa\u00eddas antes do redirecionamento<\/b>: Se houver qualquer sa\u00edda (echo, print, HTML) antes da chamada da fun\u00e7\u00e3o header, o redirecionamento n\u00e3o funcionar\u00e1 e um erro ser\u00e1 gerado.<\/span>\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ Isso causar\u00e1 um erro<\/span>\r\n\r\n<span style=\"font-weight: 400;\">echo 'Ol\u00e1, mundo!';<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Location: pagina_destino.php');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">exit;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">?&gt;<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><b>Tratamento de erros<\/b>: Use a fun\u00e7\u00e3o header dentro de uma condi\u00e7\u00e3o para garantir que o redirecionamento s\u00f3 ocorra se certas condi\u00e7\u00f5es forem atendidas.<\/span>\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n\r\n<span style=\"font-weight: 400;\">if ($login_sucesso) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0header('Location: painel_usuario.php');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0exit;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">} else {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0echo 'Login falhou!';<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">?&gt;<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><b>Redirecionamento condicional<\/b>: Voc\u00ea pode usar o redirecionamento para enviar o usu\u00e1rio para diferentes p\u00e1ginas com base em condi\u00e7\u00f5es espec\u00edficas.<\/span>\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n\r\n<span style=\"font-weight: 400;\">if ($usuario_tipo == 'admin') {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0header('Location: painel_admin.php');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">} elseif ($usuario_tipo == 'cliente') {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0header('Location: painel_cliente.php');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">exit;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">?&gt;<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Redirecionamento ap\u00f3s processamento<\/b>: Em formul\u00e1rios ou ap\u00f3s a execu\u00e7\u00e3o de alguma l\u00f3gica, use o redirecionamento para evitar reenvios de formul\u00e1rio.\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ Processamento do formul\u00e1rio aqui<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ ...<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Location: formulario_submetido.php');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">exit;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">?&gt;<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Cabe\u00e7alhos adicionais<\/b>: Al\u00e9m do Location, voc\u00ea pode enviar outros cabe\u00e7alhos, como controle de cache ou tipos de conte\u00fado.\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Cache-Control: no-cache, no-store, must-revalidate'); \/\/ Evita cache<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Pragma: no-cache'); \/\/ Suporte a navegadores antigos<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Expires: 0'); \/\/ Define a data de expira\u00e7\u00e3o para o passado<\/span>\r\n\r\n<span style=\"font-weight: 400;\">?&gt;<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Testes e depura\u00e7\u00e3o<\/b>: Se o redirecionamento n\u00e3o estiver funcionando, verifique se n\u00e3o h\u00e1 sa\u00eddas antes da chamada da fun\u00e7\u00e3o header e se o caminho da URL est\u00e1 correto. Ferramentas de desenvolvedor do navegador podem ajudar a identificar cabe\u00e7alhos enviados e poss\u00edveis erros.<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Redirecionamento ap\u00f3s a\u00e7\u00e3o<\/b>: Se voc\u00ea estiver realizando uma a\u00e7\u00e3o, como salvar informa\u00e7\u00f5es no banco de dados, e depois quiser redirecionar, fa\u00e7a isso ap\u00f3s a a\u00e7\u00e3o ser conclu\u00edda com sucesso.\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ A\u00e7\u00e3o, como salvar informa\u00e7\u00f5es no banco de dados<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ ...<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ Ap\u00f3s a a\u00e7\u00e3o ser bem-sucedida, redirecione<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Location: sucesso.php');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">exit;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">?&gt;<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Evite mensagens de erro<\/b>: Certifique-se de que o arquivo PHP n\u00e3o tenha espa\u00e7os em branco antes da abertura da tag &lt;?php ou ap\u00f3s o fechamento da tag ?&gt;, pois isso pode causar erros de cabe\u00e7alho.<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Utilize caminhos absolutos<\/b>: Ao especificar a URL no cabe\u00e7alho Location, \u00e9 recomend\u00e1vel usar caminhos absolutos em vez de relativos para evitar confus\u00e3o sobre o caminho que est\u00e1 sendo seguido.\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ Caminho absoluto<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Location: http:\/\/www.seusite.com\/pagina_destino.php');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">exit;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">?&gt;<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><strong>R<\/strong><b>edirecionamento com delay<\/b>: Se desejar atrasar o redirecionamento, voc\u00ea pode usar a fun\u00e7\u00e3o <b>sleep<\/b> antes da fun\u00e7\u00e3o header.\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ Atrasa o redirecionamento em 5 segundos<\/span>\r\n\r\n<span style=\"font-weight: 400;\">sleep(5);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Location: pagina_destino.php');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">exit;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">?&gt;<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Teste em diferentes configura\u00e7\u00f5es<\/b>: Teste o redirecionamento em diferentes servidores e configura\u00e7\u00f5es de PHP para garantir que ele funcione em todos os ambientes.<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Verifique as configura\u00e7\u00f5es do servido<\/b>r: Alguns servidores podem ter configura\u00e7\u00f5es que impedem o uso de certos cabe\u00e7alhos ou exigem configura\u00e7\u00f5es adicionais para que o header funcione como esperado.<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Use ferramentas de desenvolvedor<\/b>: Utilize as ferramentas de desenvolvedor do navegador para inspecionar os cabe\u00e7alhos de resposta e garantir que o header est\u00e1 sendo enviado corretamente.&nbsp;<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Documenta\u00e7\u00e3o e recursos<\/b><span style=\"font-weight: 400;\">: Consulte a <\/span><a href=\"https:\/\/www.php.net\/manual\/pt_BR\/function.header.php\"><span style=\"font-weight: 400;\">documenta\u00e7\u00e3o oficial do PHP<\/span><\/a><span style=\"font-weight: 400;\"> e outros recursos online para entender todas as capacidades e nuances da fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b><\/b><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Mantenha-se atualizado<\/b><span style=\"font-weight: 400;\">: Acompanhe as mudan\u00e7as nas vers\u00f5es do PHP e nas pr\u00e1ticas recomendadas, pois elas podem afetar como a fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header<\/span><span style=\"font-weight: 400;\"> deve ser usada.<\/span><\/li>\n<\/ol>\n<h2><span style=\"font-weight: 400;\">Poss\u00edveis erros e cuidados<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Ao trabalhar com a fun\u00e7\u00e3o header do PHP, \u00e9 importante estar ciente de poss\u00edveis erros e tomar certas precau\u00e7\u00f5es para garantir que seu c\u00f3digo funcione corretamente. Aqui est\u00e3o alguns dos erros mais comuns e as precau\u00e7\u00f5es que voc\u00ea pode tomar.<\/span><\/p>\n<h3><b>Headers Already Sent\u00a0<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Este \u00e9 um erro cl\u00e1ssico que ocorre quando voc\u00ea tenta enviar um cabe\u00e7alho ap\u00f3s a sa\u00edda j\u00e1 ter sido iniciada. Isso pode ser causado por espa\u00e7os em branco antes da tag &lt;?php, sa\u00edda de conte\u00fado como HTML ou texto antes da chamada da fun\u00e7\u00e3o header, ou at\u00e9 mesmo por caracteres invis\u00edveis como a marca de ordem de byte (BOM) em arquivos UTF-8.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/p>\n<h3><b>Redirecionamento N\u00e3o Funciona<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">\u00c0s vezes, o redirecionamento pode n\u00e3o funcionar se o caminho especificado no cabe\u00e7alho Location estiver incorreto ou se houver problemas com as configura\u00e7\u00f5es do servidor.<\/span><\/p>\n<h3><b>Falha ao Modificar Cabe\u00e7alhos<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Se voc\u00ea tentar modificar um cabe\u00e7alho que j\u00e1 foi enviado, receber\u00e1 um aviso de que n\u00e3o pode modificar as informa\u00e7\u00f5es do cabe\u00e7alho<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Precau\u00e7\u00f5es:<\/span><\/h2>\n<p><b>Verifique a Sa\u00edda Antecipada<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Certifique-se de que n\u00e3o h\u00e1 sa\u00edda antes da chamada da fun\u00e7\u00e3o header. Isso inclui verificar espa\u00e7os em branco, sa\u00edda de outras fun\u00e7\u00f5es ou inclus\u00e3o de arquivos que possam ter sa\u00edda pr\u00f3pria.<\/span><\/p>\n<p><b>Use Caminhos Absolutos<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Ao usar o cabe\u00e7alho Location para redirecionamentos, prefira usar URLs absolutas para evitar confus\u00e3o sobre o caminho que est\u00e1 sendo seguido.<\/span><\/p>\n<p><b>Chame exit Ap\u00f3s header<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Sempre chame a fun\u00e7\u00e3o \u201cexit\u201d imediatamente ap\u00f3s enviar um cabe\u00e7alho Location para evitar que o script continue a execu\u00e7\u00e3o e envie mais sa\u00edda ao navegador.<\/span><\/p>\n<p><b>Teste em Diferentes Ambientes<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Teste seu c\u00f3digo em diferentes navegadores e configura\u00e7\u00f5es de servidor para garantir que os cabe\u00e7alhos est\u00e3o sendo enviados corretamente.<\/span><\/p>\n<p><b>Manuseie C\u00f3digos de Status HTTP<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Se estiver enviando c\u00f3digos de status HTTP, certifique-se de usar o formato correto e de que o c\u00f3digo de status \u00e9 apropriado para a situa\u00e7\u00e3o.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Consulte a Documenta\u00e7\u00e3o: Sempre consulte a documenta\u00e7\u00e3o oficial do PHP para entender todas as capacidades e nuances da fun\u00e7\u00e3o header.<\/span><\/p>\n<p><b>Erro \u201cCannot modify header information &#8211; headers already sent\u201d:<\/b><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">O erro \u201cCannot modify header information\u201d no PHP geralmente ocorre quando voc\u00ea tenta usar a fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header()<\/span><span style=\"font-weight: 400;\"> para enviar cabe\u00e7alhos HTTP ap\u00f3s j\u00e1 ter enviado sa\u00edda (output) para o navegador. Isso pode acontecer por v\u00e1rios motivos, como:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Espa\u00e7os em branco antes da tag <\/span><span style=\"font-weight: 400;\">&lt;?php<\/span><span style=\"font-weight: 400;\"> ou depois da tag <\/span><span style=\"font-weight: 400;\">?&gt;<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">A codifica\u00e7\u00e3o do arquivo n\u00e3o ser UTF-8 sem BOM (Byte Order Mark).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Mensagens de erro ou avisos que foram impressos antes da chamada da fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header()<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Uso de fun\u00e7\u00f5es como <\/span><span style=\"font-weight: 400;\">echo<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">print<\/span><span style=\"font-weight: 400;\">, ou qualquer outra que gere sa\u00edda antes da chamada da fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header()<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Para resolver esse problema, voc\u00ea pode:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Mover a chamada da fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header()<\/span><span style=\"font-weight: 400;\"> para antes de qualquer sa\u00edda ser enviada.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Usar a fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">ob_start()<\/span><span style=\"font-weight: 400;\"> no in\u00edcio do seu script para armazenar temporariamente a sa\u00edda em um buffer, permitindo que voc\u00ea envie cabe\u00e7alhos a qualquer momento antes de chamar <\/span><span style=\"font-weight: 400;\">ob_end_flush()<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Aqui est\u00e1 um exemplo de como voc\u00ea pode ajustar o c\u00f3digo para evitar esse erro:<\/span><\/p>\n<p><strong>PHP<\/strong><\/p>\n<pre><span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ob_start(); \/\/ Inicia o buffer de sa\u00edda<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ ... seu c\u00f3digo PHP ...<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Location: index.php'); \/\/ Redireciona o usu\u00e1rio<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ob_end_flush(); \/\/ Envia o conte\u00fado do buffer para o navegador<\/span>\r\n\r\n<span style=\"font-weight: 400;\">?&gt;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Lembre-se de que qualquer sa\u00edda, incluindo espa\u00e7os em branco ou linhas em branco, antes da chamada da fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header()<\/span><span style=\"font-weight: 400;\"> pode causar esse erro. Portanto, \u00e9 importante verificar o seu c\u00f3digo cuidadosamente para garantir que n\u00e3o haja sa\u00edda antes da chamada da fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header()<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">E falando sobre Header PHP<\/span><\/h2>\n    \t\t<div class=\"hts-messages hts-messages--info  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Conhe\u00e7a!<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\t<\/p>\n<p><span style=\"font-weight: 400;\">Resolva os desafios de cabe\u00e7alho PHP com a agilidade e flexibilidade do <\/span><a href=\"https:\/\/www.locaweb.com.br\/servidor-vps\/\"><span style=\"font-weight: 400;\">Cloud VPS da Locaweb<\/span><\/a><span style=\"font-weight: 400;\">! Assim como um cabe\u00e7alho PHP bem configurado \u00e9 essencial para o funcionamento correto de suas aplica\u00e7\u00f5es web, escolher o servi\u00e7o de Cloud VPS certo \u00e9 fundamental para a escalabilidade e a performance do seu projeto.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">O Cloud VPS da Locaweb oferece armazenamento em SSD, acesso root, op\u00e7\u00f5es de sistema operacional, data center nacional, seguran\u00e7a refor\u00e7ada e suporte t\u00e9cnico especializado.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Liberte-se das limita\u00e7\u00f5es de hospedagem compartilhada e eleve seu projeto com a <\/span><a href=\"https:\/\/www.locaweb.com.br\/servidor-vps\/\"><span style=\"font-weight: 400;\">infraestrutura robusta do Cloud VPS<\/span><\/a><span style=\"font-weight: 400;\"> da Locaweb, l\u00edder em hospedagem no Brasil.<\/span><\/p>\n<p>    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t\n<h2><span style=\"font-weight: 400;\">Termos de Pesquisa Recorrentes<\/span><\/h2>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>header location php \/ php header location:<\/b><span style=\"font-weight: 400;\"> Esses termos referem-se ao uso da fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header()<\/span><span style=\"font-weight: 400;\"> para redirecionar o navegador para uma nova p\u00e1gina. O cabe\u00e7alho <\/span><span style=\"font-weight: 400;\">Location<\/span><span style=\"font-weight: 400;\"> \u00e9 usado para esse prop\u00f3sito. Exemplo:<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Location: http:\/\/www.exemplo.com');<\/span><\/pre>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>php header:<\/b><span style=\"font-weight: 400;\"> A fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header()<\/span><span style=\"font-weight: 400;\"> \u00e9 usada para enviar cabe\u00e7alhos HTTP brutos para o navegador. \u00c9 uma fun\u00e7\u00e3o poderosa para controlar o cache, o tipo de conte\u00fado, o status da resposta e outros cabe\u00e7alhos HTTP.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>php header json:<\/b><span style=\"font-weight: 400;\"> Quando voc\u00ea quer enviar JSON como resposta de uma requisi\u00e7\u00e3o, voc\u00ea define o cabe\u00e7alho <\/span><span style=\"font-weight: 400;\">Content-Type<\/span><span style=\"font-weight: 400;\"> como <\/span><span style=\"font-weight: 400;\">application\/json<\/span><span style=\"font-weight: 400;\"> usando a fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header()<\/span><span style=\"font-weight: 400;\">. Exemplo:<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Content-Type: application\/json');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">echo json_encode($array);<\/span><\/pre>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>php get header:<\/b><span style=\"font-weight: 400;\"> N\u00e3o existe uma fun\u00e7\u00e3o chamada <\/span><span style=\"font-weight: 400;\">get_header()<\/span><span style=\"font-weight: 400;\"> no PHP. No entanto, voc\u00ea pode acessar os cabe\u00e7alhos de uma requisi\u00e7\u00e3o recebida usando a vari\u00e1vel superglobal <\/span><span style=\"font-weight: 400;\">$_SERVER<\/span><span style=\"font-weight: 400;\">. Por exemplo, <\/span><span style=\"font-weight: 400;\">$_SERVER[&#8216;HTTP_HOST&#8217;]<\/span><span style=\"font-weight: 400;\"> para obter o host.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>php header redirect:<\/b><span style=\"font-weight: 400;\"> Similar ao <\/span><span style=\"font-weight: 400;\">header location php<\/span><span style=\"font-weight: 400;\">, \u00e9 usado para redirecionar o usu\u00e1rio para outra p\u00e1gina. A fun\u00e7\u00e3o <\/span><span style=\"font-weight: 400;\">header()<\/span><span style=\"font-weight: 400;\"> com o cabe\u00e7alho <\/span><span style=\"font-weight: 400;\">Location<\/span><span style=\"font-weight: 400;\"> \u00e9 usada para realizar o redirecionamento. Exemplo:<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">PHP<\/span>\r\n\r\n<span style=\"font-weight: 400;\">header('Location: http:\/\/www.exemplo.com');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">exit;<\/span>\r\n\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A fun\u00e7\u00e3o header do PHP \u00e9 uma poderosa ferramenta que permite aos desenvolvedores enviar cabe\u00e7alhos HTTP brutos para o cliente. \u00c9 essencial para controlar o fluxo de dados entre o servidor e o navegador, e pode ser usada para diversas finalidades, como redirecionamento de URLs, defini\u00e7\u00e3o de cookies, ou controle&#8230;<\/p>\n","protected":false},"author":29,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"ht-kb-category":[548],"ht-kb-tag":[],"class_list":["post-35148","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-wordpress-locaweb"],"_links":{"self":[{"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/ht-kb\/35148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/comments?post=35148"}],"version-history":[{"count":2,"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/ht-kb\/35148\/revisions"}],"predecessor-version":[{"id":37759,"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/ht-kb\/35148\/revisions\/37759"}],"wp:attachment":[{"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/media?parent=35148"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/ht-kb-category?post=35148"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/www.locaweb.com.br\/ajuda\/wp-json\/wp\/v2\/ht-kb-tag?post=35148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}