Calmypal Posted October 10, 2009 Share Posted October 10, 2009 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]; ?>: </strong><?php echo $shouts[$i]; ?><br> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/ Share on other sites More sharing options...
mikesta707 Posted October 10, 2009 Share Posted October 10, 2009 do you have session_start() at the top of the page? also please put your code in code or php tags Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934571 Share on other sites More sharing options...
Calmypal Posted October 10, 2009 Author Share Posted October 10, 2009 Sorry, Im new to this forum and did not look for a code tag xD The script has no headers, and it worked before I modified it to get the username of the user instead of being able to set it yourself. Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934573 Share on other sites More sharing options...
.josh Posted October 10, 2009 Share Posted October 10, 2009 yes because it's just unthinkable that a coding board would have some kind of code tags. Or mention them in the ToS you signed by joining. Or mentioned in the Rules stickied everywhere. Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934575 Share on other sites More sharing options...
mikesta707 Posted October 10, 2009 Share Posted October 10, 2009 um.. all scripts send headers for the most part... but yeah if you are using the $_SESSION super global you need to have session_start() at the beginning of the page Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934577 Share on other sites More sharing options...
Calmypal Posted October 11, 2009 Author Share Posted October 11, 2009 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]; ?>: </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> Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934617 Share on other sites More sharing options...
mikesta707 Posted October 11, 2009 Share Posted October 11, 2009 does common.php have session_start() at the top of the page? Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934622 Share on other sites More sharing options...
Calmypal Posted October 11, 2009 Author Share Posted October 11, 2009 Yes, if it did not it would return an error, it has to be a small error in the script Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934631 Share on other sites More sharing options...
Calmypal Posted October 11, 2009 Author Share Posted October 11, 2009 Still cannot get it to work D: Can anyone look over the script? Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934682 Share on other sites More sharing options...
janusmccarthy Posted October 11, 2009 Share Posted October 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934693 Share on other sites More sharing options...
Calmypal Posted October 11, 2009 Author Share Posted October 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934695 Share on other sites More sharing options...
janusmccarthy Posted October 11, 2009 Share Posted October 11, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934697 Share on other sites More sharing options...
Calmypal Posted October 11, 2009 Author Share Posted October 11, 2009 I got this Parse error: syntax error, unexpected T_STRING, expecting ']' in _removed_/index.php on line 5 Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934701 Share on other sites More sharing options...
janusmccarthy Posted October 11, 2009 Share Posted October 11, 2009 Oops, sorry, typo, here you go if( isset( $_SESSION[ 'userName'])) { ob_start(); var_dump($_SESSION['userName']); $a=ob_get_contents(); ob_end_clean(); } else { $a="I'm 'special'."; } Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934705 Share on other sites More sharing options...
Calmypal Posted October 11, 2009 Author Share Posted October 11, 2009 Your script does not give me a result. But no errors. But this gives my the value. echo $_SESSION['userName']; Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934714 Share on other sites More sharing options...
janusmccarthy Posted October 11, 2009 Share Posted October 11, 2009 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=. 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. Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934717 Share on other sites More sharing options...
janusmccarthy Posted October 11, 2009 Share Posted October 11, 2009 Did you print out the value of $a? Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934719 Share on other sites More sharing options...
Calmypal Posted October 11, 2009 Author Share Posted October 11, 2009 May I have you know I did not create the script, and know very little PHP. I am a beginner who scripts using Lua and a little C++. Could you possible reacreate the script for me that would work? Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934726 Share on other sites More sharing options...
janusmccarthy Posted October 11, 2009 Share Posted October 11, 2009 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].': </strong>'.$shouts[$i].'<br>'; } ?> </p> <hr> </p> </font> </body> <footer> <font face = "Times New Roman" font size = 1> Copyright© 2009, _REMOVED_<br> </center> </html> Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934730 Share on other sites More sharing options...
Calmypal Posted October 11, 2009 Author Share Posted October 11, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934733 Share on other sites More sharing options...
janusmccarthy Posted October 11, 2009 Share Posted October 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934747 Share on other sites More sharing options...
Calmypal Posted October 11, 2009 Author Share Posted October 11, 2009 I have, and I said I am new at PHP. What it is suppose to do is let the user input text for the field text_input, then submit it, then below, in between to horizontal rules it is suppose to display what they just wrote. Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934751 Share on other sites More sharing options...
mikesta707 Posted October 11, 2009 Share Posted October 11, 2009 i havent read your code at all, but if you want to print what the user has submit, then just do echo $_POST['MyFormInputTextFieldName']; Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934752 Share on other sites More sharing options...
Calmypal Posted October 11, 2009 Author Share Posted October 11, 2009 mikesta, it reads off of a text file THEN displays it. Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934754 Share on other sites More sharing options...
janusmccarthy Posted October 11, 2009 Share Posted October 11, 2009 Do you have the code from before you made your changes? Quote Link to comment https://forums.phpfreaks.com/topic/177242-simple-plaintext-shout-script-errors/#findComment-934758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.