Page 1 of 1

PHP error.... What am I doing wrong? Please help me!

Posted: Mon Mar 22, 2010 3:12 pm
by 3rlend
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:

Code: Select all

 
<?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

Code: Select all

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. :banghead: What?!

Thanks!!

Re: PHP error.... What am I doing wrong? Please help me!

Posted: Mon Mar 22, 2010 3:32 pm
by cpetercarter
strpos() takes three arguments - the 'haystack' (the string you want to search), the 'needle' (the character or string you want to search for), and - optionally - an offset (the position in the string from which you want to start searching).

If you specify an offset which is larger than the length of the 'haystack', you get an error message "offset not contained in the string".

You will need to work out why the offset is greater than the length of the string - the information you have given is insufficient for me to advise. Maybe you need to check the offset against the length of the 'haystack' string before using the strpos() function.

Re: PHP error.... What am I doing wrong? Please help me!

Posted: Mon Mar 22, 2010 3:37 pm
by Christopher
Actually, you are not getting an error message. You are getting a warning. And error would stop execution. To get rid of the warning you would need to wrap the substr() call in an if() to bounds check the offset against the string length.