1

私はphpを学ぶのは初めてで、最初のプログラムの1つで、ユーザーとpasswdの配列を使用したログイン機能を備えた基本的なphpWebサイトを作成したいと考えていました。

私の考えは、次のように、ユーザー名をリストパラメーターとして保存し、passwdをコンテンツとして持つことです。

arr = array(username => passwd, user => passwd);

今の私の問題は、ファイル()からどのように読み取ることができるかわからないdata.txtため、配列に追加できることです。

data.txt sample:
username passwd
anotherUSer passwd

でファイルを開き、にfopen保存しました$data

4

5 に答える 5

10

この機能を使用できますfile()

foreach(file("data.txt") as $line) {
    // do stuff here
}
于 2012-06-17T21:30:45.667 に答える
4

このPHPの例を変更します(公式のPHPサイトから取得...常に最初に確認してください!):

$handle = @fopen("/path/to/yourfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

に:

$lines = array();
$handle = @fopen("/path/to/yourfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        lines[] = $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

// add code to loop through $lines array and do the math...

ログインの詳細を暗号化されていないテキストファイルに保存しないでください。このアプローチには重大なセキュリティ問題があります。あなたがPHPを初めて使用することは知っていますが、最善のアプローチは、PHPをDBに保存し、MD5やSHA1などのアルゴリズムを使用してパスワードを暗号化することです。

于 2012-06-17T21:31:28.880 に答える
1

機密情報をプレーンテキストとして保存するのではなく、質問に答えるために、

$txt_file = file_get_contents('data.txt'); //Get the file
$rows = explode("\n", $txt_file); //Split the file by each line

foreach ($rows as $row) {
   $users = explode(" ", $row); //Split the line by a space, which is the seperator between username and password
   $username = $users[0];
   $password = $users[1];
}

このスレッドを見てください。

于 2012-06-17T21:32:03.667 に答える
0

これは、非常に大きなファイルでも機能します。

$handle = @fopen("data.txt", "r");
if ($handle) {
    while (!feof($handle)) { 
        $line = stream_get_line($handle, 1000000, "\n"); 
        //Do Stuff Here.
    } 
fclose($handle);
}
于 2012-06-17T21:29:45.730 に答える
0

file()またはfile_get_contents()を使用して、配列または文字列を作成します。

必要に応じてファイルの内容を処理します

// Put everything in the file in an array
$aArray = file('file.txt', FILE_IGNORE_NEW_LINES);

// Iterate throug the array
foreach ($aArray as $sLine) {

    // split username an password
    $aData = explode(" ", $sLine);

    // Do something with the username and password
    $sName = $aData[0];
    $sPass = $aData[1];
}
于 2012-06-17T21:36:40.170 に答える