Jump to content

Recommended Posts

This script will not work.

Could you possibly help? (the $_SESSION['userName'] is working)

 

 

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
    <b><?php echo $_SESSION['userName']; ?></b><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);
?>
</p><p>
<?php
  $usersname = $_SESSION['userName'];
  //insert new shout
  $input_name = $_POST['usersname'];
  $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_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_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_text != $input_text){
      $handle = fopen($file,"a"); //open shouts file to write (append)
      fputs($handle,"$usersname|||||$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++){
    ?><strong><?php echo $names[$i]; ?>:&nbsp</strong><?php echo $shouts[$i]; ?><br>
  <?php } ?>

Link to comment
https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/
Share on other sites

Here is the full code, it shows no errors, yet when I submit the text it does not do what it is suppose to, and still does not show any errors. (Note anything that is _REMOVED_ I removed for privacy.)

 

<?php
require_once('common.php');
checkUser();
?>

<html>
<head>
<font face="Fixedsys">
<center>
<img src = "Banner.bmp"><br>
<title>_REMOVED_</title>
<h3><a href = "index.php">Home</a> | <b>Chat</b> | <a href="_REMOVED_">_REMOVED_</a> | <a href="staff.php">Staff</a></h3>
</font>
</head>
<body>
<font face="Comic Sans MS" font size = "2">
<hr>
<p>
Info on 'Chat':<br>
'Chat' has an 8 chats history, max length of 140 characters.<br>
If you want to request a full history, please email me.<br>
<br>
<?php
   // a) Adjust the configuration variables to your needs

         $file = "shouts.txt"; // Name of the file which
                               // contains the shouts
         $shouts = 8; // Number of shouts to be displayed
         $maxlength_text = "140"; // 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>
  <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
    <b><?php echo $_SESSION['userName']; ?></b><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);
?>
</p><p>
<?php
  $usersname = $_SESSION['userName'];
  //insert new shout
  $input_name = $_POST['usersname'];
  $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_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_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_text != $input_text){
      $handle = fopen($file,"a"); //open shouts file to write (append)
      fputs($handle,"$usersname|||||$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++){
    ?><strong><?php echo $names[$i]; ?>:&nbsp</strong><?php echo $shouts[$i]; ?><br>
  <?php } ?>
</p>
<hr>
</p>
</font>
</body>
<footer>
<font face = "Times New Roman" font size = 1>
Copyright© 2009, _REMOVED_<br>
</center>
</html>

Still cannot get it to work D:

Can anyone look over the script?

 

You still haven't defined the behavior that's wrong.  You're just saying..."$_SESSION['username']: it's not working the way it's supposed to".  You don't give us any output.  Give us a frame of reference.

 

At this point, I'm not sure if the problem is a possible bug with your script, a design flaw, or if you're just "special".  Let's assume the worst and see what's happening.

 

Let's try some of the basic ways to troubleshoot something wrong.

 

1.  What is the value of $_SESSION['username']? 

 

Add the following code at the top of your script:

if( isset( $_SESSION[ 'username]))
{
  ob_start();
  var_dump($_SESSION['username']);
  $a=ob_get_contents();
  ob_end_clean();
}
else
{
  $a="I'm 'special'.";
}

 

Either print the value of $a to the screen or to a file.  What did you get for the value of $_SESSION?

 

If you're 'special', make sure your session has started, you haven't unset the variable somewhere, and that you actually set the value of $_SESSION['username'].

If you got the value you're expecting, tell us the intended behavior of your program and how this deviates.

If you got a value you weren't expecting or it was blank, try and find the places where *that* value gets set. 

That value returns that value of the user logged on, so if someone is logged off, it will be blank.

The program is suppose to add a simple line of text, consisting of the username and the text entered, and then read it off the text. It was working before I changed the value were you could change your name, to where it was your username. The page cannot be accessed when logged off and it returns no errors.

That value returns that value of the user logged on, so if someone is logged off, it will be blank.

The program is suppose to add a simple line of text, consisting of the username and the text entered, and then read it off the text. It was working before I changed the value were you could change your name, to where it was your username. The page cannot be accessed when logged off and it returns no errors.

 

Great, what happens when you add the code I just told you to add, and print the value of $a to a file?

I'm looking at your code and I'm seeing a couple of things that makes the baby coding Jeebus cry.  It might also be a source of your problems or of problems in the future.

 

Try and follow some simple steps.

 

1.  Keep your html and your php code as separate as possible.  Ideally, you just want to limit their mixing to simple output statements.  Code is just easier to trace if you can read most of it at once.  I prefer to keep the php code above the form code.  Find a style that works for you.

 

2.  Unless you specifically output text via the echo or print commands, your php code doesn't actually write anything.  I mention this because there are a couple of places where you've placed the php code within paragraph markers with nothing else.

 

3.  When echoing a line of html that uses php code, don't write half the line in php and break the html to write a php block.  Instead, echo the whole line within a php block.  That means, don't do this:

<iframe <?php echo 'id="'.$foobar.'"'; ?> >

And do this instead:

<?php 
   echo '<iframe id="'.$foobar.'" >';
?>

 

4.  There's almost never a good reason to have two variables with almost the same name.  That is, avoid $name and $names, $shout and $shouts, etc.  You can separate them notationally and still show they're related (pair $a_names or $name_array with $name instead)

 

5.  Speaking of $shouts: You're using $shouts to be the number of default shout messages (value=8).  Later in the script, you recycle the variable $shouts to be an array.  Try to avoid doing things like this, and verify this isn't part of your problem.

 

6.  Defining a function does not actually call that function.  They definitely doesn't belong mixed in with your html, and should be at the top of the page.  I prefer to write the function and pass in everything the function needs as a parameter.

 

 

I'm not sure if it will work, as I'm not sure what it is exactly you're trying to do. 

 

This is in general what I'm talking about though when I talk about separation of code. 

It's at least a starting point for you to jump off of. 

 

 

Oh, btw, you need to define your checkUser method.

 

 

<?php
   require_once('common.php');
   checkUser();

   //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;
  }




   // a) Adjust the configuration variables to your needs

   $file = "shouts.txt"; // Name of the file which contains the shouts
   $shouts = 8; // Number of shouts to be displayed
   $maxlength_text = "140"; // 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)


  //number of shouts to be displayed
  $usersname = $_SESSION['userName'];
  $input_name = $_POST['usersname'];
  $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_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_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_text != $input_text){
      $handle = fopen($file,"a"); //open shouts file to write (append)
      fputs($handle,"$usersname|||||$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);
?>

<html>
<head>
<font face="Fixedsys">
<center>
<img src = "Banner.bmp"><br>
<title>_REMOVED_</title>
<h3><a href = "index.php">Home</a> | <b>Chat</b> | <a href="_REMOVED_">_REMOVED_</a> | <a href="staff.php">Staff</a></h3>
</font>
</head>
<body>
<font face="Comic Sans MS" font size = "2">
<hr>
<p>
Info on 'Chat':<br>
'Chat' has an 8 chats history, max length of 140 characters.<br>
If you want to request a full history, please email me.<br>
<br>
<p>
  <?php
    echo '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
    echo '<b>'.$_SESSION['userName'].'</b><br />';

    echo '<input type="text" value="Your Text" name="input_text" maxlength="'.$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
  //print shouts
  for($i=0;$i<$max && $i<count($names);$i++)
  {
    echo '<strong> '.$names[$i].':&nbsp</strong>'.$shouts[$i].'<br>';
  } 
?>
</p>
<hr>
</p>
</font>
</body>
<footer>
<font face = "Times New Roman" font size = 1>
Copyright© 2009, _REMOVED_<br>
</center>
</html>

Didn't work, and still no errors D=

Maybe if you just write a whole new two scripts, a header and the php page(script)?

You know how to access the checkUser(), using common.php. (Check user just checks to see if the user is logged in, if not it redirects them to a different page)

Didn't work, and still no errors D=

Maybe if you just write a whole new two scripts, a header and the php page(script)?

You know how to access the checkUser(), using common.php. (Check user just checks to see if the user is logged in, if not it redirects them to a different page)

 

All I did was move the code around.  I didn't change the logic.

 

There are no errors because there are no errors.  It's like if you had a program that did the following.

 

Take a number and store it to A.

Take a number and store it to B.

Add A + B.

Display '6'.

 

You're complaining that you gave 100 for A and 200 for B, but it only prints out 6 and you're not getting any errors.

 

The problem is with the logic.  I don't know what it is *exactly* you want to do, therefore I cannot tell you where your script is going wrong.

 

What I can say is you can probably look at the script, and if YOU know what you want it to do, can likely see where it might be wrong.

 

Review your script.

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.