Help with header function?

Discussion in 'PHP' started by Illumine1983, May 4, 2008.

  1. #1
    I set up a form with username and password, and was told that I could use the header function to direct them to another page on successful login. However, from my understanding the header function has to come before any HTML tags so I cannot put it into the form. I've copied the code I've used so far below, and any advice on how to link properly would be appreciated, thanks!:

    <?php
    $username = "merlin";
    $password = "password";
    if (isset($_POST["username"]))
    {
    if ($_POST["username"] == $username && $_POST["password"] == $password)
    {
    header("location: home.php");
    }
    else
    {
    echo ("Incorrect username or password");
    }
    }
    ?>
     
    Illumine1983, May 4, 2008 IP
  2. cbn81

    cbn81 Peon

    Messages:
    160
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This vode must be placed at hte beginning of the php file which is targeted by the form action, i.e. form action="login.php", this code must be at the beginning of the login.php file
     
    cbn81, May 4, 2008 IP
  3. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <?php
    $username = "merlin";
    $password = "password";
    if (isset($_POST["username"])) 
    {
    if ($_POST["username"] == $username && $_POST["password"] == $password)
    {
    die(header("location: home.php")); // Die after sending header.
    }
    else
    {
    echo ("Incorrect username or password");
    }
    }
    ?>
    That should work...
    
    
    PHP:
     
    NatalicWolf, May 4, 2008 IP
  4. Illumine1983

    Illumine1983 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hmm, tried but it just gave me an error:

    "Warning: Cannot modify header information - headers already sent by (output started at D:\Applications\xampp\htdocs\test\login_form.php:21) in D:\Applications\xampp\htdocs\test\login_form.php on line 53"

    Guess I will have to ask the class tutor this one!
     
    Illumine1983, May 4, 2008 IP
  5. Illumine1983

    Illumine1983 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ah solved it in a moment of inspiration - i think i understand now what u meant cbn81; i pasted the entire script before my html tags and it worked - thanks!
     
    Illumine1983, May 4, 2008 IP