Adamhumbug Posted September 22, 2020 Share Posted September 22, 2020 Hi All, I am trying to use headers at the end of functions to put the user where they need to be. header("location: register.php?user_may_exist"); I understand how they work and their qwuirks. My issue is, in my head file, i am outputting which causes issues for the headers which come after. I have the following in head.php <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> <?php if(isset($currentPageTitle)){ echo $currentPageTitle; }else{ echo 'Cricket Club'; }; ?> </title> <?php if(isset($currentPageMeta)){ echo "<meta name='description' content='$currentPageMeta'>"; }else{ echo "<meta name='description' content='All the latest from CC'>"; }; ?> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> Is there a better way for me to achieve this? Quote Link to comment https://forums.phpfreaks.com/topic/311513-headers-when-php-in-head/ Share on other sites More sharing options...
Barand Posted September 22, 2020 Share Posted September 22, 2020 Simples! Do your php processing before the html section. <?php ?> <html> </html> 1 Quote Link to comment https://forums.phpfreaks.com/topic/311513-headers-when-php-in-head/#findComment-1581530 Share on other sites More sharing options...
Adamhumbug Posted September 22, 2020 Author Share Posted September 22, 2020 16 minutes ago, Barand said: Simples! Do your php processing before the html section. <?php ?> <html> </html> Do i not still have to include something after the html section? Like follows: <?php if(isset($currentPageTitle)){ $curPage = $currentPageTitle; }else{ $curPage = 'Cricket Club'; }; if(isset($currentPageMeta)){ $desc = "<meta name='description' content='$currentPageMeta'>"; }else{ $desc = "<meta name='description' content='All the latest from CC'>"; }; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> <?= $curPage ?> </title> <?= $desc ?> <meta name="viewport" content="width=device-width, initial-scale=1.0"> Quote Link to comment https://forums.phpfreaks.com/topic/311513-headers-when-php-in-head/#findComment-1581531 Share on other sites More sharing options...
Barand Posted September 22, 2020 Share Posted September 22, 2020 Yes, there will be some variables to output that were prepared in the php section, but any validation conditions requiring you to send a location header to another page will have occured before anything is sent to the browser in the html section. Quote Link to comment https://forums.phpfreaks.com/topic/311513-headers-when-php-in-head/#findComment-1581532 Share on other sites More sharing options...
Adamhumbug Posted September 22, 2020 Author Share Posted September 22, 2020 1 hour ago, Barand said: Yes, there will be some variables to output that were prepared in the php section, but any validation conditions requiring you to send a location header to another page will have occured before anything is sent to the browser in the html section. Hmm, i am sure i am being very simple here but im not sure i follow. I have updated the code in my head.php to what is in the post above. Most of my other pages include head.php On my register page i have the following error: Quote [22-Sep-2020 14:37:21 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at /homepages/1/d837472791/htdocs/includes/head.php:14) in /homepages/1/d837472791/htdocs/register.php on line 73 Register line 73 is: header("location: register.php?user_may_exist"); and head line 14 is the last line of this: <?php if(isset($currentPageTitle)){ $curPage = $currentPageTitle; }else{ $curPage = 'Baldock Town Cricket Club'; }; if(isset($currentPageMeta)){ $desc = "<meta name='description' content='$currentPageMeta'>"; }else{ $desc = "<meta name='description' content='All the latest from Baldock Town CC'>"; }; ?> <!DOCTYPE html> Quote Link to comment https://forums.phpfreaks.com/topic/311513-headers-when-php-in-head/#findComment-1581533 Share on other sites More sharing options...
Adamhumbug Posted September 22, 2020 Author Share Posted September 22, 2020 I immediately saw my issue as soon as i wrote the above. I was including head at the start of the register document rather than just above the html. Quote Link to comment https://forums.phpfreaks.com/topic/311513-headers-when-php-in-head/#findComment-1581534 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.