Gå til innhold

Legge inn logging av ip i flatfil shoutbox


Anbefalte innlegg

Jeg har en liten shoutbox som jeg forsøker å redigere litt på. Den er nesten god nok for meg nå men jeg skulle gjerne fått lese av hva posteren's ip er og :)

 

Om dere lurer så er inputen email fjernet fordi jeg har ingen bruk for den :)

 

 

<?php

 

 

 

 

/////////////////////////////////////////////

//Declare some variables

//

//Text file database

$text_file = "db.txt";

 

//Fill in the URL to the shoutbox.php from the root

$shoutbox_url = "/shoutbox.php";

 

//Where should the script go to after adding shout

$referer_url = "/shoutbox.php";

//

/////////////////////////////////////////////

 

/////////////////////////////////////////////

//Add the shout to the database

//

if( isset($_GET['action']) && $_GET['action'] == "add" )

{

if( $_POST['email'] == "Email address" )

$_POST['email'] = "";

$new_line = $_POST['name']."|".$_POST['email']."|".date("Y/m/d H:i")."|".stripslashes(htmlspecialchars($_POST['message'])) . "\n";

if( ! $fp = fopen($text_file,"a") )

echo "Cannot open " . $text_file . "! Check the file rights.";

if( flock( $fp, LOCK_EX ) )

{

fwrite( $fp,$new_line );

flock( $fp, LOCK_UN );

} else {

echo "Couldn't lock the file " . $text_file . "!";

}

header("Location: " . $referer_url );

}

//

/////////////////////////////////////////////

 

 

/////////////////////////////////////////////

//Function show_shoutbox.

//This function will print the complete shout

//box.

//

function show_shoutbox($height = 650,$width = 400,$per_page = 15, $border="border:1px #DDDDDD solid;", $font_family="verdana",$font_size=11 )

{

//When there is requested a certain page

if( isset( $_GET['shoutbox_page'] ) )

$shoutbox_page = $_GET['shoutbox_page'];

 

//If there is no page then the default page is 1

if( empty( $_GET['shoutbox_page'] ) )

$shoutbox_page = 1;

 

//The database (a little shorter)

$file = $GLOBALS['text_file'];

 

//Open the file and return error when fail

if( ! $fp = @fopen( $file, "r" ) )

{

echo "Cannot open database file for the shoutbox!";

exit;

}

 

//Init the output_buffer (string) and the shouts (array)

$output_buffer = "";

$shouts = array();

 

//When the filesize is greater then null (file contains info)

if( filesize($file) )

{

//Read the file

$fp = fread( $fp, filesize( $file ) );

//all the shouts per line

$shouts = explode("\n",$fp);

//total of shouts

$total_shouts = count($shouts);

//total number of pages

$total_pages = ceil($total_shouts/$per_page);

//set the shout_pointer from where the shouts must be shown

$shout_pointer = (($total_shouts - ($shoutbox_page * $per_page))+$per_page)-1;

 

//when the requested page is greater then the total_pages, set the page to 1

if( $shoutbox_page > $total_pages )

$shoutbox_page = 1;

 

//create the page_numbers (the select box)

$output_buffer .= page_numbers( $total_pages , $shoutbox_page );

}

//Else use the default values... (empty shoutbox)

else

{

$shouts = 0;

$total_shouts = 0;

$shout_pointer = 0;

$output_buffer .= page_numbers( 1 , 1 );

}

 

//Loop through the shouts. descending (Z to A)

for( $i = $shout_pointer ; $i > ($shout_pointer-$per_page); $i-- )

{

//if the value is greater then -1

if( $i > -1 )

{

//if the shouts exists

if( $shouts[$i] )

list( $name, $email, $date, $shout ) = explode("|",$shouts[$i]);

 

//if the name is not empty

if( !empty( $name ) )

{

//empty the email_start and end

$email_start = "";

$email_end = "";

//if the email is not empty

if( ! empty( $email ) )

{

$email_start = "<a href=\"mailto:" . $email . "\">";

$email_end = "</a>";

}

//add to output buffer

$output_buffer .= "<strong>" . $email_start . $name . $email_end . "</strong>\n<br />" . $shout . "<br /><br />";

}

}

}

?>

<table style="width:<?=$width;?>;">

<form method="post" action="<?=$GLOBALS['shoutbox_url'];?>?action=add">

<tr><td align="center" style="font-family:<?=$font_family;?>; font-size:<?=$font_size;?>px;"><strong>Shoutbox</strong></td></tr>

<tr>

<td>

<div style="<?=$border;?> font-family:<?=$font_family;?>; font-size:<?=$font_size;?>px; height:<?=$height;?>; overflow:auto; padding:3px; width:100%;">

<?=$output_buffer;?>

</div>

</td>

</tr>

<tr>

<td>

<input type="text" name="name" value="Navn" style="<?=$border;?> font-family:<?=$font_family;?>; font-size:<?=$font_size;?>px; width:100%;" onfocus="this.value = '';" onblur="if(this.value=='') this.value='Navn';" ><br />

<input type="text" name="message" value="Melding" style="<?=$border;?> font-family:<?=$font_family;?>; font-size:<?=$font_size;?>px; width:100%;" onfocus="this.value = '';" onblur="if(this.value=='') this.value='Melding';" ><br />

</td>

</tr>

<tr>

<td align="center">

<input type="submit" value="Submit" style="<?=$border;?> font-family:<?=$font_family;?>; font-size:<?=$font_size;?>px;"><br />

<a href="http://www.***.net" target="_blank" style="font-family: verdana; font-size: 9px;">Tilbake til hjemmesiden</a>

</td>

</tr>

</table>

<?php

}

//

/////////////////////////////////////////////

 

 

/////////////////////////////////////////////

//Function page_numbers

//This function will create the select box

//with page numbers to change the page

//

function page_numbers($total_pages,$current = 1)

{

//Get the URL from the address bar

$url_bar = $_SERVER['PHP_SELF']. "?" . $_SERVER['QUERY_STRING'];

 

//Remove the shoutbox_page=XX from the URL bar

$url_bar = preg_replace( "/(&shoutbox_page=[1-9]+)/","", $url_bar );

 

//Create a buffer to output this later.

$buffer = "<div align=\"center\">Page: <select onchange=\"java script: location.href='" . $url_bar . "&shoutbox_page='+this.value;\" style=\"font-family: verdana; font-size: 10px;\">\n";

 

//Loop through the number of pages.

for( $i = 1 ; $i <= $total_pages ; $i++ )

{

//None selected

$selected = "";

//If the current page is $i, select this option

if( $current == $i )

$selected = "selected=\"selected\"";

//Add output to the buffer

$buffer .= "<option value=\"".$i."\"" . $selected . ">".$i."</option>\n";

}

//Close the output buffer

$buffer .= "</select></div><hr size=\"1\">\n";

 

//Return the buffer

return $buffer;

}

//

/////////////////////////////////////////////

 

show_shoutbox();

?>

 

 

Lenke til kommentar
Videoannonse
Annonse

Jeg vil hente ip en til den som klikker på submit knappen og lagre den i db.txt. Og aller helst skjule den så den ikke blir vist ovenfor de andre som bruker shoutboxen. Ip en bør skrives på samme linje/linjen under slik at jeg kan se hvem det var som skrev meldingen

 

Og jeg kan lite om php. Jeg driver et phpbb3 forum eneste men det finnes ingen fungerende shoutbox som kan bygges inn så langt. Derfor fant jeg en shoutbox på hotscripts som fungerer til sitt bruk :)

Lenke til kommentar

Denne koden skriver inn IP'en til brukeren. Det eneste som er forandret er merket med bold.

 

 

<?php

 

 

 

 

/////////////////////////////////////////////

//Declare some variables

//

//Text file database

$text_file = "db.txt";

 

//Fill in the URL to the shoutbox.php from the root

$shoutbox_url = "/shoutbox.php";

 

//Where should the script go to after adding shout

$referer_url = "/shoutbox.php";

//

/////////////////////////////////////////////

 

/////////////////////////////////////////////

//Add the shout to the database

//

if( isset($_GET['action']) && $_GET['action'] == "add" )

{

if( $_POST['email'] == "Email address" )

$_POST['email'] = "";

$new_line = $_POST['name']."|".$_POST['email']."|".date("Y/m/d H:i")."|".stripslashes(htmlspecialchars($_POST['message'])) . "|" . $_SERVER['REMOTE_ADDR'] . "\n";

if( ! $fp = fopen($text_file,"a") )

echo "Cannot open " . $text_file . "! Check the file rights.";

if( flock( $fp, LOCK_EX ) )

{

fwrite( $fp,$new_line );

flock( $fp, LOCK_UN );

} else {

echo "Couldn't lock the file " . $text_file . "!";

}

header("Location: " . $referer_url );

}

//

/////////////////////////////////////////////

 

 

/////////////////////////////////////////////

//Function show_shoutbox.

//This function will print the complete shout

//box.

//

function show_shoutbox($height = 650,$width = 400,$per_page = 15, $border="border:1px #DDDDDD solid;", $font_family="verdana",$font_size=11 )

{

//When there is requested a certain page

if( isset( $_GET['shoutbox_page'] ) )

$shoutbox_page = $_GET['shoutbox_page'];

 

//If there is no page then the default page is 1

if( empty( $_GET['shoutbox_page'] ) )

$shoutbox_page = 1;

 

//The database (a little shorter)

$file = $GLOBALS['text_file'];

 

//Open the file and return error when fail

if( ! $fp = @fopen( $file, "r" ) )

{

echo "Cannot open database file for the shoutbox!";

exit;

}

 

//Init the output_buffer (string) and the shouts (array)

$output_buffer = "";

$shouts = array();

 

//When the filesize is greater then null (file contains info)

if( filesize($file) )

{

//Read the file

$fp = fread( $fp, filesize( $file ) );

//all the shouts per line

$shouts = explode("\n",$fp);

//total of shouts

$total_shouts = count($shouts);

//total number of pages

$total_pages = ceil($total_shouts/$per_page);

//set the shout_pointer from where the shouts must be shown

$shout_pointer = (($total_shouts - ($shoutbox_page * $per_page))+$per_page)-1;

 

//when the requested page is greater then the total_pages, set the page to 1

if( $shoutbox_page > $total_pages )

$shoutbox_page = 1;

 

//create the page_numbers (the select box)

$output_buffer .= page_numbers( $total_pages , $shoutbox_page );

}

//Else use the default values... (empty shoutbox)

else

{

$shouts = 0;

$total_shouts = 0;

$shout_pointer = 0;

$output_buffer .= page_numbers( 1 , 1 );

}

 

//Loop through the shouts. descending (Z to A)

for( $i = $shout_pointer ; $i > ($shout_pointer-$per_page); $i-- )

{

//if the value is greater then -1

if( $i > -1 )

{

//if the shouts exists

if( $shouts[$i] )

list( $name, $email, $date, $shout ) = explode("|",$shouts[$i]);

 

//if the name is not empty

if( !empty( $name ) )

{

//empty the email_start and end

$email_start = "";

$email_end = "";

//if the email is not empty

if( ! empty( $email ) )

{

$email_start = "<a href=\"mailto:" . $email . "\">";

$email_end = "</a>";

}

//add to output buffer

$output_buffer .= "<strong>" . $email_start . $name . $email_end . "</strong>\n<br />" . $shout . "<br /><br />";

}

}

}

?>

<table style="width:<?=$width;?>;">

<form method="post" action="<?=$GLOBALS['shoutbox_url'];?>?action=add">

<tr><td align="center" style="font-family:<?=$font_family;?>; font-size:<?=$font_size;?>px;"><strong>Shoutbox</strong></td></tr>

<tr>

<td>

<div style="<?=$border;?> font-family:<?=$font_family;?>; font-size:<?=$font_size;?>px; height:<?=$height;?>; overflow:auto; padding:3px; width:100%;">

<?=$output_buffer;?>

</div>

</td>

</tr>

<tr>

<td>

<input type="text" name="name" value="Navn" style="<?=$border;?> font-family:<?=$font_family;?>; font-size:<?=$font_size;?>px; width:100%;" onfocus="this.value = '';" onblur="if(this.value=='') this.value='Navn';" ><br />

<input type="text" name="message" value="Melding" style="<?=$border;?> font-family:<?=$font_family;?>; font-size:<?=$font_size;?>px; width:100%;" onfocus="this.value = '';" onblur="if(this.value=='') this.value='Melding';" ><br />

</td>

</tr>

<tr>

<td align="center">

<input type="submit" value="Submit" style="<?=$border;?> font-family:<?=$font_family;?>; font-size:<?=$font_size;?>px;"><br />

<a href="http://www.***.net" target="_blank" style="font-family: verdana; font-size: 9px;">Tilbake til hjemmesiden</a>

</td>

</tr>

</table>

<?php

}

//

/////////////////////////////////////////////

 

 

/////////////////////////////////////////////

//Function page_numbers

//This function will create the select box

//with page numbers to change the page

//

function page_numbers($total_pages,$current = 1)

{

//Get the URL from the address bar

$url_bar = $_SERVER['PHP_SELF']. "?" . $_SERVER['QUERY_STRING'];

 

//Remove the shoutbox_page=XX from the URL bar

$url_bar = preg_replace( "/(&shoutbox_page=[1-9]+)/","", $url_bar );

 

//Create a buffer to output this later.

$buffer = "<div align=\"center\">Page: <select onchange=\"java script: location.href='" . $url_bar . "&shoutbox_page='+this.value;\" style=\"font-family: verdana; font-size: 10px;\">\n";

 

//Loop through the number of pages.

for( $i = 1 ; $i <= $total_pages ; $i++ )

{

//None selected

$selected = "";

//If the current page is $i, select this option

if( $current == $i )

$selected = "selected=\"selected\"";

//Add output to the buffer

$buffer .= "<option value=\"".$i."\"" . $selected . ">".$i."</option>\n";

}

//Close the output buffer

$buffer .= "</select></div><hr size=\"1\">\n";

 

//Return the buffer

return $buffer;

}

//

/////////////////////////////////////////////

 

show_shoutbox();

?>

 

 

Endret av alec1
Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...