简体   繁体   English

在网站中间的php中重定向

[英]redirect in php in the middle of the site

I have a site in PHP; 我有一个PHP网站。 I want to redirect to external site after running some code; 我想在运行一些代码后重定向到外部站点; so I want something in the body tag; 所以我想在身体标签上放些东西; to redirect it after my code running; 在我的代码运行后重定向它; something like following example ; 类似下面的例子; what can i do? 我能做什么?

<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<header>
</header>
<?php
    $myffirend=" ";
$allmyffirend=" ";
$user=$_GET['user'];
    // this info save in my db

   redirect here // after save the information

   other code

?>

您可以通过重定向注入JavaScript脚本:

echo "<script>window.location = 'http://www.yourdomain.com'</script>";

It's not posible to redirect using PHP after printing some code. 在打印一些代码后使用PHP重定向是不可能的。 I suggest to move the code to the start of the file. 我建议将代码移到文件的开头。 If this is not possible, you could do it using javascript: 如果无法做到这一点,则可以使用javascript来完成:

window.location='your_url.php';

If you want redirect after page load 如果要在页面加载后重定向

if (window.addEventListener) {
   window.addEventListener("load", afterLoadRedirect, false);
} else {
   window.attachEvent('onload', afterLoadRedirect);
}

function afterLoadRedirect(){
  window.location='your url';
}

You can try the refresh meta tag (seconds, followed by destination): 您可以尝试使用refresh元标记 (秒,后跟目标):

<meta http-equiv="refresh" content="5; url=http://example.com/" />

The W3C recommends against using it for this purpose but I'm assuming you want to briefly show some sort of message before it changes. W3C建议不要为此目的使用它,但我假设您要在更改之前简要显示一些消息。 If not, stick with changing the Location header which must be done before any output: 如果不是,请坚持更改Location标头,这必须在任何输出之前完成:

header('Location: destination.php');

在php中使用javaScript:

echo '<script> window.location = "http://www.yoururl.com";</script>';

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

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