Home > front end >  Is it possible to set a page title with header?
Is it possible to set a page title with header?

Time:09-23

I have a main page (index.php) and if the client is not logged in, it redirect to the login.php.

header("Location: login.php");

The problem is that the search engines display as name the "example.com" instead of the title of the index or the login.

I am thinking if it is possible to do something like that.

header("Title: Example");
header("Location: login.php");

CodePudding user response:

It sounds to me as though you want the Search Engine to index (and pick up the title) of your login page only - not the redirecting one, so set an HTTP status code in your redirect to inform them of the correct status of those pages.

I'd suggest using 302 (Found) - this will indicate to the Search Engines that - while the page exists - it's redirecting them (and non-logged in users) to another page - which is the one of interest.

Something like this:

header("Location: https://www.example.com/login.php", true, 302);

Then on your login page just set up the HTML correctly as you would otherwise including:

<head><title>Your Page Title</title></head>

Now search engines will not index the redirected page, but only the login page with your correctly set title.

CodePudding user response:

You must set the title of a HTML document by using the <title> element inside the <head> element. You cannot do it via a HTTP response header.

<html>
  <head>
    <title>Example title</title>
  </head>
  <body>Some content</body>
</html>

Documentation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title

CodePudding user response:

You can create a session variable before calling header method like this

session_start();
$_SESSION["title"] = "Hello world title";
header("location:login.php");

and in the login.php file you can write

<?php 
session_start();
$title = $_SESSION["title"];
?>
<title><?= $title ?></title>

This can also be done using cookies or get variables

  • Related