在 PHP 中自动重定向页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14031569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:27:59  来源:igfitidea点击:

Redirecting a page automatically in PHP

phphtmlhttp

提问by Javeria Habib

I want to redirect a page automatically in PHP

我想在 PHP 中自动重定向页面

Logout.php:

登出.php:

<?php 
  include "base.php"; 
  $_SESSION = array(); session_destroy();
?>
<meta http-equiv="refresh" content="=0;URL=index.php" />

Where base.php calls the database and starts the session:

其中 base.php 调用数据库并启动会话:

<?php
  session_start();  
  $dbhost = "localhost";
  $dbname = "login";
  $dbuser = "root";
  $dbpass = "";
  mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());  
  mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());  
?>  

When pressing logout, I am not getting back to index.php.

按注销时,我不会回到index.php.

回答by samayo

As far as I know, HTML, JavaScriptand PHPprovide their own way of page / header redirection. Here are three examples, showing how to redirect to http://google.com

据我所知,HTML,JavaScriptPHP提供自己的页面/标题重定向方式。以下是三个示例,显示了如何重定向到http://google.com

# JavaScript:

# JavaScript:

<script type="text/javascript">
    window.location = "http://google.com";
</script>

# HTML:

# HTML:

<meta http-equiv="refresh" content="0; URL='http://google.com'"/> 

NoteThe 0 in content="0;, is a value for seconds. It tells the browser how many seconds it should wait before starting the redirect.

Note中的 0content="0;是秒值。它告诉浏览器在开始重定向之前应该等待多少秒。

# PHP:

# PHP:

<?php

header('Location: http://www.google.com');

NoteA PHP header()must be Alwaysbe placed before outputting anything to the browser; even a single empty space. Otherwise, it will cause the infamous "header already sent" errors.

Note在向浏览器输出任何内容之前,header()必须始终放置PHP ;甚至一个空的空间。否则,会导致臭名昭著的“ header already sent”错误。

回答by cristi _b

This should work, you had an extra =before 0:

这应该工作,你有一个额外的=面前0

<meta http-equiv="refresh" content="0;URL=index.php" />

Linky https://en.wikipedia.org/wiki/Meta_refresh

链接https://en.wikipedia.org/wiki/Meta_refresh

回答by gamehelp16

you can put this on your PHP code:

你可以把它放在你的 PHP 代码中:

header('Location:index.php');

Note that as per all headers, this must be placed before any output (even whitespace).

请注意,根据所有headers,这必须放在任何输出(甚至是空格)之前。

回答by Michel Feldheim

Meta refresh syntax is slightly wrong

元刷新语法略有错误

<meta http-equiv="refresh" content="0;URL='<?php echo $_SERVER['HTTP_HOST']; ?>/index.php'">

More details here http://en.wikipedia.org/wiki/Meta_refresh

更多细节在这里 http://en.wikipedia.org/wiki/Meta_refresh

The cleaner way is to send a http redirect header

更干净的方法是发送一个 http 重定向标头

More details here http://en.wikipedia.org/wiki/HTTP_301

更多细节在这里 http://en.wikipedia.org/wiki/HTTP_301

logout.php

登出.php

<?php
..
session_destroy();
header( 'HTTP/1.1 301 Moved Permanently');
header( 'Location: ' . $_SERVER['HTTP_HOST']  . '/index.php' );
exit(0);

Concerning absolute URIs in redirects W3C says

关于重定向中的绝对 URI,W3C 说

14.30 Location

14.30 地点

The Location response-header field is used to redirect the recipient to a location other than the Request-URI for completion of the request or identification of a new resource. For 201 (Created) responses, the Location is that of the new resource which was created by the request. For 3xx responses, the location SHOULD indicate the server's preferred URI for automatic redirection to the resource. The field value consists of a single absolute URI.

   Location       = "Location" ":" absoluteURI

An example is:

   Location: http://www.w3.org/pub/WWW/People.html

位置响应头字段用于将接收者重定向到请求 URI 以外的位置,以完成请求或识别新资源。对于 201(已创建)响应,位置是由请求创建的新资源的位置。对于 3xx 响应,位置应该指示服务器自动重定向到资源的首选 URI。字段值由单个绝对 URI 组成。

   Location       = "Location" ":" absoluteURI

一个例子是:

   Location: http://www.w3.org/pub/WWW/People.html

Source: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

来源:http: //www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

回答by Lucho

In the case you need to redirect the web page with a PHP variable include you can do this: where $user[0]is PHP variable. In that way, the next web page user.phpcan get the value of the variable.

如果您需要使用 PHP 变量包含重定向网页,您可以执行以下操作:$user[0]PHP 变量在哪里。这样,下一个网页user.php就可以得到该变量的值了。

header('Location:./user.php?u_id='.$user[0]);

or

或者

header("Location:./user.php?u_id=$user[0]");