0

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

1
  • 4
    This is basic syntax. You should really read up on it before continuing trying to code in PHP, in my opinion. It will make things go much more smoothly later on. Commented Aug 13, 2010 at 15:12

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.