简体   繁体   English

标题的PHP语法问题

[英]PHP Syntax issue with headers

Can't seem to get it right with the syntax: 使用语法似乎无法正确处理:

<?php 
if isset $_POST['tutorial'] {
    header('Location: /phonebooks?tutorial=1b');
} else {
    header('Location: /phonebooks?phonebook_created=1');
};
?>

What am I doing wrong?? 我究竟做错了什么??

You're missing brackets around your if condition and around $_POST['tutorial'] , which is an argument to isset . 您缺少if条件和$_POST['tutorial']左右的括号,这是isset的参数。 It should be: 它应该是:

<?php 
if (isset($_POST['tutorial']))
{
  header('Location: /phonebooks?tutorial=1b');
}
else
{
  header('Location: /phonebooks?phonebook_created=1');
}
?>

As pointed out in the comments, an absolute URL is required for a Location header as well (so instead of /phonebooks?tutorial=1b , you'd need to specify http(s)://yourdomain.com/phonebooks?tutorial=1b ). 如评论中所指出的, Location标头也需要一个绝对URL(因此,您需要指定http(s)://yourdomain.com/phonebooks?tutorial=1b来代替/phonebooks?tutorial=1bhttp(s)://yourdomain.com/phonebooks?tutorial=1b )。

Ok, first off, you need to use parenthesis. 好的,首先,您需要使用括号。 Multiple parenthesis. 多个括号。

The first set need to go around the contents of the if construct: 第一组需要遍历if构造的内容:

if ( ... ) {

The second set need to go around the isset() function call: 第二组需要isset()函数调用:

isset($_POST['tutorial'])

So that line becomes: 因此,该行变为:

if (isset($_POST['tutorial'])) {

You also have another problem. 您还有另一个问题。 Don't use location headers with relative urls. 不要将位置标头与相对网址一起使用。 It's against the HTTP specification. 这违反了HTTP规范。 Always use absolute URLs, otherwise you may break some webservers (I know IIS hates it), or browsers... 始终使用绝对URL,否则可能会破坏某些Web服务器(我知道IIS讨厌它)或浏览器...

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

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