ccc

Chat básico en PHP

<!DOCTYPE html>
<html>
<head>
<title>Chat LSG</title>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<?php
session_start();
 
function loginForm(){
    echo'
    <div id="loginform">
    <form action="index.php" method="post">
        <p>Introduce tu nombre para poder chatear:</p>
        <label for="name">Nombre:</label>
        <input type="text" name="name" id="name" />
        <input type="submit" name="enter" id="enter" value="Entrar en el chat" />
    </form>
    </div>
    ';
}

if(isset($_GET['logout'])){ 
    //Simple exit message
    $fp = fopen("log.html", 'a');
    fwrite($fp, "<div class='msgln'><i>El usuario ". $_SESSION['name'] ." ha salido del chat.</i><br></div>");
    fclose($fp);
    session_destroy();
    header("Location: index.php"); //Redirect the user
}
if(isset($_POST['enter'])){
    if($_POST['name'] != ""){
        $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
    }
    else{
        echo '<span class="error">Por favor, escribe un nombre</span>';
    }
}

if(!isset($_SESSION['name'])){
    loginForm();
}
else { ?>
<div id="wrapper">
<div id="menu">
<p class="welcome">Bienvenido, <b><?php echo $_SESSION['name']; ?></b></p>
<p class="logout"><a id="exit" href="#">Salir del Chat</a></p>
<div style="clear:both"></div>
</div>    
<div id="chatbox">
<?php
if(file_exists("log.html") && filesize("log.html") > 0){
$handle = fopen("log.html", "r");
$contents = fread($handle, filesize("log.html"));
fclose($handle);
echo $contents;
}
?>
</div>
 
<form name="message" action="">
<input name="usermsg" type="text" id="usermsg" size="63" />
<input name="submitmsg" type="submit"  id="submitmsg" value="Send" />
</form>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function(){
$("#exit").click(function(){
var exit = confirm("¿Estás seguro de que quieres salir?");
if(exit==true){window.location = 'index.php?logout=true';}
});
$("#submitmsg").click(function(){
var clientmsg = $("#usermsg").val();
$.post("post.php", {text: clientmsg});
$("#usermsg").attr("value", "");
return false;
});
setInterval (loadLog, 2500);
});

function loadLog(){
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
$.ajax({
url: "log.html",
cache: false,
success: function(html){
$("#chatbox").html(html);
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal');
}
  },
});
}
</script>
<?php
}
?>
</body>
</html>

style.css: body { font:12px arial;color: #222;text-align:center;padding:35px; }
form, p, span { margin:0;padding:0; }
input { font:12px arial; }
a { color:#0000FF;text-decoration:none; }
a:hover { text-decoration:underline; }
#wrapper, #loginform { margin:0 auto;padding-bottom:25px;background:#EBF4FB;width:504px;border:1px solid #ACD8F0; }
#loginform { padding-top:18px; }
#loginform p { margin: 5px; }
#chatbox { text-align:left;margin:0 auto;margin-bottom:25px;padding:10px;background:#fff;height:270px;width:430px;border:1px solid #ACD8F0;overflow:auto; }
#usermsg { width:395px;border:1px solid #ACD8F0; }
#submit { width: 60px; }
.error { color: #ff0000; }
#menu { padding:12.5px 25px 12.5px 25px; }
.welcome { float:left; }
.logout { float:right; }
.msgln { margin:0 0 2px 0; }

post.php: session_start();
if(isset($_SESSION['name'])){
$text = $_POST['text'];
$fp = fopen("log.html", 'a');
fwrite($fp, "
(".date("g:i A").") ".$_SESSION['name'].": ".stripslashes(htmlspecialchars($text))."
");
}

1 comentario:

  1. Hola, tengo una carpeta de fotos llamada /usuarios/ y todos tienen un numero de legajo ejemplo: 123.jpg / 123456.jpg como puedo imprimir `segun legajo sumarle .jpg` para que salga al lado de su nombre su foto. osea _SESSION['name']. + jpg

    ResponderEliminar