Título: Sencillo chat en php!!! Publicado por: Spider Burn en 14 Junio 2009, 19:58 pm Buenas!!
Como sabran anteriormente, les habia preguntado como hacer un sencillo chat en php, y me dijeron algunos que lo hiciera ajax, java etc. Lo mas probable, es que no me di a explicar la simplicidad del chat que pretendia realizar, es por eso q les dejo aqui el codigo, cabe a clarar que se hace la conexion con una base de datos espero y les sirva para algo!!! Si le pueden realizar mejoras me agradaria que las dieran a conocer!! SALU2!! *************************** funciones.php <?php function esNulo($texto){ if($texto==null||$texto=="") return true; return false; } function getConexion(){ $conexion=mysql_connect("localhost","root","root"); $bd=mysql_select_db("minichat",$conexion); return $conexion; } //FUNCION PARA SUBIR UN USUARIO function enviarmsj($nick,$msj){ $fecha=getFecha(); $enviarmensaje=mysql_query("insert into minichat values(null,'$nick','$msj','$fecha')",getConexion()); } function esUsuarioRegistrado($nick_user){ $chateadores_query="select distinct nick from minichat order by id desc limit 10"; $chat=mysql_query($chateadores_query,getConexion()); while($usuario=mysql_fetch_array($chat)){ if($nick_user==$usuario[0]){ return true; } } return false; } function getFecha(){ return date("d-m-Y H:i:s"); } ?> ************** enviarmsj.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <?php include "funciones.php"; $txtmensaje=$_GET['txtmensaje']; $txtnick=$_GET['txtnick']; enviarmsj($txtnick,$txtmensaje); ?> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Documento sin título</title> <style type="text/css"> <!-- body,td,th { color: #000000; } body { background-color: #000000; } --> </style></head> <meta http-equiv="refresh" content="0; url=chat.php?txtnick=<?php echo $txtnick ?>" /> <body> </body> </html> ******************** validarusuario.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <?php include "funciones.php"; $txtnick=$_GET['txtnick']; if(!esUsuarioRegistrado($txtnick)){ ?> <meta http-equiv="refresh" content="0; url=chat.php?txtnick=<?php echo $txtnick ?>" /> <?php }else{ ?> <script language="javascript"> alert("EL USUARIO YA ESTA REGISTRADO");</script> <meta http-equiv="refresh" content="0; url=nick.php<?php echo $txtnick ?>" /> <?php } ?> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Documento sin título</title> </head> <body> </body> </html> ***************** nick.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <?php $txtnick=$_GET['txtnick']; ?> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Documento sin título</title> <style type="text/css"> <!-- body { background-color: #000000; } .Estilo1 { color: #FFFFFF; font-size: 24px; } .Estilo2 {color: #FFFFFF} .Estilo3 { font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 16px; } .Estilo7 {color: #FFFFFF; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; } body,td,th { color: #FFFFFF; } --> </style> <style type="text/css"> <!-- .Estilo7 {color: #FFFFFF; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; } .Estilo8 {color: #FFFFFF; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; } .Estilo8 {color: #FFFFFF; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; } --> </style> </head> <body> <p align="center"><font size="+3">INTRODUCE EL NICK PARA INICIAR SESION</font> </p> <p align="center"><font size="+3">EN TECNOLOKOS CHAT</font> </p> <form id="form1" name="form1" method="get" action="validarusuario.php"> <table width="200" border="0" align="center" cellpadding="1" cellspacing="5"> <tr> <td><span class="Estilo8">NICK:</span></td> <td><input name="txtnick" type="text" id="txtnick" value="<?php echo $txtnick ?>" /></td> </tr> <tr> <td colspan="2"><div align="center"> <input type="submit" name="Submit" value="Entrar" /> </div></td> </tr> <tr> <td colspan="2"> </td> </tr> </table> </form> <p> </p> <tr> <td height="106" ><label></label> </body> </html> *************** chat.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <?php include "funciones.php"; $txtnick=$_GET['txtnick']; $chat_query="select * from minichat order by id desc limit 10"; $chateadores_query="select distinct nick from minichat order by id desc limit 10"; $chat=mysql_query($chat_query,getConexion()); $chateadores=mysql_query($chateadores_query,getConexion()); ?> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="refresh" content="18;http://localhost/chatear/chat.php?txtnick=<?php echo $txtnick ?>"> <title>EL MINICHAT</title> <style type="text/css"> <!-- body { background-color: #000000; } .Estilo1 { color: #FFFFFF; font-size: 24px; } .Estilo2 {color: #FFFFFF} .Estilo3 { font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 16px; } .Estilo7 {color: #FFFFFF; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; } body,td,th { color: #FFFFFF; } --> </style></head> <body> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="178" height="13"> <param name="movie" value="text1.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#0000FF" /> <embed src="text1.swf" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="178" height="13" bgcolor="#0000FF"></embed> </object> <form id="form1" name="form1" method="GET" action="enviarmsj.php"> <p> </p> <p align="center"><img src="images/b.gif" alt="B" width="60" height="30" /><img src="images/i.gif" alt="I" width="60" height="30" /><img src="images/e.gif" alt="E" width="60" height="30" /><img src="images/n.gif" alt="N" width="60" height="30" /><img src="images/v.gif" alt="V" width="60" height="30" /><img src="images/e.gif" alt="E" width="60" height="30" /><img src="images/n.gif" alt="N" width="60" height="30" /><img src="images/i.gif" alt="I" width="60" height="30" /><img src="images/d.gif" alt="D" width="60" height="30" /><img src="images/o.gif" alt="O" width="60" height="30" /><img src="images/s.gif" alt="S" width="60" height="30" /></p> <table width="804" height="456" border="7" align="center" cellpadding="5" cellspacing="0" bordercolor="#0066CC" bgcolor="#000000" id="AutoNumber1" style="border-collapse: collapse"> <tr> <td height="33" colspan="2" bgcolor="#000000"><iframe name="clser" id="clser" style="display:none"></iframe> <div> <div align="center"> <div id="sombra" style="position:relative;width:300;height:50r;filter:shadow(color=#00FFFF)"> <div align="center"><br /> <font size="+2"><font color="#CC0000" size="5">TECNOLOKOS CHAT</font><br /> </div> </div> <p> </p> <p>USUARIO CONECTADO: <?php echo $txtnick ?><br> </p> </div> </div></td> </tr> <tr> <td width="606"><table width="600" border="0"> <tr> <td width="123"><span class="Estilo7">FECHA MSJ</span></td> <td width="157"><span class="Estilo7">REMITENTE</span></td> <td width="306"><span class="Estilo7">MENSAJE</span></td> </tr> </table></td> <td width="606"><span class="Estilo7">LOS 10 USER'S</span></td> </tr> <tr> <td height="269"><div class="Estilo2 Estilo3" id="texto" style="PADDING:5px; OVERFLOW-X: hidden; OVERFLOW: auto; WIDTH: 400; HEIGHT: 220px"> <?php while($usuario=mysql_fetch_array($chat)){ ?> <table width="596" border="0"> <tr> <td width="120"><?php echo $usuario[3]?></td> <td width="155"><?php echo $usuario[1]?></td> <td width="307"><?php echo $usuario[2]?></td> </tr> </table> <p> </p> <?php } ?> </div></td> <td width="606"><div class="Estilo2 Estilo3" id="div" style="PADDING:5px; OVERFLOW-X: hidden; OVERFLOW: auto; WIDTH: 400; HEIGHT: 220px"> <?php while($nicks=mysql_fetch_array($chateadores)){ ?> <table width="126" border="0"> <tr> <td width="120"><?php echo $nicks[0]?></td> </tr> </table> <p> </p> <?php } ?> </div></td> </tr> <tr> <td height="106" ><label></label> <table width="200" border="0" align="center" cellpadding="1" cellspacing="5"> <tr> <td><span class="Estilo7">MENSAJE:</span></td> <td><input name="txtmensaje" type="text" id="txtmensaje" size="60" /> <input name="txtnick" type="hidden" id="txtnick" value="<?php echo $txtnick ?>"/></td> </tr> <tr> <td colspan="2"><div align="center"> <input type="submit" name="Submit" value="ENVIAR MENSAJE" /> </div></td> </tr> <tr> <td colspan="2"> </td> </tr> </table> <td height="106" ><div align="center"><img src="images/elchatMM.jpg" width="166" height="140" /></div></td> </tr> </table> </form> <p> </p> <p> </p> </body> </html> *************** Base de datos: Es una sola tabla CREATE TABLE `minichat` ( `id` int(10) NOT NULL auto_increment, `nick` varchar(20) NOT NULL, `mensaje` text NOT NULL, `fecha` varchar NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |