Why won't this code work?

Discussion in 'PHP' started by jis2507, Feb 8, 2010.

  1. #1
    I am trying to make a VERY BASIC login form to test out different PHP things and to practice PHP (I'm only learning it now).

    Why don't this code work for a login form? I can get the first part to work (the form) but then the php script that processes it won't. Please help me out.


    The first file. index.php
    
    <html>
    <body>
    
    <?php if(isset($_SESSION["login"]) && ($_SESSION["login"]=="true"))
      echo "You are Logged In";
    else
      echo "You are not logged in. Please login Below"; ?>
    
    <br>
    <br>
    <br>
    <form action="login.php" method="post">
    Password: <input type="password" name="pass">
    <br>
    <input type="submit" value="submit">
    </form>
    </body>
    </html>
    
    PHP:

    The second file. login.php
    
    <?php session_start();
    if($_POST["pass"]=="lol")
      {
      echo "Login Correct";
      $_SESSION["login"]="true";
      header("Location: index.php);
      }
    else
      {
      echo "Login Incorrect";
      session_destroy();
      header("Location: index.php");
      }
    ?>
    
    PHP:

    Any help would be greatly appreciated.
     
    jis2507, Feb 8, 2010 IP
  2. beacon

    beacon Peon

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    session_start(); Required in index.php
     
    beacon, Feb 8, 2010 IP
  3. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Oh, but you missed out braces around the if {} and the else {}:

    if(isset($_SESSION["login"]) && ($_SESSION["login"]=="true"))
      {
         echo "You are Logged In";
      } else {
        echo "You are not logged in. Please login Below";
      } 
    PHP:
    I didn't test this, but I THINK from personal experience (ahem) that without the braces it will evaluate both the IF and the ELSE and print both messages? It will do SOMETHING wrong, I am pretty sure!
     
    markowe, Feb 8, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    <html>
    <body>
    
    <?php
     if(isset($_SESSION["login"]) && $_SESSION["login"]=="true"){
      echo "You are Logged In";
    } else {
      echo "You are not logged in. Please login Below";
    }
     ?>
    
    <br>
    <br>
    <br>
    <form action="login.php" method="post">
    Password: <input type="password" name="pass">
    <br>
    <input type="submit" value="submit">
    </form>
    </body>
    </html>
    PHP:
     
    danx10, Feb 8, 2010 IP
  5. spyka

    spyka Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    You're missing a closing " in the header call in login.php
    You need to add session_start to index.php (and any other page you want to get access to the $_SESSION variables
    Use braces for your if statements in index.php
     
    spyka, Feb 8, 2010 IP
  6. picos

    picos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    81
    #6
    don't use echo or print_r function before header
    this is problem.
     
    picos, Feb 8, 2010 IP
  7. blacksheep666

    blacksheep666 Active Member

    Messages:
    68
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #7
    
    <?php session_start(); ?>
    
    <html>
    <body>
    
    <?php
     if(isset($_SESSION["login"]) && $_SESSION["login"]=="true"){
      echo "You are Logged In";
    } else {
      echo "You are not logged in. Please login Below";
    }
     ?>
    
    <br>
    <br>
    <br>
    <form action="login.php" method="post">
    Password: <input type="password" name="pass">
    <br>
    <input type="submit" value="submit">
    </form>
    </body>
    </html>
    
    
    
    Code (markup):

    You forget to put session_start();
     
    blacksheep666, Feb 9, 2010 IP
  8. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #8
    I stand corrected, braces {} are not necessary in an if else if there is just one expression to evaluate - I was reading the PHP manual as a bedtime story last night and came across that :)

    BTW, echoing stuff straight out like you are doing is sloppy - you should really look into implementing a Controller/View structure that separates your code from your output as much as possible. A framework like CodeIgniter will really help with this, and with making coding easier too! I know, I used to code like this too :) I am a reformed character now!
     
    markowe, Feb 11, 2010 IP
  9. blacksheep666

    blacksheep666 Active Member

    Messages:
    68
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #9
    Please let him understand the problem before you introduce some framework. CodeIgniter is too advance for him. How can he understand a fully object oriented framework if he even don't know how to solve this simple problem.
     
    blacksheep666, Feb 11, 2010 IP
  10. Warll

    Warll Peon

    Messages:
    122
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    never mind, I miss read something.
     
    Warll, Feb 11, 2010 IP
  11. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #11
    Well, that's one argument, the other argument is that it's better to learn good programming habits from day one. Personally I'm glad I DID learn a lot of pure PHP to begin with, but it's hard to break bad habits later. It's true though, I guess a framework would be hard to understand if you didn't know much pure PHP...
     
    markowe, Feb 12, 2010 IP