加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何在没有标题重定向的情况下将用户发送到另一个URL?

发布时间:2020-12-13 17:29:59 所属栏目:PHP教程 来源:网络整理
导读:我正在学习 PHP并且一直在寻找这个.我想要实现的是这样的: if (true/false) { go to this url;} 每次我搜索像php重定向或php链接等术语时,99%的时间我会得到一些“标题”.我读到头重定向可以实现这一点,但没有代码可以在它之前,它必须首先在页面上,否则它
我正在学习 PHP并且一直在寻找这个.我想要实现的是这样的:
if (true/false) {
    go to this url;
}

每次我搜索像php重定向或php链接等术语时,99%的时间我会得到一些“标题”.我读到头重定向可以实现这一点,但没有代码可以在它之前,它必须首先在页面上,否则它不会工作.

如果是这样,那么我该如何实现呢?

i read that header redirects can achieve this but no code can go before it. that it must be first on the page else it wont work.

那是错的.在此之前必须没有输出.因此,你必须确保,你之前没有回声,打印,?>某事<?php(或其他).

if (true)  {
  header('Location: ' . $url,false,302);
  exit; // Ensures,that there is no code _after_ the redirect executed
}

你可以准备好一切about it in the official manual.特别是:

Remember that header() must be called before any actual output is sent,either by normal HTML tags,blank lines in a file,or from PHP. It is a very common error to read code with include(),or require(),functions,or another file access function,and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读