Simple PHP redirection , why isnt this working?

Discussion in 'PHP' started by visualkreations, Aug 25, 2008.

  1. #1
    Hi,

    I am trying to make a very simple login where someone enters a username and pass word and as soon as they enter the correct uname and password fo it to redirect them to www.yahoo.com


    here is the code i am using



    <hmtl>
    <head />
    <body>

    <form action="index.php" method=POST>
    Username1: <input type=text name=user><br /><br />
    Password: <input type=password name=pass><br />
    <input type=submit value="Go!"><p>
    </form>
    <?php

    $user=$_POST['user'];
    $pass=$_POST['pass'];

    if(($user=="John")&&($pass=="Smith")) header("Location: http://www.yahoo.com");
    else echo "Access Denied!";

    ?>

    </body>
    </html>




    Thanks in advanced
     
    visualkreations, Aug 25, 2008 IP
  2. Wrighty

    Wrighty Peon

    Messages:
    199
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    All redirects have to be done before any of hte HTML is parsed. Put your PHP at the start of the code (above the HTML) and then assign 'access denied' to a variable and echo the variable where you want it! :)
     
    Wrighty, Aug 25, 2008 IP
  3. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <?php
    
    $user=$_POST['user'];
    $pass=$_POST['pass'];
    
    if($user=="John" && $pass=="Smith") {
         header("Location: http://www.yahoo.com");
    }else{
         echo "Access Denied!";
    }
    
    ?>
    
    <html>
    <body>
    
    <form action="index.php" method="post">
    Username1: <input type="text" name="user"><br /><br />
    Password: <input type="password" name="pass"><br />
    <input type="submit" value="Go!">
    </form>
    
    </body>
    </html>
    
    Code (markup):
    Try this..
     
    MCJim, Aug 25, 2008 IP
  4. kasapa

    kasapa Peon

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    or try this

     
    kasapa, Aug 26, 2008 IP
  5. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #5
    In your script, you haven't even used the { and } for the if statement and the else statement. So your script isn't processing what you want it to do
     
    Grit., Aug 26, 2008 IP
  6. projectWORD

    projectWORD Active Member

    Messages:
    287
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #6
    You actually dont need { } in an if statement is the statement is one line in length.
     
    projectWORD, Aug 26, 2008 IP
  7. rcadble

    rcadble Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    projectWORD is write.

    I think this has been explained pretty well, you cannot send headers when content has already been sent. If you want to check to see if the username/password were valid users, that might be more difficult.
     
    rcadble, Aug 26, 2008 IP
  8. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #8
    true, i just find it cleaner to use { and } in my if statements.
    i did kinda skim read the provided code for any kind of syntax error, and so it was my fault for not actually checking for content sent to the page prior to the header tag. :< I'll remember to read all of the code next time ^^.
    I've learnt my lesson
     
    Grit., Aug 26, 2008 IP
  9. The Universes

    The Universes Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You probably should stick a exit; after the header location.
     
    The Universes, Aug 26, 2008 IP