Når jeg skriver en time, er det jo også inklusive at sætte sig ind i scriptet for en udefra, koble op og hente koden, rette det, smide det op på serveren igen og teste.
Så går en time hurtigt ;-)
I din kode
- /* ########################## INFO ########################## */
- /* ###################### INSTALLATION ###################### */
-
- // a) Adjust the configuration variables to your needs
-
- $file = "shouts.txt"; // Name of the file which
- // contains the shouts
- $shouts = 20; // Number of shouts to be displayed
- $maxlength_name = "20"; // Maximum length of name
- $maxlength_text = "250"; // 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">
- <input type="text" value="Navn" name="input_name" maxlength="<?php echo $maxlength_name; ?>" onfocus="if(this.value=='Navn'){this.value='';}" onblur="if(this.value==''){this.value='Navn';}" /><br />
- <input type="text" value="Besked" name="input_text" maxlength="<?php echo $maxlength_text; ?>" onfocus="if(this.value=='Besked'){this.value='';}" onblur="if(this.value==''){this.value='Besked';}" /><br />
- <input type="submit" value="Send" />
- </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"]; ?>"></a><?php
- }else{
- ?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?show=all"></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!="Navn" && $input_text!="Besked" && 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 } ?>
- </p>
Men kort fortalt, lægges det indtastede til din shout-box / chat-box ned i en fil. Så der skal du også gemme dato/tid.
Og så ser det umiddelbart ud til, at det automatisk vil komme med ud i det nederste loop (efter // print shouts) når $shouts[$i] udskrives.
Indlæg senest redigeret d. 26.02.2012 21:43 af Bruger #9814