password protected pages

Discussion in 'PHP' started by rishirajsingh, Aug 7, 2007.

  1. #1
    I want to create password protected pages where administrator can login and check details of request of products. I am currently using the following script.

    <?php
    
    // Define your username and password
    $username = "username";
    $password = "password";
    
    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 {
    
    ?>
    
    <?php 
    $con = mysql_connect("192.168.2.7", "dbname", "password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("indiamar_rishi", $con);
    
    $result = mysql_query('SELECT * FROM `order` LIMIT 0, 30 ');
    
    echo "<table border='1'>
    <tr>
    <th>Project Name</th>
    <th>Project Description</th>
    <th>Contact Person</th>
    <th>Contact Email</th>
    <th>Contact number</th>
    <th>Company Name</th>
    <th>Country</th>
    
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['txtprojectname'] . "</td>";
      echo "<td>" . $row['txtdescription'] . "</td>";
    echo "<td>" . $row['txtname'] . "</td>";
    echo "<td>" . $row['txtemail'] . "</td>";
    echo "<td>" . $row['txtphone1'] . "</td>";
    echo "<td>" . $row['txtcompany'] . "</td>";
    echo "<td>" . $row['txtcountry'] . "</td>";
      echo "</tr>";
      }
    
    echo "</table>";
    
    mysql_close($con);
    ?>
    
    <?php
    
    }
    
    ?> 
    Code (markup):
    now i want to add extra features like admin can sort results by "submitted yesterday, last 7days, all time" first 50results, 50-100 results, back and next button"

    I don't think all this is possible with this script cause it will ask for password again and again.

    so please suggest any suitable script or logic that can help me.
    I would prefer If I can display results with all features on same page.
    I don't mind reading any topic or script related to this. I an newbie but I can edit the codes as per my requirement.
     
    rishirajsingh, Aug 7, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It would be far easier to just let the server manage it through .htaccess?

    Failing that, you could store the username and password in a short life cookie or in the session.
     
    ecentricNick, Aug 7, 2007 IP
  3. rishirajsingh

    rishirajsingh Banned

    Messages:
    286
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks buddy.

    If I password protect a page using .htacess then is it ask for password again on page reload?
     
    rishirajsingh, Aug 7, 2007 IP
  4. just-4-teens

    just-4-teens Peon

    Messages:
    3,967
    Likes Received:
    168
    Best Answers:
    0
    Trophy Points:
    0
    #4
    it shouldnt re-ask for the password untill your restart your browser session.
     
    just-4-teens, Aug 7, 2007 IP
  5. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    rishirajsingh is correct, it will (or at least should) remember the user is logged in until the browser is closed (or until a timeout).

    i use it on some of my stuff and it works really well for protecting admin areas and the like.

    it's very easy to implement and keeps your security out of the php stuff.
     
    ecentricNick, Aug 7, 2007 IP
  6. rishirajsingh

    rishirajsingh Banned

    Messages:
    286
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks friends,
    I am using .htacess and its working fine.

    I wanted to add features like admin can sort results by "submitted yesterday, last 7days, all time" first 50results, 50-100 results, back and next button"
    I have made a form with calendar that can take the from and to date value and display data according to it.
    How can i create button for next 50, previous 50, and links like
    Page 1 2 3 4 5 6 7 8 9 10

    What would be the best practice to do that?
     
    rishirajsingh, Aug 8, 2007 IP
  7. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Generally, if using a mysql database, the technique is to use the LIMIT clause. Here you can specify the start row and the number of rows to return. So...

    SELECT * FROM <table> LIMIT 0,50 would return the first 50 rows...
    SELECT * FROM <table> LIMIT 50,50 would return the next 50 rows...

    This makes it quite easy to code up your Page 1,2,3, etc links.

    Usually, another query is required to count the total number of rows so that you can determine how many page 1,2,3's etc to place on the page.

    Alternately, it is possible to fast forward and rewind through a result set but it's not as clear to read the code and so not as obvious as the former solution. Speed wise there isn't much in it anyway.
     
    ecentricNick, Aug 8, 2007 IP