Hey! I am making a chat program. This is the syntax of the messages:
[time-stamp:millisecons]Name:message
For example
[12846842643]Bob:Hello[12846842650]Bob2:Hey! Whats up?

To check if there are any new messages, I will compare every timestamp with the timestamp on last received message, which is a variable defined in the clients side (at the end of the php script, you will see that I echo out [lastMsgTS]text).

Here is my php script in read.php:

<?php
$NAME = $_POST['F'];
$LastMsgTS = $_POST['TS'];
$HANDLER = fopen($NAME);
$data = fread($HANDLER,filesize($NAME));
$text = "";
$TS_From = strlen($data)+1;
$TS_To = strlen($data)+1;
//search for timestamps:
$Continues = true;
$i = 0;
$first = true;
while($Continues) {
    $i += 1;
    $TS_From = strrpos($data,'[', $TS_From-1-strlen($data));
    $TS_To = strrpos($data,']', $TS_To-1-strlen($data));
    $TS_TS = substr($data,$TS_From+1, $TS_To-$TS_From-1);
    $TS_MsgTo = strpos($data,'[',$TS_From+1);

    if(is_bool($TS_MsgTo)) {
        $TS_MsgTo = strlen($data)+1;
    }
    if(!is_bool($TS_From)) {
        $TS_TSt = $TS_TS;
        if($TS_TS > $LastMsgTS) {
            if($first) {
                $LastMsgTStemp = $TS_TS;
            }
            $text += substr($data,$TS_To+1,$TS_MsgTo-$TS_To-1);
        }else {
            $Continues =  false;
        }
    }
    if($TS_From < 0) {
        $Continues = false;
    }
    if($i>100) {
        $Continues = false;
        echo 'Broke out of loop!';
    }
    $first = false;
}

if(isset($LastMsgTStemp)) {
    $LastMsgTS = $LastMsgTStemp;
}
//
$text = str_replace($text,'\n','<br />');
if(strlen($text)>0){
    echo '['.$LastMsgTS.']'.$text;
}
?>

In client side:


Send request to read.php, with variables 'F=allMessages.txt&TS='+LastMsgTS

Then after the request is received, execute the HandleRequest(Data); function, which defines LastMsgTS as the timestamp at the beginning of the responseText, and the rest of the text is added to the message bord.

The problem is this, that this error comes 100 times (only 100 because if($i > 100){$Continues = false} ):


Warning: strpos() [function.strpos]: Offset not contained in string in /home/arlenstb/public_html/pjattread.php on line 18

Why does this happen?

Please answer me!
At first, this code was in JavaScript, and it worked, but to save my server from huge Bandwidth, I tried to translate it to PHP. It is mostly the same, but something goes wrong. What?!

Thanks!!

Member Avatar for diafol
$TS_MsgTo = strpos($data,'[',$TS_From+1);

It says your offset is weird. The third parameter in the function is the offset. Is your offset greater than the length of the string ($data)?
echo out the variable to see:

echo $TS_From + 1;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.