• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Who can change the php codes 4 me?(saving data in txt file)

I m doing a notesboard, i use flash to loadvariables the below php file to save data to the text files.

My php file is like this:

<?
// store the variables passed by POST
$fp = fopen("notes1.txt", "w");
if (!$fp) die ("cannot open the file");
fputs($fp, "&a1=".$a1, 4096);
fputs($fp, "&d1=".$d1, 4096);
fclose($fp);
?>

This code is not wrote by me. I m an outsider of PHP so I wish someone can help me to modify the above
codes to what i want.

Using the above php codes, I meet great inconvenience when I want to update only a single variable
in the text.

for example, like the above code stated, I have two vars "a1" and "d1",and both have some values.

Now the main problem is, even if i only want to update the value of "a1", i can't load an url like this:
"MYPATH\notes1.php?time=xxx&a1=yyyyyyyy"
(bcoz if i load variables like the above url, the value of d1 will automatically be empty)
so I have to loadvariables like this
"MYPATH\notes1.php?time=xxx&a1=yyyyyyyy&d1=zzzzzzz&"

So, the above php code will cause me into great trouble when I got about 50 variables in a single text
files and when, even if I just want to update a single variable but I m forced to gather all 50 variables'
data and loadvariables like this :
"MYPATH\notes1.php?time=xxx&a1=yyyyyyyy&d1=zzzzzzz&a2=......&a50=...&d50=..."
the result will be error because the total length of this url is too long for a browser.

In short, I hope somebody can change the above code for me so that, if i want to change the value of
"a1" only, then I can loadvariable like this :
"MYPATH\notes1.php?time=xxx&a1=yyyyyyyy&"
but the php code will NOT replace "d1" value by an empty value!!!!!

Hope that u can understand wat i mean!
0
MrTerrence
Asked:
MrTerrence
  • 12
  • 10
  • 7
1 Solution
 
vmvCommented:
Something like this:

$vars=array("a1","d1"); //add var names here
list($data)=file("notes1.txt");
parse_str($data,$data);
foreach($vars as $var)if($$var)$data[$var]='$var='.urlencode($$var);
$fp=@fopen("notes1.txt", "w");
$fp or die("cannot open the file");
fputs($fp,"&".join('&',$data));
fclose($fp);

Not tested though...
0
 
dkjariwalaCommented:
Fine,
its simple !!

<?
// store the variables passed by POST
$fp = fopen("notes1.txt", "w");
if (!$fp) die ("cannot open the file");

// by which method you would be submitting data to PHP ??
// get or post ?
$a1 = trim($HTTP_POST_VARS['a1']); // Use $HTTP_GET_VARS if you are caling script using query string.
//trim removes the unnecessary whitespace.
// so above statment would get the value of variable a1
if(! empty($a1)) //and if it is not EMPTY,
     fputs($fp, "&a1=".$a1, 4096); //would write in file !! :)

$d1 = trim($HTTP_POST_VARS['d1']); // Use $HTTP_GET_VARS if you are caling script using query string.
if(! empty($d1))
     fputs($fp, "&d1=".$d1, 4096);

// you can use same logic further.
fclose($fp);
?>

JD


0
 
vmvCommented:
Uhm... The following line:

foreach($vars as $var)if($$var)$data[$var]='$var='.urlencode($$var);

should look like this:

foreach($vars as $var)if(isset($$var))$data[$var]='$var='.urlencode($$var);

to allow saving empty vars.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
vmvCommented:
dkjariwala, your method will destroy all vars which should not be updated...
0
 
MrTerrenceAuthor Commented:
ok, its seems like vmv using array is simplier bcoz i got a1 d1 .. to a50 d50.

so i will test the following php codes:

$vars=array("a1","d1"); //add var names here
list($data)=file("notes1.txt");
parse_str($data,$data);
foreach($vars as $var)if(isset($$var))$data[$var]='$var='.urlencode($$var);
$fp=@fopen("notes1.txt", "w");
$fp or die("cannot open the file");
fputs($fp,"&".join('&',$data));
fclose($fp);

hope it works.
0
 
dkjariwalaCommented:
vmv,

Your code is correct and compact for sure.

But I don't think the code I gave would destroy ANYTHING. It checks for the value if it is present, then writes in file otherwise not.

Thats it !!

JD
0
 
vmvCommented:
dkjariwala:
That's it - your snippet does not write the value not passed to the script. The problem is that file is cleaned on opening and then you write values passed to the script... But if you want to UPDATE data you should read the file first and then replace passed values with the new data.
0
 
dkjariwalaCommented:
Oh,
Okie.
Thats true.
JD
0
 
MrTerrenceAuthor Commented:
Hi vmv,

I m not sure whay u r discussing about with JD. But I have tested your codes and i dunno y i doesnt works. see upload samples here:

http://s3s12001.host.sk/php/phptest3.swf

it doesnt save the new data, dunno why...

???

Regards,
Terrence.
0
 
dkjariwalaCommented:
It seems like that your server is not *parsing* PHP files, its just printing it out.

Configure PHP first.

JD
0
 
vmvCommented:
Did you enclose the code in '<?' and '?>' ?
0
 
MrTerrenceAuthor Commented:
oh i didnt!!!!! no wonder why the codes fail. i will try again this code, i need some times, because the server host.sk i m using has down i think. vmv or JD, r u using any free web space that support php now?
0
 
MrTerrenceAuthor Commented:
unfortunately, it still doesnt works, but i think nearly success. I use below codes, any mistakes? :

<?
$vars=array("a1","d1","a2","d2"); //add var names here
list($data)=file("newnotes1.txt");
parse_str($data,$data);
foreach($vars as $var)if(isset($$var))$data[$var]='$var='.urlencode($$var);
$fp=@fopen("newnotes1.txt", "w");
$fp or die("cannot open the file");
fputs($fp,"&".join('&',$data));
fclose($fp);
?>

Thanks.
0
 
MrTerrenceAuthor Commented:
the new faulty testpage can be viewed here:
http://s3s12001.host.sk/phptest5.swf
0
 
vmvCommented:
Yes, there is a mistake.

foreach($vars as $var)if(isset($$var))$data[$var]='$var='.urlencode($$var);

should be

foreach($vars as $var)if(isset($$var))$data[$var]="$var=".urlencode($$var);

Should work now...
0
 
MrTerrenceAuthor Commented:
after changing the codes, when u click the "save d2 only", u will find that the text file now only got the newest value of d2 but the values of a1,d1 and a2 had lost.

sample still here:
http://s3s12001.host.sk/phptest5.swf


Thanks.
0
 
MrTerrenceAuthor Commented:
after changing the codes, when u click the "save d2 only", u will find that the text file now only got the newest value of d2 but the values of a1,d1 and a2 had lost.

sample still here:
http://s3s12001.host.sk/phptest5.swf


Thanks.
0
 
dkjariwalaCommented:
Fine,
It took bit time, but check this code. Truly flexible and it works.

<?php

$your_vars  = array('a1','d1','a2','d2','a3','d3');
$lines = file('notes.txt');
$line = $lines[0];
print "Line BEFORE $line <br><hr>";
foreach($your_vars as $var)
{
     $reg_condition .= "&$var=(.*)";
}
#print $reg_condition;
$i = 1 ;
if(eregi($reg_condition,$line,$regs))
{
     foreach($your_vars as $var)
     {
          $$var = $regs[$i];
          #print "Regs i is " . $regs[$i];
          $i++;
     }
}
if($i != 0)
{
     foreach($your_vars as $var)
     {
          if(isset($HTTP_GET_VARS[$var]))
          {
               $$var = $HTTP_GET_VARS[$var];
          }
     }
     $fp = fopen('notes.txt','w');
     $string = '';
     foreach($your_vars as $var)
     {
          $value = $$var;
          $string .= "&" . $var . "="  . $value;
          #print "<br>String is $string<br>";
     }
     print "Line AFTER $string";
     fwrite($fp,$string);
     fclose($fp);
}
?>


Just put this script. and say if its name is check.php call it like

http://yourserver.com/check.php (wihtout arguments)

line in file wont be changed.

call it like
http://yourserver.com/check.php?a2=2000 

and see only a2 would be changed.

kewl, isnt it ?
JD
0
 
vmvCommented:
Uhh... Sorry, one more bug. Works now:

<?
$vars=array("a1","d1","a2","d2"); //add var names here
list($data)=file("newnotes1.txt");
parse_str($data,$data);
foreach($vars as $var)$data[$var]=isset($$var)?"$var=".urlencode($$var):"$var=".urlencode($data[$var]);
$fp=@fopen("newnotes1.txt", "w");
$fp or die("cannot open the file");
fputs($fp,"&".join('&',$data));
fclose($fp);
?>
0
 
vmvCommented:
JD, don't forget to urlencode data...
0
 
dkjariwalaCommented:
Why urlencode ??

As far as i understand that is how variable reading works in FLASH, and it has nothing to do with creating URL.

Right MrTerrence ??

JD
0
 
vmvCommented:
But what if your data contains the '&' character?
0
 
dkjariwalaCommented:
I think '&' can not be part of data.  If it is then you can just use urlencode. You are correct there.

Otherwise code works, right vmv ??
JD
0
 
vmvCommented:
Probably yes - didn't check your code. But I think it is better to use parse_str rather than eregi since it is there especially for such data format.
0
 
MrTerrenceAuthor Commented:
Congratulation to... both of u two! :)
I have tried two pieces of codes, both of them works for me!

And thanks for remind me that the data should not have the symbol "&". ( I tried both codes, both codes go wrong when data got the symbol "&").

Sorry, JD, I will accept vmv's comment as answer bcoz his codes are more compact, as u have said b4. Sorry.
0
 
MrTerrenceAuthor Commented:
hi vmv r u still there? do u mind helping once more although i gave u my points already?

I found that I need not the data to be "urlencode" bcoz it will make my chinese data into smbols, so, how to remove the "urlencode"??

is it like this?:
<?
$vars=array("a1","d1","a2","d2"); //add var names here
list($data)=file("newnotes1.txt");
parse_str($data,$data);
foreach($vars as $var)$data[$var]=isset($$var)?"$var="$$var:"$var="$data[$var];
$fp=@fopen("newnotes1.txt", "w");
$fp or die("cannot open the file");
fputs($fp,"&".join('&',$data));
fclose($fp);
?>

I feel sth wrong... ( I removed the ".urlencode()" )
can you help me to correct the above code?
thanks for helping me once more!
0
 
MrTerrenceAuthor Commented:
hi vmv r u still there? do u mind helping once more although i gave u my points already?

I found that I need not the data to be "urlencode" bcoz it will make my chinese data into smbols, so, how to remove the "urlencode"??

is it like this?:
<?
$vars=array("a1","d1","a2","d2"); //add var names here
list($data)=file("newnotes1.txt");
parse_str($data,$data);
foreach($vars as $var)$data[$var]=isset($$var)?"$var="$$var:"$var="$data[$var];
$fp=@fopen("newnotes1.txt", "w");
$fp or die("cannot open the file");
fputs($fp,"&".join('&',$data));
fclose($fp);
?>

I feel sth wrong... ( I removed the ".urlencode()" )
can you help me to correct the above code?
thanks for helping me once more!
0
 
MrTerrenceAuthor Commented:
Dear vmv,
I gave u my points too early, I havent success yet! I promise I will give u another 100pts if u can continue to help me to solve my problem here.

Besides the urlencode problem I also discovered that both codes of u 2 experts will give me a same err msg of fatal error :

"Fatal error A Maximum execution time of 30 seconds exceeded in host-users notes.php on line"

Below is my codes after adding many variables:

<?
$vars=array("a1","d1","a2","d2","a3","d3","a4","d4","a5","d5","a6","d6","a7","d7","a8","d8","a9","d9","a10","d10","a11","d11","a12","d12","a13","d13","a14","d14","a15","d15","a16","d16","a17","d17","a18","d18","a19","d19","a20","d20","a21","d21","a22","d22","a23","d23","a24","d24","a25","d25","a26","d26","a27","d27","a28","d28","a29","d29","a30","d30","a31","d31","a32","d32","a33","d33","a34","d34","a35","d35","a36","d36","a37","d37","a38","d38","a39","d39","a40","d40","a41","d41","a42","d42","a43","d43","a44","d44","a45","d45","a46","d46","a47","d47","a48","d48","a49","d49","a50","d50","a51","d51","a52","d52","a53","d53","a54","d54","a55","d55","a56","d56","a57","d57","a58","d58","a59","d59","a60","d60","a61","d61","a62","d62","a63","d63","a64","d64","a65","d65","a66","d66","a67","d67","a68","d68","a69","d69","a70","d70","a71","d71","a72","d72","a73","d73","a74","d74","a75","d75","a76","d76","a77","d77","a78","d78","a79","d79","a80","d80","a81","d81","a82","d82","a83","d83","a84","d84","a85","d85","a86","d86","a87","d87","a88","d88","a89","d89","a90","d90","a91","d91","a92","d92","a93","d93","a94","d94","a95","d95","a96","d96","a97","d97","a98","d98","a99","d99","a100","d100","totalmsg"); //add var names here
list($data)=file("notes.txt");
parse_str($data,$data);
foreach($vars as $var)$data[$var]=isset($$var)?"$var=".urlencode($$var):"$var=".urlencode($data[$var]);
$fp=@fopen("notes.txt", "w");
$fp or die("cannot open the file");
fputs($fp,"&".join('&',$data));
fclose($fp);
?>


Does the error msg means that I put too many variables in one txt?
0
 
MrTerrenceAuthor Commented:

NOW NO MORE PROBLEM, THIS THREAD CLOSED.
(I have discovered the method, if there is further question, I will ask a new question.)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 12
  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now