Jump to content

Recommended Posts

Hi folks,

 

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

CHATBOXTEXT
7
username=helraizer
1202416953
ip=86.140.73.183
color=yellow
font=palab
message=bit of a bug, I admit
###
username=Helraizer
1202420235
ip=86.140.73.183
color=yellow
font=palab
message=Teeeeeeesting
###
username=Fjar
1202420306
ip=81.77.39.76
color=yellow
font=palab
message=I love you, my Sam 
###
username=helraizer
1202420637
ip=81.77.39.76
color=aqua
font=palab
message=I love you too, my Fjar!! <3
###

 

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)

<?php
$text = htmlspecialchars(stripcslashes($_POST['input'] . "\n"));
            $username = htmlspecialchars(stripslashes($_POST['username']));
            $color = $_POST['color'];
            $font = $_POST['font'];
            $ip = $_SERVER['REMOTE_ADDR'] . "\n";
            $ip1 = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
            $_SESSION['username'] = $username;
            $_SESSION['color'] = $color;
            $current_time = mysql_real_escape_string(time());

    $data[] = "\n".trim("\nusername=".htmlspecialchars_decode(substr($username, 0, 10)) . "\r");
            $data[] = trim($current_time) . "\r";
            $data[] = "ip=".trim($ip1) . "\r";
            $data[] = "color=".trim($color) . "\r";
            $data[] = "font=".trim($font) . "\r";
            $data[] = "message=".htmlspecialchars_decode(trim(substr($text, 0, 75)) . "\r");
            $data[] = trim("###");

//followed by later


$datal = file_get_contents("data.line");
if (stristr($datal, $_SERVER['REMOTE_ADDR'])) {

    echo "<a href='http://helraizer.dnsalias.net/Chat/index.php5?action=delete'><b>Delete your post</b></a>";

    $_SESSION['a'] = 1;

    if (isset($_SESSION['a']) && $_GET['action']=="delete") {
?><table align='center'><tr><td><b>Post Deletion</b></td></tr><tr><td>
<?php
        $us_name = $_POST['user'];
        echo "<form align='center' name='post' action='delete.php5' method='post'>";
        echo "<label align='center' for='user'>Please enter the username you used in your post:</label>";
        echo " <input type='text' align='center' id='usera' name='usera' size='10' maxlength='10'>";
        echo " <input type='submit' value='Delete!' name='submita' id='submita'>";

?>
  </form></td></tr></table><br><br><br>
<?php
   }
}
?>

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

<?php
function ddfm_flock($handle, $param) {

global $enable_file_locking;

if ($enable_file_locking == TRUE) {
	return flock($handle, $param);	
} else {
	return TRUE;
}
?>
}

 

Then in finally in delete.php

 

<?php
if (isset($_POST['submita'])) {

    $entries = file_get_contents("data.line");
    $entries = (array )explode('###', $entries);

    foreach ($entries as $entry) {
        $data_t = explode("\r", trim($entry));

        if ($data_t[0] == "username=" . $_POST['usera']) {

            $data = array();
            foreach ($data_t as $dt) {
                if (strpos($dt, '=') != false) {
                    $k = substr($dt, 0, strpos($dt, '='));
                    $v = substr($dt, strpos($dt, '=') + 1, strlen($dt) - strpos($dt, '='));
                    $data[$k] = $v;
                }
            }
            $data['timestamp'] = $data_t[1];


            if (isset($_POST['usera'])) {
                if (is_string($_POST['usera'])) {
                    $item = "username=" . $_POST['usera'];
                }
            }

            $the_ip = "";
            $entries = file_get_contents("data.line");
            $entries = (array )explode('###', $entries);
            foreach ($entries as $entry) {
                $data_t = explode("\r", trim($entry));
                if (trim($data_t[0]) == $item) {
                    foreach ($data_t as $d) {
                        if (strpos($d, 'username=') === 0) {
                            $the_ip = substr($d, 9, strlen($d) - 1);
                        }
                    }
                }
            }

            $entries = file_get_contents("data.line");
            $entries = (array )explode('###', $entries);

            // recreate file
            $handle = fopen("data.line", "w");

            if (ddfm_flock($handle, LOCK_EX)) { // do an exclusive lock

                foreach ($entries as $entry) {

                    $data_t = explode("\r", trim($entry));

                    if (trim($data_t[0]) != "") { // if valid item

                        foreach ($data_t as $d) {
                            if (strpos($d, 'username=') === 0) {
                                $test_ip = substr($d, 9, strlen($d) - 1);
                            }
                        }

                        if ($test_ip != $the_ip) { // put back

                            foreach ($data_t as $d) {
                                fwrite($handle, $d . "\r");
                            }
                            fwrite($handle, "###\r");

                        } else {
                            // skip items from this IP
                        }

                    }
                }

                ddfm_flock($handle, LOCK_UN); // release the lock
} else {
        }
    }
}
?>

 

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:

CHATBOXTEXT
7
username=helraizer
1202416953
ip=86.140.73.183
color=yellow
font=palab
message=bit of a bug, I admit
###username=Fjar
1202420306
ip=81.77.39.76
color=yellow
font=palab
message=I love you, my Sam 
###username=helraizer 
1202420637
ip=81.77.39.76
color=aqua
font=palab
message=I love you too, my Fjar!! <3
###

 

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

Link to comment
https://forums.phpfreaks.com/topic/89982-deleting-posts-from-file-bug-help/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.