Simple password .php page

Discussion in 'PHP' started by txwebdesign, Jan 7, 2006.

  1. #1
    I'm brand new to .php. I understand it will validate data you enter (i.e. username and password)...

    I need a .php validation page like this that validates usernames & passwords to allow access into a wholesale only online store.

    Can someone tell me:

    1. How to write the script?
    2. Where I would store the usernames & passwords?

    Thanks!
    TxWebDesign
     
    txwebdesign, Jan 7, 2006 IP
  2. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    it is simple

    have a form
    <form method="post" action "page.php">
    <input type="text" name="username">
    <input type="password" name="pass">
    <input type="submit" value="Log In">

    then to validate that they are filled in
    
    <?php
    //get the posted data
    $username = $_POST["username"];
    $pass = $_POST["pass"];
    
    //then use IF to find out, having ! says not true
    if (!$username) {
    echo "You forgot to add your username";
    
    }else{
    
    if (!$password) {
    echo "You forgot to add your password";
    
    }elese{
    //Then run the MySQL query and log them in.
    
    PHP:
     
    onlyican.com, Jan 7, 2006 IP
  3. cytech

    cytech Guest

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey, If you have MANY users store the username/passwords in a MySQL database.

    Table in the database:
    
    CREATE TABLE users (
    userId INT(11) NOT NULL AUTO_INCREMENT,
    username CHAR(35) NOT NULL DEFAULT '',
    password CHAR(20) NOT NULL DEFAULT '',
    PRIMARY KEY userId(userId)
    );
    
    Code (markup):

    Then you can do this similar to what only posted.
    
    <form method="post" action "page.php">
    <input type="text" name="username">
    <input type="password" name="pass">
    <input type="submit" value="Log In">
    
    Code (markup):
    page.php will be:
    
    
    <?php
    
    $username=$_POST[username];
    $password=$_POSt[password];
    
    //Be sure you do error checking and validating.
    $username=addslashes($username);
    $username=htmlspecialchars($username);
    $password=addslashes($password);
    $password=htmlspecialchars($password);
    
    if(!eregi("[a-zA-Z_0-9]",$username)){
    echo "BAD USERNAME!";
    exit;
    }
    if(!eregi("[a-zA-Z_0-9]",$password)){
    echo "BAD PASSWORD!";
    exit;
    }
    
    //If all is good
    
    $select=@mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
    if(mysql_num_rows($select)==0){
    echo "Sorry your not a member.";
    }else{
    echo "WELCOME BACK $username!!";
    }
    
    
    Code (markup):
    You will need to connect to the db for the above to work. You should also do more field validating so it is safe for SQL injection. Hope this helps! :)

    P.S Sorry if there are script errors, just wrote it fast.
     
    cytech, Jan 7, 2006 IP
  4. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    One thing to remeber, something I have noticed on a few pages.

    On the registration page, be sure to have password and re-type password.

    As i have said, I have seen a few sites with one password field to reg, you mis type your password, your screwed.
     
    onlyican.com, Jan 7, 2006 IP
  5. cytech

    cytech Guest

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    so true onlyican. So VERY True haha.

    Be sure to have forgot password ;)
     
    cytech, Jan 7, 2006 IP
  6. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    2 ways of doing that, the learner way
    Sends an email to you.

    The better way,

    They enter there name
    Then you run a MySQL query, grab the password, uncode the password, send an email with the username and password.

    Don't go for the secret question thing, because I don't know the answer to any of my secret questions no more.

    with email, then the person must have access to the email to gain access logged in.
     
    onlyican.com, Jan 7, 2006 IP
  7. txwebdesign

    txwebdesign Peon

    Messages:
    123
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for the responses. Based on your input, I thought I should better clarify what I need the script to do...

    So I would like to have an SQL database with usernames & passwords that I can add to and modify at any time (I'll contact my hosting company to figure out how to do this.)

    I would like the form to ask for the username and password, validate the input against the usernames & passwords in the database, and then if they match up, take them to a new URL (with the online restricted-access store.)

    Can someone point me to a sample code that I could tweak for my pages?

    Thank you!
     
    txwebdesign, Jan 8, 2006 IP
  8. smallbusinessboost

    smallbusinessboost Guest

    Messages:
    40
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Just look around at google for free php member scripts.
    You can find pretty much anything you want in search engines if you look hard enough.
    I typed 'free php scripts' in google and here is the first listing:
    http://www.free-php.net/scripts.php?id=8
     
    smallbusinessboost, Jan 9, 2006 IP