tags:

views:

44

answers:

2

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??

+3  A: 

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

Daniel Vandersluis
And `Location:` headers should, according to the RFC, be full URIs (i.e., they should start with `http://` or `https://`.
TRiG
Are brackets really necessary for a single condition `if` statement? It's been a while since I've whipped out the PHP skills.
Anthony Forloney
@Anthony Yes, brackets are required around the conditions for all the php keywords that take conditions (`if`, `for`, `while`, etc.)
Daniel Vandersluis
+3  A: 

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

ircmaxell
+1 for the explanations.
Brad F Jacobs