如何从php中的文本文件中获取值

时间:2016-06-04 23:49:22

标签: php arrays foreach comparison

我有脚本在页面php的值和txt文件中的数据存储之间进行比较,然后它会做一些特殊的代码。

txt文件的内容(account.txt)

F: user pass { expire=date; afexpire=date; email=email@gmail.com; Country=Germani; visit_from=none; ip=none; hosted=none }
F: mike fghg58g { expire=2016-05-24; afexpire=2015-5-24 17; email=mike@gmail.com; Country=uk; visit_from=none; ip=none; hosted=none }
F: adresson f5849dh9d { expire=2016-11-01; afexpire=2015-11-01 17; email=mike@gmail.com; Country=Germani; visit_from=none; ip=none; hosted=none }

我的剧本

<?php

$user = "Mike"; // user that is inserted in page form
$email = "mike@gmail.com"; // email that is inserted in page form

$userFile = "Mike"; // user in txt file
$emailFile = "mike@gmail.com"; // email in txt file

if( $user == $userFile && $email == $emailFile ) {

    echo "The user and email is used";

} elseif( $user == $userFile && $email != $emailFile ) {

    echo "The user is used";

} else{

    // do special code

}

我不知道如何从路径读取文件txt并将文件中的用户和电子邮件更改为值以使其成为

$userFile = "Mike"; // user in txt file (account.txt)
$emailFile = "mike@gmail.com"; // email in txt file (account.txt)

这是我的spcial脚本,它在(account.txt)

中输出
<?php
if (isset($_POST["g-recaptcha-response"])) {
    $name = $_POST['name'];
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $country = $_POST['country'];
    $plan = $_POST['plan'];
    $quantity = $_POST['quantity'];
    $payment = $_POST['payment'];
    $reciever = $_POST['reciever'];
    $captcha = $_POST['g-recaptcha-response'];
    $message_user = $_POST['message'];
    $serverip = $_POST['REMOTE_ADDR'];
    $to = 'sup.alphas@gmail.com';
    $parts = explode("@", $email);
    $sufemail = $parts[0];
    // $sufemail = substr(strstr($email, '@'), 1);  for domain//
    $subject_form = 'Request new account by '.$sufemail.'';
    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }
    // Check if name has been entered
    if (!$_POST['user']) {
        $errUser = 'Please enter your username';
    }
    // Check if name has been entered
    if (!$_POST['pass']) {
        $errPass = 'Please enter your password';
    }
    // Check if email has been entered and is valid
    if (!$_POST['email']) {
        $errEmail = 'Please enter a valid email address';
    }
    if (!$_POST['country']) {
        $errCountry = 'Please enter your country';
    }
    if (!$_POST['plan']) {
        $errPlan = 'Please enter your plan';
    }
    if (!$_POST['quantity']) {
        $errQuantity = 'Please enter your quantity';
    }
    if (!$_POST['payment']) {
        $errPayment = 'Please enter your method of payment';
    }
    if (!$_POST['g-recaptcha-response']) {
        $errCaptcha = 'Please enter captcha';
    }
    // If there are no errors, send the email
    if (!$errName && !$errUser && !$errPass && !$errEmail && !$errCountry && !$errPlan && !$errQuantity && !$errPayment && !$errCaptcha) {
        // Start Create new account //
        $dateadd = date('Y-m-d', strtotime("$plan"));
        $datetry = date('Y-m-d H', strtotime("+1 day"));
        $handle = fopen('/usr/www/users/alphacz/alpha/phpm/account.cfg', 'a');
        fwrite($handle, 'F: ' . $_POST["user"] . ' ' . $_POST["pass"] . ' { expire=' . $datetry . '; afexpire=' . $dateadd . '; email=' . $email . '; Country=' . $country . '; visit_from=none; ip=none; hosted=' . $_POST['REMOTE_ADDR'] . " }\r\n");
        fclose($handle);

请帮帮我

谢谢

2 个答案:

答案 0 :(得分:2)

您可以在大海捞针检查您的帐户文件。 用户名模式是&#34; F:{user}通过{...&#34; 电子邮件模式是&#34 ;;电子邮件= {电子邮件};国家=&#34; 注意:您还必须认为此抽样检查关键字不敏感,所以 在搜索和插入时,您应该将关键字转换为小写

$user_used = userExists('Mike');
$email_used = emailExists('mike@gmail.com');
if ($user_used && $email_used)
{
    echo 'The user and email is used';
}
elseif ($user_used) {
    echo 'The user is used';
}
else
{
    //do special code
}
function userExists($user)
{
    return (exec('grep ' . escapeshellarg('F: ' . $user . ' ') . ' {file-path}'));
}
function emailExists($email)
{
    return (exec('grep ' . escapeshellarg('; email=' . $email . ';') . ' {file-path}'));
}

答案 1 :(得分:-2)

我们可以修改您当前的代码以从文件中读取,但我们首先重写您写入文件的代码,因此之后将更容易从文件中读取。
(因为你只显示写入文件的部分脚本,我只能重写那部分。)

<强>更改

  1. 文件的JSON格式

    我们不会以自定义格式将数据写入文件,而是将数据保存在JSON format中。您可以轻松地在PHP中使用JSON,因为它有built-in functions可以使用它。

  2. $_POST$_SERVER

    在某些时候你使用$_POST['REMOTE_ADDR'],但我认为你想使用$_SERVER["REMOTE_ADDR"]。请参阅:http://php.net/manual/en/reserved.variables.server.php

  3. !$XY

    虽然使用!$XY作为条件可能有效,但它不是很实用。因为它只是否定了值,然后检查它是否是真值或假值,并且应该输入if语句。因此,我建议您使用!empty()检查您的输入是否已设置且不为空。

  4. <强>代码

    <?php
    
        if (isset($_POST["g-recaptcha-response"])){
    
            $checkPostIndices = ["name", "user", "pass", "email", "phone", "country", "plan", "quantity", "payment", "reciever", "g-recaptcha-response", "message"];
            $data = [];
    
            $errors = [];
            $errorMessages = [
                              "name" => "Please enter your name",
                              "user" => "Please enter your username",
                              "pass" => "Please enter your password",
                              "email" => "Please enter a valid email address",
                              "phone" => "Please enter your phone number",
                              "country" => "Please enter your country",
                              "plan" => "Please enter your plan",
                              "quantity" => "Please enter your quantity",
                              "payment" => "Please enter your method of payment",
                              "reciever" => "Please eneter a reciever",
                              "g-recaptcha-response" => "Please enter captcha",
                              "message" => "Please enter a message",
                             ];
    
    
            foreach($checkPostIndices as $index){
    
                if(!empty($_POST[$index])){
                    $data[$index] = $_POST[$index];    
                } else {
                    $errors[] = $errorMessages[$index];
                }
    
            }
    
            $data["serverip"] = $_SERVER["REMOTE_ADDR"];
    
            $to = "sup.alphas@gmail.com";
            $sufemail = explode("@", $data["email"])[0];
            $subject_form = "Request new account by " . $sufemail;
    
    
            if(!empty($errors)){
    
                $fileData = array_intersect_key($data, ["user", "pass", "email", "country", "serverip"]);
                $fileData["dateadd"] = date("Y-m-d", strtotime($data["plan"]));
                $fileData["datetry"] = date("Y-m-d H", strtotime("+1 day"));
    
                $file = file_get_contents("/usr/www/users/alphacz/alpha/phpm/account.cfg");
                $file = empty($file) ? [] : json_decode($file, TRUE);
                $file[] = $fileData;
                file_put_contents("/usr/www/users/alphacz/alpha/phpm/account.cfg", json_encode($file));
    
    
            }
    
        }
    
    
    ?>
    

    所以现在你的数据应该存储在JSON中,如下所示:

    [
        {"key":"data"}
        //...
    ]
    

    然后您可以轻松使用json_decode()将文件解码为数组,遍历数组并检查电子邮件和用户是否已被使用。

    <强>代码

    <?php
    
        $user = "Mike";
        $email = "mike@gmail.com";
    
        $file = file_get_contents("/usr/www/users/alphacz/alpha/phpm/account.cfg");
        $data = json_decode($file, TRUE);
    
        foreach($data as $v){
    
            if($v["user"] == $user && $v["email"] == $email){
                echo "Email and user already used";
            }
    
        }
    
    
    ?>