Jump to content

Headers when php in head


Adamhumbug

Recommended Posts

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?

Link to comment
Share on other sites

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">

 

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.