【问题标题】:how to get value from text file in php如何从php中的文本文件中获取值
【发布时间】:2023-04-01 17:09:01
【问题描述】:

我有一个脚本,可以比较页面 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)

这是我在 (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);

请帮帮我

谢谢

【问题讨论】:

  • 您在文件中如何以及在何处存储数据?由于格式不是最好的。
  • 感谢回复,例如(/usr/www/users/alphacz/alpha/phpm/)
  • 我的意思是使用哪个代码?你可以让它与一个正则表达式一起工作,但是当你首先以良好的格式写入文件时,你可以更容易地做到这一点。
  • 这是来自其他程序的输出
  • 好的。如果您可以更改它,那么我肯定会从以 JSON 格式保存数据开始。 .所以在这里你只是想搜索输入中的用户是否已经在你的文件中?

标签: php arrays foreach comparison


【解决方案1】:

您可以在大海捞针,这是您的帐户文件。 用户名模式是“F: {user} pass {...” 电子邮件模式是 "; email={email}; Country=" 注意:你还必须认为这个例子检查关键字不区分大小写,所以 搜索和插入时,应将关键字转换为小写

$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}'));
}

【讨论】:

  • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,这会降低代码和解释的可读性!
  • 谢谢@mustafayelmer,您的脚本非常好,很有帮助,这是我想要的,但我需要它来搜索位于同一行的用户和电子邮件。如果脚本在第一行找到用户并在第二行找到电子邮件,则会出现您的脚本(使用用户和电子邮件)。请帮帮我
  • 有人帮帮我吗?
【解决方案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() 来检查您的输入是否已设置且不为空。

代码

<?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";
        }
        
    }


?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-14
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多