Prikazi cijelu temu 28.09.2010 23:27
laponac84 Van mreze
Clan
Registrovan od:20.11.2008
Lokacija:Lapovo, Srbija


Predmet:Jednostavna CHAT skripta
Ova tri fajla samo iskopirajte i radice Vam perfektno Smiling

Naziv fajla: index.php
PreuzmiIzvorni kôd (PHP):
  1. <?
  2.  
  3. if(isset($_GET['logout'])){    
  4.        
  5.         //Poruka o napustanju cata
  6.         $fp = fopen("log.html", 'a');
  7.         fwrite($fp, "<div class='msgln'><i>Korisnik ". $_SESSION['name'] ." je napustio cat sesiju.</i><br></div>");
  8.         fclose($fp);
  9.        
  10.         session_destroy();
  11.         header("Location: index.php"); //preusmerava korisnika na pocetnu stranu
  12. }
  13.  
  14. function loginForm(){
  15.         echo'
  16.         <div id="loginform">
  17.         <form action="index.php" method="post">
  18.                 <p>Unesite Vas nick da bi otpoceli dopisivanje:</p>
  19.                 <label for="name">Nick:</label>
  20.                 <input type="text" name="name" id="name" />
  21.                 <input type="submit" name="enter" id="enter" value="Enter" />
  22.         </form>
  23.         </div>
  24.         ';
  25. }
  26.  
  27. if(isset($_POST['enter'])){
  28.         if($_POST['name'] != ""){
  29.                 $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
  30.         }
  31.         else{
  32.                 echo '<span class="error">Molimo Vas unesite Nick</span>';
  33.         }
  34. }
  35. ?>
  36. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  37. <html xmlns="http://www.w3.org/1999/xhtml">
  38. <head>
  39. <title>Chat - Customer Module</title>
  40. <link type="text/css" rel="stylesheet" href="style.css" />
  41. </head>
  42.  
  43. <?php
  44. if(!isset($_SESSION['name'])){
  45.         loginForm();
  46. }
  47. else{
  48. ?>
  49. <div id="wrapper">
  50.         <div id="menu">
  51.                 <p class="welcome">Dobrodosli, <b><?php echo $_SESSION['name']; ?></b></p>
  52.                 <p class="logout"><a id="exit" href="#">Napusti Chat</a></p>
  53.                 <div style="clear:both"></div>
  54.         </div> 
  55.         <div id="chatbox"><?php
  56.         if(file_exists("log.html") && filesize("log.html") > 0){
  57.                 $handle = fopen("log.html", "r");
  58.                 $contents = fread($handle, filesize("log.html"));
  59.                 fclose($handle);
  60.                
  61.                 echo $contents;
  62.         }
  63.         ?></div>
  64.        
  65.         <form name="message" action="">
  66.                 <input name="usermsg" type="text" id="usermsg" size="63" />
  67.                 <input name="submitmsg" type="submit"  id="submitmsg" value="Posalji" />
  68.         </form>
  69. </div>
  70. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
  71. <script type="text/javascript">
  72. // jQuery Document
  73. $(document).ready(function(){
  74.         //If user submits the form
  75.         $("#submitmsg").click(function(){      
  76.                 var clientmsg = $("#usermsg").val();
  77.                 $.post("post.php", {text: clientmsg});                         
  78.                 $("#usermsg").attr("value", "");
  79.                 return false;
  80.         });
  81.        
  82.         //Load the file containing the chat log
  83.         function loadLog(){            
  84.                 var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
  85.                 $.ajax({
  86.                         url: "log.html",
  87.                         cache: false,
  88.                         success: function(html){               
  89.                                 $("#chatbox").html(html); //Insert chat log into the #chatbox div                              
  90.                                 var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
  91.                                 if(newscrollHeight > oldscrollHeight){
  92.                                         $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
  93.                                 }                              
  94.                         },
  95.                 });
  96.         }
  97.         setInterval (loadLog, 2500);    //Reload file every 2.5 seconds
  98.        
  99.         //Proverava da li korsinik zaista zeli da napusti sesiju
  100.         $("#exit").click(function(){
  101.                 var exit = confirm("Da li ste sigurni da zelite da napustite sesiju?");
  102.                 if(exit==true){window.location = 'index.php?logout=true';}             
  103.         });
  104. });
  105. </script>
  106. <?php
  107. }
  108. ?>
  109. </body>
  110. </html>