SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    Your daddy. WALoeIII's Avatar
    Join Date
    Apr 2001
    Location
    USA
    Posts
    526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am trying to password protect a page using this simple code, I am planning to add in a query to MySQL, but I wanted to make sure I had the basic syntax working, and guess what...I dont!
    anyways, I have checked this file backwards and forwards to no avail, so see what isn't working for me:

    <?php
    function unauthorized()
    {
    $title="JuniorSailing.com Programs Bucktown Coaches Authorization";
    include("/usr/local/etc/httpd/vhosts/www.juniorsailing.com/includes/header.php");
    ?>
    <table cellpadding="0" cellspacing="5" width="100%">
    <tr>
    <td valign="top">
    <table width="100%" cellpadding="2" cellspacing="0" border="0">
    <tr>
    <td valign="top">
    <font class="main_header">Bucktown Juniors Coaches</font>
    </td>
    </tr>
    <tr>
    <form method="post" action="
    <?php
    echo "$PHP_SELF";
    ?>
    ">
    <table cellpadding="2" cellspacing="0">
    <tr>
    <td align="right"><font class="standard">Username/font></td><td><input type="text" name="username" size="20"></td>
    </tr>
    <tr>
    <td align="right"><font class="standard">Password/font></td><td><input type="password" name="password" size="20"></td>
    </tr>
    <tr>
    <td>&nbsp;</td>

    <td align="left" valign="top">
    <input type="submit" name="Submit" value="Go!">
    <input type="reset" name="Reset" value="Clear">
    </td>
    </tr>
    </table>
    </form>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    <?php
    include("/usr/local/etc/httpd/vhosts/www.juniorsailing.com/includes/footer.php");
    }
    function authorized()
    {
    $title="JuniorSailing.com Programs Bucktown Coaches Authorized";
    include("/usr/local/etc/httpd/vhosts/www.juniorsailing.com/includes/header.php");
    ?>
    <table cellpadding="0" cellspacing="5" width="100%">
    <tr>
    <td valign="top">
    <table width="100%" cellpadding="2" cellspacing="0" border="0">
    <tr>
    <td valign="top">
    <font class="main_header">Bucktown Juniors Coaches</font>
    </td>
    </tr>
    <tr>
    <td valign="top"><font class="main_header">Post Announcement</font><br>
    <form method="post" action="/cgi-bin/postannouncements.cgi">
    <table cellpadding="2" cellspacing="0">
    <tr>
    <td align="right"><font class="standard">Name/font></td><td><input type="text" name="name" size="20"></td>
    </tr>
    <tr>
    <td align="right"><font class="standard">Section/font></td><td><SELECT NAME="class"><OPTION VALUE="none" SELECTED>Choose Section
    <Option value="grnflt">Green Fleet
    </select></td>
    </tr>
    <tr>
    <td align="right" valign="top">
    <font class="standard">Announcement/font>
    </td>
    <td>

    <textarea name="message" cols="30" rows="7"></textarea>
    </td>
    </tr>
    <tr>
    <td>&nbsp;</td>

    <td align="left" valign="top">
    <input type="submit" name="Submit" value="Post it">
    <input type="reset" name="Reset" value="Clear form">
    </td>
    </tr>
    </table>
    </form>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    <?php
    include("/usr/local/etc/httpd/vhosts/www.juniorsailing.com/includes/footer.php");
    }

    if (isset($PHP_POST_VARS['password'])) {

    if ($PHP_POST_VARS['username']=="test" and $PHP_POST_VARS['password']=="password") {
    echo authorized();
    } else {
    echo unauthorized();
    }
    } else {
    echo unauthorized();
    }
    ?>
    It just loops me through the unauthorized page...forever, and also, my ads, which are included in my header, don't show upm and it greets as guest, not my log in name (It greets correctly on all other pages) It seems that my header isn't being read correctly, well actually, the php isn't being parsed. I CANNOT get it to work... Any Ideas?
    Last edited by WALoeIII; Apr 18, 2001 at 21:09.

  2. #2
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is the authorized() and unauthorized() function did not return anything for the echo() to print, and it would be a messy process to make that happen. Try rewriting it by escaping to HTML at the appropriate places. Here, I took a few minutes and rewrote your code in a fashion that should work. It might not be perfect, but if you've read the PHP manual, or read the right parts, you should understand what I'm trying to do. I hope you can still understand it. It was redone a bit hastily, as I wanted to get to bed.

    PHP Code:
    <?PHP
    if (isset($PHP_POST_VARS['password'])) {
     if (
    $PHP_POST_VARS['username']=="test" and $PHP_POST_VARS['password']=="password") {
      
    // User is authorized
      
    $title="JuniorSailing.com  Programs  Bucktown  Coaches  Authorized"
      include(
    "/usr/local/etc/httpd/vhosts/www.juniorsailing.com/includes/header.php"); 
    ?>
      <table cellpadding="0" cellspacing="5" width="100%">
      <tr>
      <td valign="top"> 
      <table width="100%" cellpadding="2" cellspacing="0" border="0">
      <tr>
      <td valign="top">
      <font class="main_header">Bucktown Juniors Coaches</font>
      </td>
      </tr>
      <tr>
      <td valign="top"><font class="main_header">Post Announcement</font><br>
      <form method="post" action="/cgi-bin/postannouncements.cgi">
      <table cellpadding="2" cellspacing="0">
      <tr>
      <td align="right"><font class="standard">Name:</font></td><td><input type="text" name="name" size="20"></td>
      </tr>
      <tr>
      <td align="right"><font class="standard">Section:</font></td><td><SELECT NAME="class"><OPTION VALUE="none" SELECTED>Choose Section 
      <Option value="grnflt">Green Fleet 
      </select></td> 
      </tr> 
      <tr> 
      <td align="right" valign="top"> 
      <font class="standard">Announcement:</font> 
      </td> 
      <td> 
      
      <textarea name="message" cols="30" rows="7"></textarea> 
      </td> 
      </tr> 
      <tr> 
      <td> </td> 
      
      <td align="left" valign="top"> 
      <input type="submit" name="Submit" value="Post it"> 
      <input type="reset" name="Reset" value="Clear form"> 
      </td> 
      </tr> 
      </table> 
      </form> 
      </td> 
      </tr> 
      </table> 
      </td> 
      </tr> 
      </table> 
    <?php 
      
    include("/usr/local/etc/httpd/vhosts/www.juniorsailing.com/includes/footer.php");
      
    // End authorized

      
    } else {
      
    // Unauthorized
      
    $title="JuniorSailing.com  Programs  Bucktown  Coaches  Authorization";
      include(
    "/usr/local/etc/httpd/vhosts/www.juniorsailing.com/includes/header.php");
     
    ?>
      <table cellpadding="0" cellspacing="5" width="100%">
      <tr> 
      <td valign="top"> 
      <table width="100%" cellpadding="2" cellspacing="0" border="0"> 
      <tr> 
      <td valign="top"> 
      <font class="main_header">Bucktown Juniors Coaches</font> 
      </td> 
      </tr> 
      <tr> 
      <form method="post" action="<?php echo "$PHP_SELF"?>">
      <table cellpadding="2" cellspacing="0"> 
      <tr> 
      <td align="right"><font class="standard">Username:</font></td><td><input type="text" name="username" size="20"></td> 
      </tr> 
      <tr> 
      <td align="right"><font class="standard">Password:</font></td><td><input type="password" name="password" size="20"></td> 
      </tr> 
      <tr> 
      <td> </td> 
      
      <td align="left" valign="top"> 
      <input type="submit" name="Submit" value="Go!"> 
      <input type="reset" name="Reset" value="Clear"> 
      </td> 
      </tr> 
      </table> 
      </form> 
      </td> 
      </tr> 
      </table> 
      </td> 
      </tr> 
      </table> 
      <?php include("/usr/local/etc/httpd/vhosts/www.juniorsailing.com/includes/footer.php");
      }
     } else {
     
    // Unauthorized
     
    $title="JuniorSailing.com  Programs  Bucktown  Coaches  Authorization";
     include(
    "/usr/local/etc/httpd/vhosts/www.juniorsailing.com/includes/header.php");
     
    ?>

     <table cellpadding="0" cellspacing="5" width="100%">
     <tr> 
     <td valign="top"> 
     <table width="100%" cellpadding="2" cellspacing="0" border="0"> 
     <tr> 
     <td valign="top"> 
     <font class="main_header">Bucktown Juniors Coaches</font> 
     </td> 
     </tr> 
     <tr> 
     <form method="post" action="<?php echo "$PHP_SELF"?>">
     <table cellpadding="2" cellspacing="0"> 
     <tr> 
     <td align="right"><font class="standard">Username:</font></td><td><input type="text" name="username" size="20"></td> 
     </tr> 
     <tr> 
     <td align="right"><font class="standard">Password:</font></td><td><input type="password" name="password" size="20"></td> 
     </tr> 
     <tr> 
     <td> </td> 
     
     <td align="left" valign="top"> 
     <input type="submit" name="Submit" value="Go!"> 
     <input type="reset" name="Reset" value="Clear"> 
     </td> 
     </tr> 
     </table> 
     </form> 
     </td> 
     </tr> 
     </table> 
     </td> 
     </tr> 
     </table> 
     <?php include("/usr/local/etc/httpd/vhosts/www.juniorsailing.com/includes/footer.php"); 
     }
     
    ?>
    Ed Rands

  3. #3
    Your daddy. WALoeIII's Avatar
    Join Date
    Apr 2001
    Location
    USA
    Posts
    526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I understand

    I understand you completely, thanks for the code, I was thinking I could save time by saving them in functions, but I see that donesn't work!

  4. #4
    Your daddy. WALoeIII's Avatar
    Join Date
    Apr 2001
    Location
    USA
    Posts
    526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ARGH!!!

    Still doesn't work, it loops me forever on the login page, do I have the condition for the if statement correct?

  5. #5
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another thing you could do is make a seperate page for include(), if authorization fails, and follow the include() with exit(). If authorization passes, the code will continue on with the first page.

    Here's a rough example...

    PHP Code:
    <?PHP
     
    if ($username != "correct" or $password != "correct") {
      include (
    "login.php");
      exit();
     } else if (
    $username == "correct" and $password == "correct") {
     
    ?>
     <!-- Main content goes here -->
    login.php would be a file that could stand alone, and does whatever's needed to get the right username and password.
    Ed Rands

  6. #6
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: ARGH!!!

    Originally posted by Snoozy
    Still doesn't work, it loops me forever on the login page, do I have the condition for the if statement correct?
    Lemme see. I've more time and I'm more awake than I was last night, so I'll give it a better go. I'll be back in a bit.
    Ed Rands

  7. #7
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found the problem! Results of posting a form are returned
    in $HTTP_POST_VARS, not $PHP_POST_VARS. In fact,
    there's no reference to $PHP_POST_VARS in the PHP manual.
    Last edited by Percipient; Apr 19, 2001 at 11:18.
    Ed Rands

  8. #8
    Your daddy. WALoeIII's Avatar
    Join Date
    Apr 2001
    Location
    USA
    Posts
    526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ok i'm an idiot

    I hate it when I do blatently stupid things like that, let me give it a try....

  9. #9
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, even the best of us do it, now and then. I think you could find a post or two of mine, here in the forums, where my problem had an answer that was so obvious it made me want to bang my head on my mousepad. I need one of those Dilbert mousepads that have the target rings and the text "Bang head here," or something like that.
    Ed Rands

  10. #10
    Your daddy. WALoeIII's Avatar
    Join Date
    Apr 2001
    Location
    USA
    Posts
    526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    LOL

    works perfectly,

    This was my FIRST script, so I sorta have an excuse, since I already have this one down, just give me an idea of how I would have it remeber if someone logged it?

    Set a cookie?

  11. #11
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An excellent and completely valid excuse.

    To log out, you wouldn't do anything with cookies, since logging in had nothing to do with cookies.

    I dunno if $HTTP_POST_VARS automatically empties itself or not, after it's done. I think if you just left the page for another, and returned (sounds like a soap opera ), you would have to log in again. Also, if you added...
    Code:
    <a href="<?PHP $PHP_SELF ?>">Log Out</a>
    ...I think you would get the desired effect.

    Doing authentication with cookies is a whole 'nother kettle of smelly fish. One of which I'm not very experienced in, yet. However, as I think I mention earlier, I'm working on such a system, using cookies. If you like, when I'm finished, and have made sure the code works, I'll pass it on to you, if you want?

    Also, keep in mind that the system you're using now, as I read it, requires that a person log in seperately, for every page. It's really only functional for one page needs. Cookies, on the other hand, let you easily span numerous pages.
    Last edited by Percipient; Apr 19, 2001 at 19:01.
    Ed Rands

  12. #12
    Your daddy. WALoeIII's Avatar
    Join Date
    Apr 2001
    Location
    USA
    Posts
    526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ok

    I know that this only protects only one page, I am talking about experimenting with PHP4's sessions, and then setting a cookie at the end. I don't know, I'll play with it, and tell you what I get. Just PM me when you are done with all that stuff.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •