rewriter Posted March 7, 2008 Share Posted March 7, 2008 Hi all, i have a .txt file as this: [paragraph1] data1 data2 data_three... [paragraph2] data15 data other data three... [paragraph_other] data1 some data data_four... From a page web, if a check the checkbox1 I want to write some data in append of paragraph1; if I click the checkbox2, I want append others values in paragraph2 and so on... How can I do? con you help me with some code? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/ Share on other sites More sharing options...
puritania Posted March 7, 2008 Share Posted March 7, 2008 Check out file_put_contents and its flag FILE_APPEND in the manual. Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-486071 Share on other sites More sharing options...
rewriter Posted March 7, 2008 Author Share Posted March 7, 2008 but the file_put_contents function append new text at the end of file or where I want? Can you post a example? Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-486124 Share on other sites More sharing options...
puritania Posted March 7, 2008 Share Posted March 7, 2008 no it appends it at the ed of the file. if you don't want that, you have to read the file first with file_get_contents, add the new content at the position you want and overwrite the file. Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-486128 Share on other sites More sharing options...
rewriter Posted March 7, 2008 Author Share Posted March 7, 2008 add the new content at the position you want how can i do this? now, i using this code: $op = file_get_contents($filename); $op contains all the content txt file: i must to append some data only in [paragraph1] or [paragraph2].... Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-486399 Share on other sites More sharing options...
wildteen88 Posted March 7, 2008 Share Posted March 7, 2008 Try: <?php function get_paragraphs() { $lines = file('data.txt'); $cur_pgraph = null; $paragraph = array(); foreach($lines as $line) { #$line = trim($line); if(eregi("^\[([a-z0-9\-_ ]+)\]", $line)) { $cur_pgraph = trim(str_replace(array('[', ']'), '', $line)); $paragraph[$cur_pgraph] = null; } else { $paragraph[$cur_pgraph] .= $line; } } return $paragraph; } if(isset($_POST['submit'])) { $output = null; foreach($_POST['paragraph'] as $data) { $title = $data['title']; $content = $data['content']; $output .= "[$title]\n$content\n"; } file_put_contents('data.txt', $output); header('Location: ' . $_SERVER['PHP_SELF']); } else { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table border="0" cellpadding="5" cellspacing="1"> <tr> <th colspan="2"><h1>Paragraph Editor</h1></th> </tr> <tr> <th width="150">Title</th> <th>Content</th> </tr> <?php $i = 0; foreach(get_paragraphs() as $key => $data) { echo " <tr valign=\"top\">\n <td><input type=\"text\" name=\"paragraph[$i][title]\" value=\"$key\"></td>\n ". "<td><textarea name=\"paragraph[$i][content]\" cols=\"60\" rows=\"6\">$data</textarea></td>\n </tr>\n"; $i++; } ?> <tr> <td colspan="2"><input type="submit" name="submit" value="Apply Changes" /></td> </tr> </table> </form> <?php } ?> change data.txt to the name of the file that contains your paragaphs Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-486478 Share on other sites More sharing options...
rewriter Posted March 8, 2008 Author Share Posted March 8, 2008 thanks: it works fine! Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-487140 Share on other sites More sharing options...
puritania Posted March 25, 2008 Share Posted March 25, 2008 Try: <?php ... header('Location: ' . $_SERVER['PHP_SELF']); ... ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> ... Why do you make a header Location? Very ugly! And the form action ... uuuhh, what about XSS? Uglier than ugly... Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-500468 Share on other sites More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 I'm honestly not sure how someone would perform an XSS attack when the variables used are coming directly from the PHP engine. Please expand... If I'm wrong on this one I'd like to know the possible vulnerabilities in using PHP_SELF Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-500485 Share on other sites More sharing options...
wildteen88 Posted March 25, 2008 Share Posted March 25, 2008 I had used to header to clear the $_POST and for the changes to take affect, otherwise the browser will display the old values. I could of told the browser to not cache the page but it was only an example Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-500487 Share on other sites More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 I had used to header to clear the $_POST and for the changes to take affect, otherwise the browser will display the old values. I could of told the browser to not cache the page but it was only an example I really don't think anything was wrong with your code. Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-500501 Share on other sites More sharing options...
puritania Posted March 25, 2008 Share Posted March 25, 2008 i don't know if you really know what xss (cross site scripting) is, but you should make everything to avoid it. You should !!!really!!! always use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF'] or write it as a plain text when using forms. I had used to header to clear the $_POST and for the changes to take affect, otherwise the browser will display the old values. I could of told the browser to not cache the pagePlease... if you really need or want to clear the $_POST Variable you can use $_POST = array(); and that's it. Regarding the header-stuff: Why don't you do it like this: <?php ... if(!empty($_POST['paragraph'])) { $output = null; foreach($_POST['paragraph'] as $data) { $title = $data['title']; $content = $data['content']; $output .= "[$title]\n$content\n"; } file_put_contents('data.txt', $output); } ?> <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post"> <table border="0" cellpadding="5" cellspacing="1"> <tr> <th colspan="2"><h1>Paragraph Editor</h1></th> </tr> <tr> <th width="150">Title</th> <th>Content</th> </tr> <?php $i = 0; foreach(get_paragraphs() as $key => $data) { echo " <tr valign=\"top\">\n <td><input type=\"text\" name=\"paragraph[$i][title]\" value=\"$key\"></td>\n ". "<td><textarea name=\"paragraph[$i][content]\" cols=\"60\" rows=\"6\">$data</textarea></td>\n </tr>\n"; $i++; } ?> <tr> <td colspan="2"><input type="submit" name="submit" value="Apply Changes" /></td> </tr> </table> </form> As you can see, the header location is totally useless. Note: a header location to the same domain is useless in 99.9%. 2 other things: Don't check the submit button, check the data. Some browsers don't submit the button's value. And to save the data in text file with your scheme [] isn't really elegant. you should use xml or a database or if you want to keep it simple a serialized array. by the way, i see that you're using the eregi functions, these are old... you should use preg_match and co. Sorry about the Objections, but i had to say it (although if you are a Genius Super Moderator ) Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-500564 Share on other sites More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 You still didn't explain why. This site sums it up nicely http://www.volectricity.com/phpmysql/articles/how_php_self_opens_you_up_to_xss Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-500670 Share on other sites More sharing options...
wildteen88 Posted March 25, 2008 Share Posted March 25, 2008 i don't know if you really know what xss (cross site scripting) is, but you should make everything to avoid it. You should !!!really!!! always use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF'] or write it as a plain text when using forms. I had used to header to clear the $_POST and for the changes to take affect, otherwise the browser will display the old values. I could of told the browser to not cache the pagePlease... if you really need or want to clear the $_POST Variable you can use $_POST = array(); and that's it. Regarding the header-stuff: Why don't you do it like this: <?php ... if(!empty($_POST['paragraph'])) { $output = null; foreach($_POST['paragraph'] as $data) { $title = $data['title']; $content = $data['content']; $output .= "[$title]\n$content\n"; } file_put_contents('data.txt', $output); } ?> <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post"> <table border="0" cellpadding="5" cellspacing="1"> <tr> <th colspan="2"><h1>Paragraph Editor</h1></th> </tr> <tr> <th width="150">Title</th> <th>Content</th> </tr> <?php $i = 0; foreach(get_paragraphs() as $key => $data) { echo " <tr valign=\"top\">\n <td><input type=\"text\" name=\"paragraph[$i][title]\" value=\"$key\"></td>\n ". "<td><textarea name=\"paragraph[$i][content]\" cols=\"60\" rows=\"6\">$data</textarea></td>\n </tr>\n"; $i++; } ?> <tr> <td colspan="2"><input type="submit" name="submit" value="Apply Changes" /></td> </tr> </table> </form> As you can see, the header location is totally useless. Note: a header location to the same domain is useless in 99.9%. 2 other things: Don't check the submit button, check the data. Some browsers don't submit the button's value. And to save the data in text file with your scheme [] isn't really elegant. you should use xml or a database or if you want to keep it simple a serialized array. by the way, i see that you're using the eregi functions, these are old... you should use preg_match and co. Sorry about the Objections, but i had to say it (although if you are a Genius Super Moderator ) I'm understanding what you're saying but what I posted was to the OP needs. As for using xml or a database for storing the paragraphs, the OP already had the paragraphs in a text file. I wrote the code around what they already had. As with the use of header I only added it in for an easy fix, because the browser was caching the page and not showing the new changes made when the form was submited. I know its not the best however it will do. I do understand was xss is yes. I don't know what you're trying to prove. Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-500694 Share on other sites More sharing options...
puritania Posted March 26, 2008 Share Posted March 26, 2008 I do understand was xss is yes. I don't know what you're trying to prove.I just said that you should never use a plain <?php echo $_SERVER['PHP_SELF']; ?> in a form action. That's all. Quote Link to comment https://forums.phpfreaks.com/topic/94880-write-some-values-in-a-txt-file/#findComment-501320 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.