如何在PHP中进行重定向?
是否可以通过使用PHP将用户重定向到不同的页面 假设用户转到如何在PHP中进行重定向?,php,redirect,http-status-code-301,http-redirect,Php,Redirect,Http Status Code 301,Http Redirect,是否可以通过使用PHP将用户重定向到不同的页面 假设用户转到www.example.com/page.php,我想将他们重定向到www.example.com/index.php,如果不使用元刷新,我怎么做?可能吗 这甚至可以防止未经授权的用户访问我的页面。使用发送: 与一些人的想法相反,die()与重定向无关。如果要重定向正常执行的而不是,请仅使用它 文件example.php: 恢复-强制的die()/exit()是一些与实际PHP无关的城市传奇。它与客户机“尊重”位置:标题无关。发送标头不
www.example.com/page.php
,我想将他们重定向到www.example.com/index.php
,如果不使用元刷新,我怎么做?可能吗
这甚至可以防止未经授权的用户访问我的页面。使用发送:
与一些人的想法相反,die()
与重定向无关。如果要重定向正常执行的而不是,请仅使用它
文件example.php:
恢复-强制的die()
/exit()
是一些与实际PHP无关的城市传奇。它与客户机“尊重”位置:标题无关。发送标头不会停止PHP执行,无论使用的是哪个客户端。标头('位置:http://www.yoursite.com/new_page.html' );代码>
别忘了去死()/退出() 现有答案的摘要加上我自己的两分钱:
1.基本答案
您可以使用header()
函数发送新的HTTP头,但必须在任何HTML或文本之前发送到浏览器(例如,在
声明之前)
2.重要细节
模具()或退出()
为什么要使用die()
或exit()
:
绝对或相对URL
自2014年6月起,可以使用绝对URL和相对URL。看看哪一个取代了旧的,那里只允许绝对URL
状态代码
<?php
header("Refresh: 3;url=https://theweek.com.br/índex.php");
PHP的“Location”头仍然使用-redirect代码,这是一个“临时”重定向,可能不是您应该使用的重定向。你应该考虑(永久重定向)或(其他)。
注意:303头与“许多HTTP/1.1之前的用户代理”不兼容。当前使用的浏览器都是HTTP/1.1用户代理。对于许多其他用户代理,如爬行器和机器人,情况并非如此
3.文件
HTTP头和PHP中的header()
函数
4.备选方案
您可以使用另一种方法http_redirect($url);
,需要安装
5.辅助功能
此功能不包含303状态代码:
function Redirect($url, $permanent = false)
{
header('Location: ' . $url, true, $permanent ? 301 : 302);
exit();
}
Redirect('http://example.com/', false);
这是更灵活的:
function redirect($url, $statusCode = 303)
{
header('Location: ' . $url, true, $statusCode);
die();
}
6.变通办法
如前所述,header()
重定向只在写入任何内容之前起作用。如果输出失败,它们通常会失败。然后您可以使用HTML标头解决方法(不是很专业!),如:
正如这里的其他人所说,发送位置标头时使用:
header( "Location: http://www.mywebsite.com/otherpage.php" );
但您需要在将任何其他输出发送到浏览器之前执行此操作
此外,如果您要使用此选项阻止未经身份验证的用户访问某些页面(如您所述),请记住,某些用户代理仍将继续访问当前页面,因此您需要在发送后死亡()。大多数答案都忘记了一个非常重要的步骤
header("Location: myOtherPage.php");
die();
把重要的第二行删掉可能会看到你结束了。问题是浏览器不必尊重你的页面返回的页眉,所以页眉被忽略了,页面的其余部分将不用重定向而执行。
在语义Web的前夕,正确性是要考虑的。遗憾的是,PHP的“位置”。“-header仍然使用-redirect代码,严格来说,这不是重定向的最佳代码。它应该使用的是一个
function Redirect($url, $code = 302)
{
if (strncmp('cli', PHP_SAPI, 3) !== 0)
{
if (headers_sent() !== true)
{
if (strlen(session_id()) > 0) // If using sessions
{
session_regenerate_id(true); // Avoids session fixation attacks
session_write_close(); // Avoids having sessions lock other requests
}
if (strncmp('cgi', PHP_SAPI, 3) === 0)
{
header(sprintf('Status: %03u', $code), true, $code);
}
header('Location: ' . $url, true, (preg_match('~^30[1237]$~', $code) > 0) ? $code : 302);
}
exit();
}
}
Redirect 301 / http://new-site.com/
W3C非常友好,303头与“许多HTTP/1.1之前的用户代理”不兼容,这相当于当前使用的浏览器。因此,302是一件文物,不应该使用
…或者你可以忽略它,就像其他人一样…我已经回答了这个问题,但我会再回答一遍,因为在此期间,我了解到,如果你在CLI中运行(重定向无法发生,因此不应该退出()
),或者如果你的Web服务器作为(F)CGI运行PHP,则会出现一些特殊情况(需要先前设置的状态
标题才能正确重定向)
我还处理了支持不同HTTP重定向代码(301
、302
、303
和307
)的问题,正如我在上一个回答的评论中所述。以下是描述:
- 301-永久移动
- 302-找到
- 303-参见其他
- 307-临时重定向(HTTP/1.1)
这些答案中有许多是正确的,但它们假设您有一个绝对URL,而实际情况可能并非如此。如果您想使用相对URL并生成其余URL,则可以执行以下操作
$url = 'http://' . $_SERVER['HTTP_HOST']; // Get the server
$url .= rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); // Get the current directory
$url .= '/your-relative/path-goes/here/'; // <-- Your relative path
header('Location: ' . $url, true, 302); // Use either 301 or 302
$url='http://'。$\u SERVER['http\u HOST'];//获取服务器
$url.=rtrim(dirname($\u SERVER['PHP\u SELF']),'/\\\');//获取当前目录
$url.='/your relative/path goes/here/';//使用echo从PHP输出JavaScript,这将完成此任务
echo '<script type="text/javascript">
window.location = "http://www.google.com/"
</script>';
echo'
window.location=”http://www.google.com/"
';
除非您缓冲页面输出,然后稍后检查重定向条件,否则无法在PHP中真正执行此操作。这可能太麻烦了。请记住,页眉是从页面发送的第一件事。大多数重定向通常在页面的稍后部分需要。为此,您必须缓冲页面的所有输出,并检查是否存在重定向此时,您可以重定向page user header()或简单地回显缓冲输出
有关缓冲的更多信息(优势)
使用以下代码:
header("Location: /index.php");
exit(0);
您可以使用如下一些JavaScript方法
self.location=”http://www.example.com/index.php“;
window.location.href=”http://www.examp
header( "Location: http://www.mywebsite.com/otherpage.php" );
header("Location: myOtherPage.php");
die();
function Redirect($url, $code = 302)
{
if (strncmp('cli', PHP_SAPI, 3) !== 0)
{
if (headers_sent() !== true)
{
if (strlen(session_id()) > 0) // If using sessions
{
session_regenerate_id(true); // Avoids session fixation attacks
session_write_close(); // Avoids having sessions lock other requests
}
if (strncmp('cgi', PHP_SAPI, 3) === 0)
{
header(sprintf('Status: %03u', $code), true, $code);
}
header('Location: ' . $url, true, (preg_match('~^30[1237]$~', $code) > 0) ? $code : 302);
}
exit();
}
}
$url = 'http://' . $_SERVER['HTTP_HOST']; // Get the server
$url .= rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); // Get the current directory
$url .= '/your-relative/path-goes/here/'; // <-- Your relative path
header('Location: ' . $url, true, 302); // Use either 301 or 302
echo '<script type="text/javascript">
window.location = "http://www.google.com/"
</script>';
header("Location: /index.php");
exit(0);
<?php header('Location: another-php-file.php'); exit(); ?>
header('Location: another-php-file.php'); exit();
header('Location: https://www.google.com'); exit();
<?php
session_start();
if (!isset( $_SESSION["valid_user"]))
{
header("location:../");
exit();
}
// Page goes here
?>
ob_start();
header("Location: " . $website);
ob_end_flush();
<?php
header("Location: /index.php");
?>
header("Location: /index.php");
header("Location: /index.php");
echo "I am a web developer";
header("Location: /index.php");
return $something;
header("Location: /index.php");
<?php echo "<script>location.href='target-page.php';</script>"; ?>
<?php
header('Location: target-page.php');
exit();
?>
<?php
ob_start(); //this should be first line of your page
header('Location: target-page.php');
ob_end_flush(); //this should be last line of your page
?>
<?php
header("Location:./"); // Redirect to index file
header("Location:index.php"); // Redirect to index file
header("Location:example.php");
?>
<?php
header('Location: redirectpage.php');
header('Location: redirectpage.php');
exit();
echo "<script>location.href='redirectpage.php';</script>";
?>
<?php
header('refresh:5;url=redirectpage.php '); // Note: here 5 means 5 seconds wait for redirect.
?>
Redirect 301 / http://new-site.com/
<?php
$url = "targetpage"
function redirect$url(){
if (headers_sent()) == false{
echo '<script>window.location.href="' . $url . '";</script>';
}
}
?>
header("Location: http://www.yourwebsite.com/user.php"); /* Redirect browser */
exit();
<?php
header("Location: https://bskud.com/PINCODE/BIHAR.php");
exit;
?>
<?php
$myVar = "bskud";
if ($myVar == "bskud") {
?>
<script> window.location.href="https://bskud.com"; </script>
<?php
}
else {
echo "<b>Check the website name again</b>";
}
?>
$your_target_url = “www.example.com/index.php”;
header(“Location : $your_target_url”);
exit();
<?php
function authRedirect($get_auth_level,
$required_level,
$if_fail_link = “www.example.com/index.php”){
if ($get_auth_level != $required_level){
header(location : $if_fail_link);
return false;
exit();
}
else{
return true;
}
}
. . .
<?php
// page.php
require “function.php”
// Redirects to www.example.com/index.php if the
// user isn’t authentication level 5
authRedirect($_SESSION[‘u_auth’], 5);
// Redirects to www.example.com/index.php if the
// user isn’t authentication level 4
authRedirect($_SESSION[‘u_auth’], 4);
// Redirects to www.someotherplace.com/somepage.php if the
// user isn’t authentication level 2
authRedirect($_SESSION[‘u_auth’], 2, “www.someotherplace.com/somepage.php”);
. . .
<?php
header('Location: mypage.php');
?>
<?php
header('Location: /directory/mypage.php');
?>
<?php
header('Location: http://www.ccm.net/forum/');
?>
<?
header('Status: 301 Moved Permanently', false, 301);
header('Location: new_address');
?>
<?
header('Status: 301 Moved Permanently', false, 301);
header('Location: /pc/imprimante.php3');
exit();
?>
<?
header('Status: 301 Moved Permanently', false, 301);
header('Location: address');
exit();
?>
<?php
header('Location: index.php');
?>
<?php
$id = 2;
header("Location: index.php?id=$id&msg=succesfully redirect");
?>
<?php
echo "<script>location.href='index.php';</script>";
?>
<?php
$id = 2;
echo "<script>location.href='index.php?id=$id&msg=succesfully redirect';</script>";
?>
<?php
header("Refresh: 3;url=https://theweek.com.br/índex.php");
<?php
if(isset($_GET['go_to_page_b'])) {
header('Location: B.php');
exit();
}
?>
<p>I am page A</p>
<button name='go_to_page_b'>Page B</button>
<p> I am Page B</p>