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

  • SEARCH
  • Home
  • 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 6365787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:20:18+00:00 2026-05-25T00:20:18+00:00

What I’m wanting to achieve is basically setting variables from data entered through my

  • 0

What I’m wanting to achieve is basically setting variables from data entered through my form (these variables are then used throughout my website). I’m not to concerned with security as I’m the only one who would have access to the form.

So at the moment I am using fwrite to save the data in separate files and then using file_get_contents for each variable. The data from the form is small, one or two words for each field.

I’m unable to use a database so below is an example of what I have working at the moment, can it be improved or is there any other ways of achieving this?

<?php

if(isset($_REQUEST['sub']))
{
$myFile = "first.php";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_REQUEST['first'];
$string = preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]/", "", $stringData);
fwrite($fh, $string);
fclose($fh);

$myFile_second = "second.php";
$fh2 = fopen($myFile_second, 'w') or die("can't open file2");
$stringData2 = $_REQUEST['second'];
$string2 = preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]/", "", $stringData2);
fwrite($fh2, $string2);
fclose($fh2);

$myFile_third = "third.php";
$fh3 = fopen($myFile_third, 'w') or die("can't open file3");
$stringData3 = $_REQUEST['third'];
$string3 = preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]/", "", $stringData3);
fwrite($fh3, $string3);
fclose($fh3);
}

$first = file_get_contents ("first.php");
$second = file_get_contents ("second.php");
$third = file_get_contents ("third.php");

?>

<form method="post" name="installer">

<div id="field">
<label>First</label>
<input type="text" name="first" value="<?php echo $first; ?>" />
</div>

<div id="field">
<label>Second</label>
<input type="text" name="second" value="<?php echo $second; ?>" />
</div>

<div id="field">
<label>Third</label>
<input type="text" name="third" value="<?php echo $third; ?>" />
</div>

<div id="submit">
<input type="submit" value="Save" name="sub" />
</div>

</form>

Any suggestions would be much appreciated thanks 🙂

Update

Well thank you to everyone for your help and suggestions. I’ve now implemented something similar to below and I must say it works wonderfully!

<?
if (isset($_REQUEST['sub'])) {

    $files_data = array(
        'first' => &$_REQUEST['first'],
        'second' => &$_REQUEST['second'],
        'third' => &$_REQUEST['third']
    );

    $files_data = preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]+/", "", $files_data);

    file_put_contents('data.txt', serialize($files_data)) !== FALSE or die("Can't write to file!" . PHP_EOL);
    } 

$files_data = unserialize(file_get_contents('data.txt'));

$first = $files_data[first];
$second = $files_data[second];
$third = $files_data[third];

?>

<form method="post" name="installer">
<div id="field">
<label>First</label>
<input type="text" name="first" value="<?php echo $first; ?>" />
</div>
<div id="field">
<label>Second</label>
<input type="text" name="second" value="<?php echo $second; ?>" />
</div>
<div id="field">
<label>Third</label>
<input type="text" name="third" value="<?php echo $third; ?>" />
</div>
<div id="submit">
<input type="submit" value="Save" name="sub" />
</div>
</form>
  • 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-25T00:20:19+00:00Added an answer on May 25, 2026 at 12:20 am

    I guess you are doing this for persistence (i.e. the web server reboots and you want your data to be there once it’s back up)? So if you are stuck with writing, reading and parsing a multitude of files it’s hard to shave massive amounts of time from your execution.

    You could look at fscan and see if that will make the reading a bit faster for you.

    Other than that I recommend that you cache the data once you have parsed the files so you don’t have to do it over and over and only rescan once the files change. You could keep track of this by a checksum or looking at the files modified time stamp.

    One way to “simplify” it would be to save your posted data in an array and serializing that and saving it to a file. Then you could read the entire file in one go and unserialize it and you would have your array with the data back.

    A quick-and-dirty way to save a array to a file would be something like

    file_put_contents('mydatafile', serialize($myArray)));
    

    and to get the array back

    $myArray = unserialize(file_get_contents('mydatafile'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I

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.