problem with file

Discussion in 'PHP' started by ssimon171078, May 15, 2015.

  1. #1
    i created simple login php script that check username and password and save it in file log.txt.
    problem is simple when it wrote to file i need that script write new line each login access:
    login pass
    username= user
    login fail
    username= test
    my script is:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title>PHP form test comment</title>
    <!-- define some style elements-->
    <style>
    
    textarea {
      text-align:left;
      margin-left:+40px;
      }
    input#name{
      margin-left:+15px;
    }
    input#password{
        margin-left:+5px;
    }
    input#id{
        margin-left:+54px;
    }
    input#submit {
        margin-left:+54px;
    }
    
    </style>   
    </head>
    
    <body>
    <?php
    $test=""; $list="";
    $name="";
    $username="";
    $password="";
    $log="log.txt";
    $fd=fopen($log,"a+");
    $id="";
    $flag=1;
    //$test = $_POST['name'];
    //echo $test;
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
     
      $username = test_input($_POST["username"]);
      if (empty($username)) {
      echo   "Username is required" ;}
      $password = test_input($_POST["password"]);
      if (empty($password)) {
      echo   "Password is required" ;}
     
    }
    if (empty($_POST["username"]) || (empty ($_POST["password"]))){
        //$login="login_fail";
        $login = "login fail\n";}
        else
         //$login="login_pass";   
          $login = "login pass\n";
        $login_details=$_POST["username"];
        fwrite($fd,$login);
        fwrite ($fd," ");
        fwrite($fd,"username=");
        fwrite($fd," ");
        fwrite ($fd,$login_details);
        fwrite ($fd,"  ");
        fclose($fd);
    
    function test_input($data) {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }
    
    ?>
    
    <form id="test" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
       
       
    
      Username: <input type='text'  name='username' id='usename' ><br />
    
      Password: <input type='text' name='password' id='password' ><br />
      
      <input type='submit' value='Submit' id="submit" />
       
        </form>
    
    </body>
    </html>
    PHP:

     
    ssimon171078, May 15, 2015 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #2
    Anveto, May 15, 2015 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    You also don't want to do that to passwords, what you're doing with test_input() - basically, what that does is reduce the complexity of the passwords - granted, I can't see that you're even hashing anything (thus far, at least), hence the passwords are basically a joke anyway, but you shouldn't reduce the complexity for no reason.
     
    PoPSiCLe, May 15, 2015 IP
  4. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #4
    
    function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return substr($data, -1);
    }
    
    PHP:
    Haha....

    But yea, you shouldn't do this. Better to use Blowfish (http://www.the-art-of-web.com/php/blowfish-crypt/) or something to hash your passwords and then check new entries against the stored hash.
     
    Anveto, May 15, 2015 IP