By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

Deleting posts from file, file format bug, help.

100+
P: 118
Hi folks,

I have a file for my chatbox called data.line, which the posts are in the layout

Expand|Select|Wrap|Line Numbers
  1.  
  2. CHATBOXTEXT
  3. 7
  4. username=helraizer
  5. 1202416953
  6. ip=86.140.73.183
  7. color=yellow
  8. font=palab
  9. message=bit of a bug, I admit
  10. ###
  11. username=Helraizer
  12. 1202420235
  13. ip=86.140.73.183
  14. color=yellow
  15. font=palab
  16. message=Teeeeeeesting
  17. ###
  18. username=Fjar
  19. 1202420306
  20. ip=81.77.39.76
  21. color=yellow
  22. font=palab
  23. message=I love you, my Sam ^_^
  24. ###
  25. username=helraizer
  26. 1202420637
  27. ip=81.77.39.76
  28. color=aqua
  29. font=palab
  30. message=I love you too, my Fjar!! <3
  31. ###
  32.  
This is fine, when you post a comment it posts your data like this from a form using this code
index.php (only relavent code shown)

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. $text = htmlspecialchars(stripcslashes($_POST['input'] . "\n"));
  4.             $username = htmlspecialchars(stripslashes($_POST['username']));
  5.             $color = $_POST['color'];
  6.             $font = $_POST['font'];
  7.             $ip = $_SERVER['REMOTE_ADDR'] . "\n";
  8.             $ip1 = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
  9.             $_SESSION['username'] = $username;
  10.             $_SESSION['color'] = $color;
  11.             $current_time = mysql_real_escape_string(time());
  12.  
  13.         $data[] = "\n".trim("\nusername=".htmlspecialchars_decode(substr($username, 0, 10)) . "\r");
  14.             $data[] = trim($current_time) . "\r";
  15.             $data[] = "ip=".trim($ip1) . "\r";
  16.             $data[] = "color=".trim($color) . "\r";
  17.             $data[] = "font=".trim($font) . "\r";
  18.             $data[] = "message=".htmlspecialchars_decode(trim(substr($text, 0, 75)) . "\r");
  19.             $data[] = trim("###");
  20.  
  21. //followed later by
  22.  
  23.  
  24. $datal = file_get_contents("data.line");
  25. if (stristr($datal, $_SERVER['REMOTE_ADDR'])) {
  26.  
  27.     echo "<a href='http://helraizer.dnsalias.net/Chat/index.php5?action=delete'><b>Delete your post</b></a>";
  28.  
  29.     $_SESSION['a'] = 1;
  30.  
  31.     if (isset($_SESSION['a']) && $_GET['action']=="delete") {
  32. ?><table align='center'><tr><td><b>Post Deletion</b></td></tr><tr><td>
  33. <?php
  34.         $us_name = $_POST['user'];
  35.         echo "<form align='center' name='post' action='delete.php5' method='post'>";
  36.         echo "<label align='center' for='user'>Please enter the username you used in your post:</label>";
  37.         echo "&nbsp;<input type='text' align='center' id='usera' name='usera' size='10' maxlength='10'>";
  38.         echo "&nbsp;<input type='submit' value='Delete!' name='submita' id='submita'>";
  39.  
  40. ?>
  41.   </form></td></tr></table><br><br><br>
  42.  <?php
  43.    }
  44. }
  45. ?>
Which is fine. It adds the new post after the previous, starting on a new line each time. It must do this or the script won't read be able to read the file at all, or will pull the wrong information out and thus break the formatting.


function.php

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. function ddfm_flock($handle, $param) {
  4.  
  5.     global $enable_file_locking;
  6.  
  7.     if ($enable_file_locking == TRUE) {
  8.         return flock($handle, $param);    
  9.     } else {
  10.         return TRUE;
  11.     }
  12. ?>
  13. }

Then in finally in delete.php


Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. if (isset($_POST['submita'])) {
  4.  
  5.     $entries = file_get_contents("data.line");
  6.     $entries = (array )explode('###', $entries);
  7.  
  8.     foreach ($entries as $entry) {
  9.         $data_t = explode("\r", trim($entry));
  10.  
  11.         if ($data_t[0] == "username=" . $_POST['usera']) {
  12.  
  13.             $data = array();
  14.             foreach ($data_t as $dt) {
  15.                 if (strpos($dt, '=') != false) {
  16.                     $k = substr($dt, 0, strpos($dt, '='));
  17.                     $v = substr($dt, strpos($dt, '=') + 1, strlen($dt) - strpos($dt, '='));
  18.                     $data[$k] = $v;
  19.                 }
  20.             }
  21.             $data['timestamp'] = $data_t[1];
  22.  
  23.  
  24.             if (isset($_POST['usera'])) {
  25.                 if (is_string($_POST['usera'])) {
  26.                     $item = "username=" . $_POST['usera'];
  27.                 }
  28.             }
  29.  
  30.             $the_ip = "";
  31.             $entries = file_get_contents("data.line");
  32.             $entries = (array )explode('###', $entries);
  33.             foreach ($entries as $entry) {
  34.                 $data_t = explode("\r", trim($entry));
  35.                 if (trim($data_t[0]) == $item) {
  36.                     foreach ($data_t as $d) {
  37.                         if (strpos($d, 'username=') === 0) {
  38.                             $the_ip = substr($d, 9, strlen($d) - 1);
  39.                         }
  40.                     }
  41.                 }
  42.             }
  43.  
  44.             $entries = file_get_contents("data.line");
  45.             $entries = (array )explode('###', $entries);
  46.  
  47.             // recreate file
  48.             $handle = fopen("data.line", "w");
  49.  
  50.             if (ddfm_flock($handle, LOCK_EX)) { // do an exclusive lock
  51.  
  52.                 foreach ($entries as $entry) {
  53.  
  54.                     $data_t = explode("\r", trim($entry));
  55.  
  56.                     if (trim($data_t[0]) != "") { // if valid item
  57.  
  58.                         foreach ($data_t as $d) {
  59.                             if (strpos($d, 'username=') === 0) {
  60.                                 $test_ip = substr($d, 9, strlen($d) - 1);
  61.                             }
  62.                         }
  63.  
  64.                         if ($test_ip != $the_ip) { // put back
  65.  
  66.                             foreach ($data_t as $d) {
  67.                                 fwrite($handle, $d . "\r");
  68.                             }
  69.                             fwrite($handle, "###\r");
  70.  
  71.                         } else {
  72.                             // skip items from this IP
  73.                         }
  74.  
  75.                     }
  76.                 }
  77.  
  78.                 ddfm_flock($handle, LOCK_UN); // release the lock
  79. } else {
  80.         }
  81.     }
  82. }
  83. ?>
which adds a blank line to the chatbox and thus the chatbox returns the wrong values and format.. How would I stop it from adding this extra line? If I change fwrite($handle, "###\r") to fwrite($handle, "###") it

makes data.line format as:
Expand|Select|Wrap|Line Numbers
  1.  
  2. CHATBOXTEXT
  3. 7
  4. username=helraizer
  5. 1202416953
  6. ip=86.140.73.183
  7. color=yellow
  8. font=palab
  9. message=bit of a bug, I admit
  10. ###username=Fjar
  11. 1202420306
  12. ip=81.77.39.76
  13. color=yellow
  14. font=palab
  15. message=I love you, my Sam ^_^
  16. ###username=helraizer 
  17. 1202420637
  18. ip=81.77.39.76
  19. color=aqua
  20. font=palab
  21. message=I love you too, my Fjar!! <3
  22. ###
  23.  
The username of the next post starts on the same lines as the ### and the same problem occurs

Instead of reading

[helraizer]bit of a bug, I admit (should be yellow)
[Fjar] I love you my Sam!! ^_^ (should be yellow)
[helraizer]I love you too, my Fjar!! <3 (should be blue)

it reads

[palab]palab
[202416953][202420306]
helraizer

^all of them white (the default if $color is not specified or different to those it is supposed to be).

How would I get it to, when I delete a post, print the comment (all information) starting on the line after the previous ###?

Hope that makes sense. Just ask if you need more information.

Thanks,
Sam

The normal look:


The broken look:

Feb 8 '08 #1
Share this Question
Share on Google+
2 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Sam.

Can you store these values in a database? SQLite can take care of everything for you, and you don't have to run a separate db server (http://php.net/sqlite).
Jun 27 '08 #2

100+
P: 118
Heya, Sam.

Can you store these values in a database? SQLite can take care of everything for you, and you don't have to run a separate db server (http://php.net/sqlite).
Heya pbmods,

Yeah, I moved onto a database a while back because of this deletion and editing problem. It made the functionality of the code much easier to work with.

It was just that I made the first incarnation of the script was for flat file, which worked perfectly until I needed to edit or delete. Hense the move to a db.

Thanks for the response.

Sam
Jun 28 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.