473,326 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Deleting posts from file, file format bug, help.

118 100+
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
2 1472
pbmods
5,821 Expert 4TB
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
helraizer1
118 100+
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

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

Similar topics

7
by: georgios zakitraxis | last post by:
Hi all, I have a folder with many pictures in it. All have the name like: HHMMDDMMYYYY.jpg H-hour M-minute D-day M-month Y-year
5
by: ShyGuy | last post by:
I have a database that does an auto backup of some data before closing. I also have it set up to delete all backup files older than 5 days. I can't figure out how to check if the file is there...
2
by: SiouxieQ | last post by:
Hi there, I'm using the code below to try to delete a name from a list of names in a file. Unfortunately it doesn't quite do what I want it to. Instead of looking for the name in the...
5
by: Sandeep Singh Sekhon | last post by:
I am Developing a Web Application with ASP.NET 1.1 I have one project Folder which is virtual directory of IIS. In this directory, I have one Folder named Photos in which I used to Store Photos....
2
by: fool | last post by:
Dear group, I am a beginner in php and I was little bit experience in C language. I want to read a file's content and delete the first line of the file and display those lines which has got...
9
by: Hamed | last post by:
Hello I have a DataGrid that a is bound to a DataTable. Some of the rows in the DataTable should not be deleted. How can I prohibit deleting of some identified rows? The problem could be...
8
by: NAdir | last post by:
Hi, thank you for your help. My VB.Net application contains a document that the user can refresh at any time. The refresh works fine and needs to loop through few datatables (hundreds of rows)....
13
by: programming | last post by:
how do i delete from a text file 1 of the following lines: jon|scott adam|smith <--delete paul|clark say i would like to delete the middle line of this txt, in member.txt what php code or...
4
by: sphinney | last post by:
I searched for this topic on TheScripts but didn't see any earlier posts. Here are the basic facts of my problem: * I've deleveloped an Access 2002 database (.mdb) that contains many tables and...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.