Sabato, 10 maggio 2008 - 0.04.47 A A A Homepage | Forum | Feed RSS


salta il menu principale





Ricerca articoli




PHP: Creare una Tag Board o shoutbox per il proprio sito web
Tutorials, esempi di codice, per illustrare come realizzare applicazioni php professionali.
 
a cura della Redazione

Una particolare chat che viene più utilizzato nei siti web è la cosiddetta Tag Board o shoutbox. SI tratta di un piccolo spazio, spesso rettangolare dove gli utenti che stanno navigando sul sito possono comunicare tra loro senza bisogno di registrarsi.

Questo tutorial ha l'obiettivo di spiegare come creare un shoutbox (o Tag Board) completo con Php e MySql.

codice SQL per la tabella
potete utilizzare phpmyadmin per incollare il codice ed eseguire la query

CREATE TABLE  `shoutbox` (
  `postid` int(11) NOT NULL auto_increment,
  `poster` varchar(45) NOT NULL default '',
  `message` varchar(255) NOT NULL default '',
  `date` int(11) unsigned NOT NULL default '0',
  `ipadress` varchar(15) NOT NULL default '',
  PRIMARY KEY  (`postid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 

Codice dello shoutbox ( versione standalone )

<?php
/**************************************************************/
//| |//
//| Fast Shout Box Script 0.1 |//
//| © RevokeSoft 2006 |//
//| Author : trashofmasters |//
//| Date : 1146068386 |//
//| |//
/**************************************************************/
 
//limit : INTEGER / NULL if NULL show all messages else show N messages
//dbname: database name
//pass : database password
//host : database host , usually ( 90% cases ) localhost
//user : database username
//lunghezza_minima : INTEGER the minium lenght ( in characters ) of the username and message
//message_default_value : show the default value for message
//username_default_value : show the default value for username
 
/**********************************************/
/**/ #EDIT UNDER HERE# /**/ 
/**********************************************/
/**/ 
$message_default_value 'Messaggio'/**/
/**/ 
$username_default_value 'Username'/**/
/**/ 
$lunghezza_minima 10/**/
/**/ 
$dbname ''/**/
/**/ 
$user ''/**/
/**/ 
$pass ''/**/
/**/ 
$host 'localhost'/**/
/**/ 
$limit NULL/**/
/**********************************************/
 
//connecting to the database
$connection mysql_connect($host,$user,$pass);
$database mysql_select_db($dbname,$connection);
 
if(
$limit == NULL){
$limit '';
}else{
$limit 'LIMIT '.$limit;
}
 
//extracting messages from the database
$query 'SELECT * FROM shoutbox ORDER BY date DESC '.$limit;
$select mysql_query($query);
 
//check if the form is correctly filled and if the submit button was pressed
if($_GET['act'] == 'post' ){
 
//check if the directive magic_quotes_gpc is set to 1 in php.ini 
//if not escapes all special characters
//else do anything
if(!get_magic_quotes_gpc()){
$poster addslashes($_POST['username']); 
$message addslashes($_POST['message']);
}else{
$poster $_POST['username'];
$message $_POST['message'];
}
//check if the fields are filled
if(empty($poster) or empty($message)){
$error .= '<p class="error">Impossibile Inserire Il Messaggio : Campi Vuoti!</p>';
}
//check if the field are more than $lunghezza_minima characters
if(strlen($poster) < $lunghezza_minima or strlen($message) < $lunghezza_minima){
$error .= '<p class="error">Impossibile Inserire Il Messaggio : Minimo '.$lunghezza_minima.' Caratteri!</p>';
}
//check if the form is submitted only by pressing submit
if($poster == $username_default_value or $message == $message_default_value){
$error .= '<p class="error">Impossibile Inserire Il Messaggio : Valori Di Default Non Ammessi!</p>';
}
}
?>

 

Codice HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>shout box</title>
<style type="text/css">
.textarea {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #3C6180;
text-decoration: none;
width: 150px;
padding: 1px;
vertical-align: middle;
border: 2px solid #B3CADD;
}
.shout {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #009900;
}
.cancel {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #CC0000;
}
.username {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #006699;
}
.scrollarea {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
background-color: #CBDBE4;
border: 2px solid #B3CADD;
width: 150px;
padding: 2px;
}
.success {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #006600;
background-color: #C4F5BC;
border: 1px solid #009900;
width: 150px;
padding: 2px;
 
}
.error {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #CC0000;
background-color: #F4BDBD;
border: 1px solid #CC0000;
width: 150px;
padding: 2px;
 
}
a{
text-decoration: none;
}
</style>
</head>
 
<body>
<form id="shoutpost" method="post" action="?act=post">
<table width="162" border="0" cellpadding="1" cellspacing="1">
<tr>
<td width="156" class="scrollarea" style="vertical-align: top; height: 200px;">
<marquee direction="up" scrollamount="1">

 

Codice PHP:

<?php 
//fetching the messages
while($shout mysql_fetch_array($select)){ 
//un-escape the string
if(get_magic_quotes_gpc()) {
$mex stripslashes($shout['message']);
}else{
$mex $shout['message'];

//show the messages and convert them timestamp into a human readable date
echo '<span class="username">'.$shout['poster'].'</span> : <br />'.$mex.'<br />'.date('d/m/Y H:i:s',$shout['date']).'<br />'; } 
?>

Codice HTML:

</marquee></td>
</tr>
<tr>
<td><input name="username" type="text" class="textarea" id="username" onfocus="this.value=('')" value="<?php echo $username_default_value; ?>" size="80"/></td>
</tr>
<tr>
<td><input name="message" type="text" class="textarea" id="messaggio" onfocus="this.value=('')" value="<?php echo $message_default_value; ?>" maxlength="255"/></td>
</tr>
<tr>
<td><input name="Submit" type="submit" class="shout" value="Urla" />
<input name="Submit2" type="reset" class="cancel" value="Cancella" /></td>
</tr>
</table>
</form>

 

Codice PHP:

<?php 
//check if there is any error 
if(!empty($error))
{
echo 
$error;
}elseif(isset(
$_POST['Submit'])){
//if there aren't submit data into database
$query "INSERT INTO shoutbox (poster,message,date,ipadress) VALUES ('".$poster."','".$message."','".time()."','".$_SERVER['REMOTE_ADDR']."');";
$ins mysql_query($query);
if(
$ins)
{
//success message
echo '<p class="success">Messaggio Inserito Con Successo <a href='.$_SERVER['PHP_SELF'].'>aggiorna</a></p>';
}
}
?>

 

Codice HTML:

</body>
</html>

 

Fonte: http://forum.tutorialweb.org/showthread.php?p=4111#post4111

 

Le ultime news
Stampa NewsStampa Pagina - Info sull'autoreInfo sull'autore
SEGNALA QUESTA LEZIONE AD UN AMICO CON ICQICQ o EMAILEMAIL
forumDISCUTI DI QUESTA LEZIONE SUL FORUM

Pubblicità locale Web Marketing

Newsletter

Iscriviti alla nostra newsletter per ricevere novita e aggiornamenti dal nostro sito in modo GRATUITO!