Php read password file

PHP: Read users and passwords from a txt file

I’m trying to extract each of these pairs and use them in a script. I’m not really a php developer and have almost zero skills in that area but I could work my way with functions and I know the basic syntax.

Thanks in advance for your attention.

Best Solution

Assuming your password file contains valid lines and empty lines only

EDIT: If your php is configured on strict level:

$lines = file('file.txt'); $credentials = array(); foreach($lines as $line) < if(empty($line)) continue; // whole line $line = trim(str_replace(": ", ':', $line)); $lineArr = explode(' ', $line); // username only $username = explode(':', $lineArr[0]); $username = array_pop($username); // password $password = explode(':', $lineArr[1]); $password = array_pop($password); // putting them together $credentials[$username] = $password; >print_r($credentials); 
C# – How to create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office

You can use a library called ExcelLibrary. It’s a free, open source library posted on Google Code:

Читайте также:  Настройка sublime для php

This looks to be a port of the PHP ExcelWriter that you mentioned above. It will not write to the new .xlsx format yet, but they are working on adding that functionality in.

It’s very simple, small and easy to use. Plus it has a DataSetHelper that lets you use DataSets and DataTables to easily work with Excel data.

ExcelLibrary seems to still only work for the older Excel format (.xls files), but may be adding support in the future for newer 2007/2010 formats.

You can also use EPPlus, which works only for Excel 2007/2010 format files (.xlsx files). There’s also NPOI which works with both.

There are a few known bugs with each library as noted in the comments. In all, EPPlus seems to be the best choice as time goes on. It seems to be more actively updated and documented as well.

Also, as noted by @АртёмЦарионов below, EPPlus has support for Pivot Tables and ExcelLibrary may have some support (Pivot table issue in ExcelLibrary)

Here some example code for ExcelLibrary:

Here is an example taking data from a database and creating a workbook from it. Note that the ExcelLibrary code is the single line at the bottom:

//Create the data set and table DataSet ds = new DataSet("New_DataSet"); DataTable dt = new DataTable("New_DataTable"); //Set the locale for each ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture; dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture; //Open a DB connection (in this example with OleDB) OleDbConnection con = new OleDbConnection(dbConnectionString); con.Open(); //Create a query and fill the data table with the data from the DB string sql = "SELECT Whatever FROM MyDBTable;"; OleDbCommand cmd = new OleDbCommand(sql, con); OleDbDataAdapter adptr = new OleDbDataAdapter(); adptr.SelectCommand = cmd; adptr.Fill(dt); con.Close(); //Add the table to the data set ds.Tables.Add(dt); //Here's the easy part. Create the Excel worksheet from the data set ExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls", ds); 

Creating the Excel file is as easy as that. You can also manually create Excel files, but the above functionality is what really impressed me.

Java – How to create a Java string from the contents of a file

Read all text from a file

Java 11 added the readString() method to read small files as a String , preserving line terminators:

String content = Files.readString(path, StandardCharsets.US_ASCII); 

For versions between Java 7 and 11, here’s a compact, robust idiom, wrapped up in a utility method:

static String readFile(String path, Charset encoding) throws IOException

Read lines of text from a file

Java 7 added a convenience method to read a file as lines of text, represented as a List . This approach is «lossy» because the line separators are stripped from the end of each line.

List lines = Files.readAllLines(Paths.get(path), encoding); 

Java 8 added the Files.lines() method to produce a Stream . Again, this method is lossy because line separators are stripped. If an IOException is encountered while reading the file, it is wrapped in an UncheckedIOException , since Stream doesn’t accept lambdas that throw checked exceptions.

try (Stream lines = Files.lines(path, encoding))

This Stream does need a close() call; this is poorly documented on the API, and I suspect many people don’t even notice Stream has a close() method. Be sure to use an ARM-block as shown.

If you are working with a source other than a file, you can use the lines() method in BufferedReader instead.

Memory utilization

The first method, that preserves line breaks, can temporarily require memory several times the size of the file, because for a short time the raw file contents (a byte array), and the decoded characters (each of which is 16 bits even if encoded as 8 bits in the file) reside in memory at once. It is safest to apply to files that you know to be small relative to the available memory.

The second method, reading lines, is usually more memory efficient, because the input byte buffer for decoding doesn’t need to contain the entire file. However, it’s still not suitable for files that are very large relative to available memory.

For reading large files, you need a different design for your program, one that reads a chunk of text from a stream, processes it, and then moves on to the next, reusing the same fixed-sized memory block. Here, «large» depends on the computer specs. Nowadays, this threshold might be many gigabytes of RAM. The third method, using a Stream is one way to do this, if your input «records» happen to be individual lines. (Using the readLine() method of BufferedReader is the procedural equivalent to this approach.)

Character encoding

One thing that is missing from the sample in the original post is the character encoding. There are some special cases where the platform default is what you want, but they are rare, and you should be able justify your choice.

The StandardCharsets class defines some constants for the encodings required of all Java runtimes:

String content = readFile("test.txt", StandardCharsets.UTF_8); 

The platform default is available from the Charset class itself:

String content = readFile("test.txt", Charset.defaultCharset()); 

Note: This answer largely replaces my Java 6 version. The utility of Java 7 safely simplifies the code, and the old answer, which used a mapped byte buffer, prevented the file that was read from being deleted until the mapped buffer was garbage collected. You can view the old version via the «edited» link on this answer.

Related Question

Источник

php — Read users and passwords from a txt file

I have a txt file which looks like this

USER: user46226234 PASS: #)(HF(WE*FHWE

USER: user49503420594 PASS: yellowbird

USER: user449202934 PASS: dog

I’m trying to extract each of these pairs and use them in a script.

Answer

Solution:

Assuming your password file contains valid lines and empty lines only

$lines = file('file.txt'); $credentials = array(); foreach($lines as $line) < if(empty($line)) continue; // whole line $line = trim(str_replace(": ", ':', $line)); $lineArr = explode(' ', $line); // username only $username = explode(':', $lineArr[0]); $username = array_pop($username); // password $password = explode(':', $lineArr[1]); $password = array_pop($password); // putting them together $credentials[$username] = $password; >print_r($credentials); 

Answer

Solution:

You may want to try file_get_contents() see it here

In case you want to use config files such .ini files you want to also check parse_ini_files() check it here

Share solution ↓

Additional Information:

Didn’t find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Similar questions

Find the answer in similar questions on our website.

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

About the technologies asked in this question

PHP

PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/

Welcome to programmierfrage.com

programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.

Get answers to specific questions

Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.

Help Others Solve Their Issues

Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.

Источник

php — Read users and passwords from a txt file

I have a txt file which looks like this

USER: user46226234 PASS: #)(HF(WE*FHWE

USER: user49503420594 PASS: yellowbird

USER: user449202934 PASS: dog

I’m trying to extract each of these pairs and use them in a script.

Answer

Solution:

Assuming your password file contains valid lines and empty lines only

$lines = file('file.txt'); $credentials = array(); foreach($lines as $line) < if(empty($line)) continue; // whole line $line = trim(str_replace(": ", ':', $line)); $lineArr = explode(' ', $line); // username only $username = explode(':', $lineArr[0]); $username = array_pop($username); // password $password = explode(':', $lineArr[1]); $password = array_pop($password); // putting them together $credentials[$username] = $password; >print_r($credentials); 

Answer

Solution:

You may want to try file_get_contents() see it here

In case you want to use config files such .ini files you want to also check parse_ini_files() check it here

Share solution ↓

Additional Information:

Didn’t find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Similar questions

Find the answer in similar questions on our website.

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

About the technologies asked in this question

PHP

PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/

Welcome to programmierfrage.com

programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.

Get answers to specific questions

Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.

Help Others Solve Their Issues

Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.

Источник

Reading Passwords from STDIN in PHP

Hi Everyone, Recently, I had to create an artisan command that submitted some data on behalf of a user. I needed it to have access to the user’s password to do that. My first thought was to store the password in an environment variable. I really did not want to do that, and preferred to make the user of the script enter the password every time it was necessary. Secure Password Prompt

How it was done

It was important to ensure that the password was entered securely (invisible too), so I looked into how to and here’s what I came up with.

private function getPassword($prompt = "Enter Password:")  echo $prompt; system('stty -echo'); $password = trim(fgets(STDIN)); system('stty echo'); return $password; > 

How I understand it

  • stty -echo sets the echo property to false, so the terminal will NOT echo the input it receives to the screen, rendering the password invisible
  • fgets(. ) retrieves input from a stream interface (STDIN).
  • STDIN is the input stream that fgets reads the input from
  • stty echo restores the echo property to true, so we can now see output on the screen again

I hope you learned something from this, as I did. Happy Coding. 😍

Источник

Оцените статью