phpfilecookieslogin-page

how to make a file based user login page


this thing really blown my mind up i want to design a page with a text box where my clients can enter their username and by using php i want to tell the page to check if there is a username such as that in a file named locationn.html if existed create a cookie and let him in another page if not add a new line to the file containing the user name entered.

this is my code in this code "unamec" is the name of the cookie and "$user" is the user name and "umname" is the name of the username text box which its value is sent to the page itself using a post method.

<?php
if(isset($_POST["uname"])){
$user=$_POST["uname"];
$pass=$_POST["passs"];
$see=file_get_contents("locationn.html");
$lines=explode("\n",$see);
foreach($lines as $line){
if($line == $user){
setcookie("unamec",$user,time()+86400,"/");
echo '<script>window.location="main.html";</script>';
}
}
}
?>

Solution

  • I can help you with the storage and retrieval of a username from a file. You could adapt this and couple with session management, to achieve your aims.

    <?php
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
        $username = isset($_POST['username']) ? $_POST['username'] : null;
        if($username !== preg_replace('/[^a-zA-Z0-9]+/', '', $username))
            throw new Exception('Invalid username.');
        $userStore = new UserStore('/tmp/creds.txt');
        if(! $userStore->isStored($username)) {
            if($userStore->storeUsername($username)) {
                echo $username . ' stored.';
            }
        } else {
            echo $username . ' is on file.';
        }
    }
    
    
    class userStore
    {
        public $fp;
        public $filename;
    
        public function __construct($filename)
        {
            $this->filename = $filename;
            if(!file_exists($this->filename))
                file_put_contents($this->filename, null);
            $this->fp = fopen($this->filename, "r+");
        }
    
        public function isStored($username) {
            $username_exists = false;
    
            if(! $size = filesize($this->filename))
                return false;
            $contents  = fread($this->fp, $size);
    
            $lines = array_filter(explode("\n", $contents));
            foreach($lines as $line) {
                if(trim($line) == $username) {
                    $username_exists = true;
                    break;
                }
            }
    
            return $username_exists;
        }
    
        public function storeUsername($username)
        {
            $fp = $this->fp;
            if (flock($fp, LOCK_EX)) {
                fwrite($fp, "$username\n");
                fflush($fp);
                flock($fp, LOCK_UN);
            } else {
                return false;
            }
    
            return true;
        }
    }
    ?>
    <form method='POST'>
        <input type='text' name='username'>
        <input type='submit' value='login'>
    </form>