简体   繁体   English

是否可以使用 header 设置页面标题?

[英]Is it possible to set a page title with header?

I have a main page (index.php) and if the client is not logged in, it redirect to the login.php.我有一个主页(index.php),如果客户端没有登录,它会重定向到 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.问题是搜索引擎显示为名称“example.com”,而不是索引或登录名的标题。

I am thinking if it is possible to do something like that.我在想是否有可能做这样的事情。

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

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.在我看来,好像您希望搜索引擎仅索引(并获取标题)您的登录页面 - 而不是重定向页面,因此在重定向中设置 HTTP 状态代码以通知他们这些页面的正确状态.

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.我建议使用 302 (Found) - 这将向搜索引擎表明 - 当页面存在时 - 它会将它们(以及未登录的用户)重定向到另一个页面 - 这是一个感兴趣的页面。

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:然后在您的登录页面上正确设置 HTML ,否则您将包括:

<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.现在搜索引擎不会索引重定向页面,而只会索引具有正确设置标题的登录页面。

You must set the title of a HTML document by using the <title> element inside the <head> element.您必须使用<head>元素内的<title>元素设置 HTML 文档的标题。 You cannot do it via a HTTP response header.您不能通过 HTTP 响应 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文档:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title

You can create a session variable before calling header method like this您可以在像这样调用 header 方法之前创建一个 session 变量

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

and in the login.php file you can write在 login.php 文件中你可以写

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

This can also be done using cookies or get variables这也可以使用 cookies 或获取变量来完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM