ok... password protecting just one page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djsjm
    New Member
    • Nov 2008
    • 23

    ok... password protecting just one page

    Hello again. So I googled myself into finding this code:

    Code:
    <?php
    // Define your username and password
    $username = "someuser";
    $password = "somepassword";
    
    if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {
    ?>
    
    <h1>Login</h1>
    
    <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <p><label for="txtUsername">Username:</label>
        <br /><input type="text" title="Enter your Username" name="txtUsername" /></p>
    
        <p><label for="txtpassword">Password:</label>
        <br /><input type="password" title="Enter your password" name="txtPassword" /></p>
    
        <p><input type="submit" name="Submit" value="Login" /></p>
    
    </form>
    
    <?php
    }
    else {
    ?>
    
    // put password protected content here
    
    <?php
    }
    ?>
    Now, I thought I was pretty clever, until I tried it. It gives me a nice little password form, but it also displays the page content right along with it. And then, just for kicks and giggles, when I went ahead and entered the username and password I assigned, it took all of the supposedly protected content away and breaks my page apart. Kind of backwards, no?

    Does it matter that there is php in the protected content?

    <sigh>
  • GazMathias
    Recognized Expert New Member
    • Oct 2008
    • 228

    #2
    Odd, seems to work for me, though it complained about an undefined index, but other than that works as intended.

    Comment

    • chelvan
      New Member
      • Aug 2008
      • 90

      #3
      hi
      change your if condition. that means assign your posted values to the variable.

      like this
      Code:
      if($variablename!=$_Post[variablename]){
      //login form
      }
      else{
      //password protected content
      
      }
      and remove 25th and 29th lines.

      hope it will help you.

      cheers
      chel-1

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by chelvan
        hi
        change your if condition. that means assign your posted values to the variable.

        like this
        Code:
        if($variablename!=$_Post[variablename]){
        //login form
        }
        else{
        //password protected content
        
        }
        and remove 25th and 29th lines.

        hope it will help you.

        cheers
        chel-1
        Che, a couple of things:
        1. That code you supplied will give no different output to the original code (minus the errors below)
        2. $_POST has to be uppercase
        3. array indexes should be in quotes $_POST['index']


        Djsjm, the code works fine for me. The only thing I can see a problem with is that you don't check to see if the form was submitted - you'll end up with an array index problem.

        Comment

        • djsjm
          New Member
          • Nov 2008
          • 23

          #5
          hi there! ok... i changed it to this, based on everyone's advice:

          Code:
          <?php
          // Define your username and password
          $username = "xxxxxx";
          $password = "xxxxxx";
          
          if($username!=$_POST['username'] || $password!=$_POST['password']){ 
          //login form 
          
          <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
              <p><label for="username">Username:</label>
              <br /><input type="text" title="Enter your Username" name="username" /></p>
          
              <p><label for="password">Password:</label>
              <br /><input type="password" title="Enter your password" name="password" /></p>
          
              <p><input type="submit" name="Submit" value="Login" /></p>
          
          </form>
          
          }
          else{ 
          //password protected content 
          } 
          ?>
          and got this error message...

          Parse error: syntax error, unexpected '<' (took my address out for now) on line 51

          my line 51 is line 9 above.

          ???

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            You can't output html in php. You either need to close the php block, or change the html into an echo/print statement.

            Comment

            • djsjm
              New Member
              • Nov 2008
              • 23

              #7
              aha!! that's what i thought!!! but i was just following directions. :/

              anyway, i echo'd it and it is still giving me the same error.

              Comment

              • djsjm
                New Member
                • Nov 2008
                • 23

                #8
                and one more thing.... it REALLY hates that "else"... catches it every time and complains about it.

                seems like it needs to be there... and i'm doing it exactly like i've been told... so i have no idea what i'm doing wrong.

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  How is you code looking now, and what exactly does it say when it complains about the "else"?

                  You need to properly separate the PHP and the HTML.
                  It needs to be either:
                  [code=php]
                  <?php
                  if($something == true) {
                  ?>
                  <div>If output</div>
                  <?php
                  } else {
                  ?>
                  <div>Else output</div>
                  <?php
                  }
                  ?>
                  [/code]
                  Or:
                  [code=php]
                  <?php
                  if($something == true) {
                  echo "<div>If output</div>";
                  }
                  else {
                  echo "<div>Else output</div>";
                  }
                  ?>
                  [/code]
                  There is a much greater change of errors in the first example, so I would recommend something more like the second one.

                  Comment

                  Working...