简体   繁体   中英

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 . 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 ).

Ok, first off, you need to use parenthesis. Multiple parenthesis.

The first set need to go around the contents of the if construct:

if ( ... ) {

The second set need to go around the isset() function call:

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. Always use absolute URLs, otherwise you may break some webservers (I know IIS hates it), or browsers...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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