Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4567700
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:58:08+00:00 2026-05-21T18:58:08+00:00

I encountered some problems, I want this script to: Open test.txt file. Check if

  • 0

I encountered some problems, I want this script to:

  1. Open test.txt file.
  2. Check if user have added any text to the txt file.
  3. If user have added any text, delete the existing line and replace it with the new. From $_POST.
  4. If user have not, add $_POST in test.txt

Problem:

When I spam the submit button, the .txt will mess up. Anyone know how to make checks, so it does not mess up?
Please don’t suggest MYSQL, I need these in .txt file.

Thanks.

function cutline($filename,$line_no=-1) { 

    $strip_return=FALSE; 

    $data=file($filename); 
    $pipe=fopen($filename,'w'); 
    $size=count($data); 

    if($line_no==-1) $skip=$size-1; 
        else $skip=$line_no-1; 

    for($line=0;$line<$size;$line++) 
        if($line!=$skip) 
            fputs($pipe,$data[$line]); 
        else 
            $strip_return=TRUE; 

    return $strip_return; 
} 


if ($userid = 1) {
    if(!isset($_POST['submit'])){
?>
        <center><form action="" method="POST">
            <b>HWID</b>
            <input type="text" name="HWID" />
            <input type="submit" value="Add HWID" name="submit">
            </form>
        </center>
<?php

    }else{

        $userid= 1;
        $userid = "user=" . $userid;

        $file = "test.txt";
        $lines = file($file);
        $count = 1;

        foreach ($lines as $e) {
            if(strpos($e, $userid) !== FALSE){
                cutline($file,$count);

                ++$count;


            }

        }
                $fh = fopen($file, 'a') or die("can't open file");
                $stringData = $userid . $_POST['HWID'] . "\n";
                fwrite($fh, $stringData);
        }
    }
}else{ 
    echo "You're not logged in";
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T18:58:08+00:00Added an answer on May 21, 2026 at 6:58 pm

    I am not 100% sure how the text file is messing up, but I guess locking won’t help here as locks are released when the script finished (or is reloaded).

    It looks like you just “kill” your cutline while in progress and the remaining lines will not be written. One way to fix this could be to save the new content of the file in a temporary variable and call fwrite only once. (I am not 100% sure if this will work)

    Another possibility is to write the results of cutline into a temporary file and replace the old file with the new one, when the cutline method is done. This can happen inside the method.

    In either ways the existing file will not be touched if the script gets killed in an unsafe state. But you can still loose the new input from the user when he manages to reload the page right after the function call of cutline and before you add the new input in this line

    fwrite($fh, $stringData);
    

    I think this is really hard to force as this operation is quite fast.

    EDIT:

    Don’t forget to test the script using multiple users at the same time, if this is a valid use case. If two or more guys are editing the same file at the same time it will mess up as well. So you might end up with some locking but that will not solve the problem described here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.