Solved

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

Posted on 2002-05-27
29
215 Views
Last Modified: 2010-05-19
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
Comment
Question by:MrTerrence
  • 12
  • 10
  • 7
29 Comments
 
LVL 2

Expert Comment

by:vmv
Comment Utility
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
 
LVL 5

Expert Comment

by:dkjariwala
Comment Utility
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
 
LVL 2

Expert Comment

by:vmv
Comment Utility
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
 
LVL 2

Expert Comment

by:vmv
Comment Utility
dkjariwala, your method will destroy all vars which should not be updated...
0
 

Author Comment

by:MrTerrence
Comment Utility
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
 
LVL 5

Expert Comment

by:dkjariwala
Comment Utility
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
 
LVL 2

Expert Comment

by:vmv
Comment Utility
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
 
LVL 5

Expert Comment

by:dkjariwala
Comment Utility
Oh,
Okie.
Thats true.
JD
0
 

Author Comment

by:MrTerrence
Comment Utility
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
 
LVL 5

Expert Comment

by:dkjariwala
Comment Utility
It seems like that your server is not *parsing* PHP files, its just printing it out.

Configure PHP first.

JD
0
 
LVL 2

Expert Comment

by:vmv
Comment Utility
Did you enclose the code in '<?' and '?>' ?
0
 

Author Comment

by:MrTerrence
Comment Utility
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
 

Author Comment

by:MrTerrence
Comment Utility
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
 

Author Comment

by:MrTerrence
Comment Utility
the new faulty testpage can be viewed here:
http://s3s12001.host.sk/phptest5.swf
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Expert Comment

by:vmv
Comment Utility
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
 

Author Comment

by:MrTerrence
Comment Utility
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
 

Author Comment

by:MrTerrence
Comment Utility
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
 
LVL 5

Expert Comment

by:dkjariwala
Comment Utility
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
 
LVL 2

Accepted Solution

by:
vmv earned 200 total points
Comment Utility
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
 
LVL 2

Expert Comment

by:vmv
Comment Utility
JD, don't forget to urlencode data...
0
 
LVL 5

Expert Comment

by:dkjariwala
Comment Utility
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
 
LVL 2

Expert Comment

by:vmv
Comment Utility
But what if your data contains the '&' character?
0
 
LVL 5

Expert Comment

by:dkjariwala
Comment Utility
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
 
LVL 2

Expert Comment

by:vmv
Comment Utility
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
 

Author Comment

by:MrTerrence
Comment Utility
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
 

Author Comment

by:MrTerrence
Comment Utility
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
 

Author Comment

by:MrTerrence
Comment Utility
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
 

Author Comment

by:MrTerrence
Comment Utility
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
 

Author Comment

by:MrTerrence
Comment Utility

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now