Jump to content

Error In Flat File Shout Script


dwite

Recommended Posts

Hi guys, i would love if you could help me out with this script.

I am getting these errors:

 

Notice: Undefined offset: 1 in /www/tests/shoutbox/shout on line 111 Notice: Undefined variable: str_br in /www/tests/geminixx/shoutbox/shout on line 81 Notice: Undefined variable: str_br in /www/tests/shoutbox/shout on line 81 Notice: Undefined offset: 1 in /www/tests/shoutbox/shout on line 145

 

Here is the code:

 

<?php

         $file = "shouts.txt"; // Name of the file which
                               // contains the shouts
         $shouts = 4; // Number of shouts to be displayed
         $maxlength_name = "20"; // Maximum length of name
         $maxlength_text = "150"; // Maximum length of text
         $break_name = "15"; // Break name after characters
                             // without space
         $break_text = "15"; // Break text after characters
                             // without space

?>
<p>
  <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
    <input type="text" value="Your name" name="input_name" maxlength="<?php echo $maxlength_name; ?>" onfocus="if(this.value=='Your name'){this.value='';}" onblur="if(this.value==''){this.value='Your name';}" /><br />
    <input type="text" value="Your text" name="input_text" maxlength="<?php echo $maxlength_text; ?>" onfocus="if(this.value=='Your text'){this.value='';}" onblur="if(this.value==''){this.value='Your text';}" /><br />
    <input type="submit" value="Shout!" />
  </form>
</p>
<hr />
<p>
<?php
  //function to break text after x characters
  function str_break($str,$maxlen){
    $nobr = 0;
    $len = strlen($str);

    for($i=0;$i<$len;$i++){
      if(($str[$i]!=' ') && ($str[$i]!='-') && ($str[$i]!="\n")){
        $nobr++;
      }else{
        $nobr = 0;

        if($maxlen+$i>$len){
          $str_br .= substr($str,$i);
          break;
        }
      }
      
      if($nobr>$maxlen){
        $str_br .= ' '.$str[$i];
        $nobr = 1;
      }else{
        $str_br .= $str[$i];
      }
    }
    
    return $str_br;
  }

  //number of shouts to be displayed
  $display = (isset($_GET["show"]) ? "all" : $shouts);

  //print links to either show all or specific number of shouts
  if($display == "all"){
    ?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>">View small shoutbox</a><?php
  }else{
    ?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?show=all">View all shouts</a><?php
  }
?>
</p><p>
<?php
  //insert new shout
  $input_name = $_POST["input_name"];
  $input_text = $_POST["input_text"];
  
  //check if form has been submitted
  if(isset($input_name) && isset($input_text) && $input_name!="Your name" && $input_text!="Your text" && strlen($input_name)>0 && strlen($input_text)>0){
    //get last name and comment
    $handle = fopen($file,"r");

    while(!feof($handle)){
      $row = fgets($handle,999999);
      list($tmp_name,$tmp_text) = split("\|\|\|\|\|",$row);

      if($tmp_name != "" && $tmp_text != ""){
        $last_name = $tmp_name;
        $last_text = str_replace("\n","",$tmp_text);
      }
    }
    
    fclose($handle);

    $input_name = str_break($input_name,$break_name); //break name
    $input_name = str_replace("<","<",$input_name); //prevent html input
    $input_name = str_replace(">",">",$input_name); //prevent html input
    $input_name = stripslashes($input_name); //strip slashes
    
    $input_text = str_break($input_text,$break_text); //break text
    $input_text = str_replace("<","<",$input_text); //prevent html input
    $input_text = str_replace(">",">",$input_text); //prevent html input
    $input_text = stripslashes($input_text); //strip slashes

    if($last_name != $input_name || $last_text != $input_text){
      $handle = fopen($file,"a"); //open shouts file to write (append)
      fputs($handle,"$input_name|||||$input_text\n"); //insert name and shout
      fclose($handle); //close file handle
    }
  }

  //read shouts file
  $names = array(); //array to store names
  $shouts = array(); //array to store shouts
  $handle = fopen($file,"r"); //open shouts file to read
  
  while(!feof($handle)){ //read row by row
    $row = fgets($handle,999999);
    list($name,$shout) = split("\|\|\|\|\|",$row);
    
    if($name){
      array_push($names,$name);
      array_push($shouts,$shout);
    }
  }
  
  fclose($handle); //close file handle

  //reverse arrays so that new lines are first
  $names = array_reverse($names);
  $shouts = array_reverse($shouts);

  //number of shouts to really print
  $max = ($display == "all" ? count($names) : $display);

  //print shouts
  for($i=0;$i<$max && $i<count($names);$i++){
    ?><p><strong><?php echo $names[$i]; ?>:</strong> <?php echo $shouts[$i]; ?></p>
  <?php } ?>

 

I tried fixing it with the tiny bit of knowledge i have but i couldn't  :'(

Link to comment
Share on other sites

To fix the 'undefined variable' warnings, this should work;

function str_break($str,$maxlen) {
$str_br = ''; // <---- ADD THIS LINE
  	$nobr = 0;

 

For the 'undefined offset' warning, can you point out which lines are 111 and 145? What you posted doesn't have 145 lines . . .

Link to comment
Share on other sites

Yes it was created and it have a few text in it, as i was testing it out,  and i had but in the part you gave to me and the lines have change. Would you like to the new lines?

Link to comment
Share on other sites

split() returns an array of values based on the pattern. list() assigns array values to scalar variables.

 

// This line of code ...
list($name,$shout) = split("\|\|\|\|\|",$row);

// ... is equivalent to ...
$ar = split("\|\|\|\|\|",$row);
$name = $ar[0];
$shout = $ar[1];

 

"Undefined offset 1 ..." indicates that the returned array contains a single value (which is element zero) and $ar[1] does not exist. The manual for split says this will happen if the pattern is not found or the string is empty. split() will return false if it encounters an error (in that case offset 0 (zero) will be undefined). So, in this case, the variable $row is empty or does not contain the pattern.

 

Note: The manual says this function is deprecated. You should use preg_split or explode instead.

Link to comment
Share on other sites

This is the little bit i did:

<?php
/* ########################## INFO ##########################

                       Advanced shoutbox
                          Version 1.0

         Free script from WB - Webdesign for beginners.
          Visit http://plohni.com/wb for more scripts.
               Feel free to modify this script.
                              lgp

/* ########################## INFO ########################## */
/* ###################### INSTALLATION ###################### */

   // a) Adjust the configuration variables to your needs

         $file = "shouts.txt"; // Name of the file which
                               // contains the shouts
         $shouts = 4; // Number of shouts to be displayed
         $maxlength_name = "20"; // Maximum length of name
         $maxlength_text = "150"; // Maximum length of text
         $break_name = "15"; // Break name after characters
                             // without space
         $break_text = "15"; // Break text after characters
                             // without space

   // b) Copy this code to your PHP file
   // c) Copy your PHP file and the shouts file defined in
   //    variable $file to your server using ASCII mode
   // d) Make the shouts file writable (Windows: adjust
   //    security, Unix: chmod 777)

/* ###################### INSTALLATION ###################### */
/* ############# SCRIPT (EDIT AT YOUR OWN RISK) ############# */
?>
<p>
  <?php echo '<form action="'. $_SERVER["PHP_SELF"] .'" method="post">
    <input type="hidden" value="'. $username .'" name="input_name" maxlength="'. $maxlength_name .'" /><br />
    <input type="text" value="" name="input_text" maxlength="'. $maxlength_text .'" /><br />
    <input type="submit" value="Shout!" />
  </form>'; ?>
</p>
<hr />
<p>
<?php
  //function to break text after x characters
  function str_break($str,$maxlen){
    $str_br = '';
    $nobr = 0;
    $len = strlen($str);

    for($i=0;$i<$len;$i++){
      if(($str[$i]!=' ') && ($str[$i]!='-') && ($str[$i]!="\n")){
        $nobr++;
      }else{
        $nobr = 0;

        if($maxlen+$i>$len){
          $str_br .= substr($str,$i);
          break;
        }
      }
      
      if($nobr>$maxlen){
        $str_br .= ' '.$str[$i];
        $nobr = 1;
      }else{
        $str_br .= $str[$i];
      }
    }
    
    return $str_br;
  }

  //number of shouts to be displayed
  $display = (isset($_GET["show"]) ? "all" : $shouts);

  //print links to either show all or specific number of shouts
  if($display == "all"){
    ?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>">View small shoutbox</a><?php
  }else{
    ?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?show=all">View all shouts</a><?php
  }
?>
</p><p>
<?php

  //insert new shout
  if (isset($_POST["input_name"]))
  {
  $input_name = $_POST["input_name"];
  }
  if (isset($_POST["input_text"]))
  {
  $input_text = $_POST["input_text"];
  }
  //check if form has been submitted
  if(isset($input_name) && isset($input_text) && $input_name!="Your name" && $input_text!="Your text" && strlen($input_name)>0 && strlen($input_text)>0){
    //get last name and comment
    $handle = fopen($file,"r");

    while(!feof($handle)){
      $row = fgets($handle,999999);
      list($tmp_name,$tmp_text) = split("\|\|\|\|\|",$row);

      if($tmp_name != "" && $tmp_text != ""){
        $last_name = $tmp_name;
        $last_text = str_replace("\n","",$tmp_text);
      }
    }
    
    fclose($handle);

    $input_name = str_break($input_name,$break_name); //break name
    $input_name = str_replace("<","<",$input_name); //prevent html input
    $input_name = str_replace(">",">",$input_name); //prevent html input
    $input_name = stripslashes($input_name); //strip slashes
    
    $input_text = str_break($input_text,$break_text); //break text
    $input_text = str_replace("<","<",$input_text); //prevent html input
    $input_text = str_replace(">",">",$input_text); //prevent html input
    $input_text = stripslashes($input_text); //strip slashes

    if($last_name != $input_name || $last_text != $input_text){
      $handle = fopen($file,"a"); //open shouts file to write (append)
      fputs($handle,"$input_name|||||$input_text\n"); //insert name and shout
      fclose($handle); //close file handle
    }
  }

  //read shouts file
  $names = array(); //array to store names
  $shouts = array(); //array to store shouts
  $handle = fopen($file,"r"); //open shouts file to read
  
  while(!feof($handle)){ //read row by row
    $row = fgets($handle,999999);
    list($name,$shout) = split("\|\|\|\|\|",$row);
    
    if($name){
      array_push($names,$name);
      array_push($shouts,$shout);
    }
  }
  
  fclose($handle); //close file handle

  //reverse arrays so that new lines are first
  $names = array_reverse($names);
  $shouts = array_reverse($shouts);

  //number of shouts to really print
  $max = ($display == "all" ? count($names) : $display);

  //print shouts
  for($i=0;$i<$max && $i<count($names);$i++){
    ?><p><strong><?php echo $names[$i]; ?>:</strong> <?php echo $shouts[$i]; ?></p>
  <?php } ?>

 

When i post messages, i get these errors:

 

Notice: Undefined offset: 1 in /www/tests/geminixx/shoutbox/shout on line 118 

Notice: Undefined offset: 1 in /www/tests/geminixx/shoutbox/shout on line 152

 

Line 118 is:

  list($tmp_name,$tmp_text) = split("\|\|\|\|\|",$row);

 

Line 152 is: 

 list($name,$shout) = split("\|\|\|\|\|",$row);

 

This is whats currently in the .txt file:

 

user|||||hey

user|||||hey

user|||||whats up?

user||||how you doing?

 

However when the .txt file is empty i only get the line 152 error.

Link to comment
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.