• Apfeltalk ändert einen Teil seiner Allgemeinen Geschäftsbedingungen (AGB), das Löschen von Useraccounts betreffend.
    Näheres könnt Ihr hier nachlesen: AGB-Änderung
  • Sprichwörter und Redewendungen sind das Thema unseres Monatswettbewerbes. Nähere Informationen dazu gibt es natürlich auch, und zwar auf dieser Seite ---> Klick

Chat integrieren

tepsch

Erdapfel
Registriert
16.02.09
Beiträge
1
Guten Abend!

Ich habe für Kollegen eine Internetseite entwickelt und wollte da ein chat system einbinden. Das hat soweit eigentlich gut geklappt, nur: Derzeit laufen 2 Sessions nebeneinander. Die vom Login-system der Page und die des Chats. Es wäre schön, das irgendwie verbinden zu können.

Irgendwie habe ich das gefühl die session wird nach einer bestimmten zeit im chat aufgelöst. Die sollte sich aber erst auflösen, wenn jemand von der page runter geht oder ähnlich.

Eventuell ist es unnötig, im chat nochmal eine session neu aufzurufen? Könnte man einfach die Sessionsvariablen setzten die der chat braucht, und wieder auf 0 setzten wenn man nicht mehr chatten möchte?

Ich bin layenprogrammierer und den chatcode habe ich teils aber nicht ganz begriffen.

Hat jemand gute Ideen?

(Pagelogin-Session)
Code:
if($userlogin->password==$md5pw) { 
$login='true';
$_SESSION['myid'] = $userlogin->id;
$_SESSION['myip'] = $ip;
$_SESSION['user'] = $userlogin->nick;
$_SESSION['laston'] = $userlogin->laston;
(Chat-code-Ausschnitt)
Code:
if ($_GET['action'] == 'get_messages') {header("Content-Type: text/xml; charset=utf-8");}
session_start();

if (!empty($_SESSION['last_action_time']) && microtime_float() - $_SESSION['last_action_time'] > 10.0) {
    $_SESSION = array();
}

define("PASSWORD", "sinfath");
define("CHAT_FILE", "msgs.txt");
define("USER_FILE", "user.txt");

if ($_GET['action'] && $_SESSION['auth'] == true) {
   switch ($_GET['action']) {
        case 'logout':
            remove_user($_SESSION['name']);
            session_destroy();
            header("Location: " . $_SERVER['PHP_SELF']);
            break;
        case 'get_messages':
            changeRoom();
            $xml = false;
            $xml = '<?xml version="1.0" ?><root>';
            $xml.= get_messages($_SESSION['last_action_time']);
            $xml.= get_userlist();
            $xml.= '</root>';
            echo $xml;
            die;
            break;
   }
}
  
if ($_POST['msg'] && $_SESSION['auth'] == true) {
   write_message(microtime_float(), $_SESSION['room'], $_SESSION['name'], $_POST['msg']);
   die;
}
  
if ($_POST['name'] && $_POST['password'] && $_SESSION['auth'] == false) {
   $_SESSION['name'] = $_POST['name'];
   if ($_POST['password'] == PASSWORD) {
        $_SESSION['auth'] = true;
        $_SESSION['room'] = '1';
      $_SESSION['last_action_time'] = microtime_float();
      add_user($_SESSION['last_action_time'], $_SESSION['name']);
   }
    else {
        $msg = "<h3 class='error'>Fehler bei der Anmeldung!</h3>";
   }
}

function changeRoom() {
    if (!empty($_GET['room']) && $_GET['room'] != $_SESSION['room']) {
        $timestamp = microtime_float()-1;
        write_message(microtime_float(), $_SESSION['room'], "Chatbot", $_SESSION['name']." ist jetzt in Raum ".$_GET['room']);
        write_message($timestamp, $_GET['room'], "Chatbot", $_SESSION['name']." betritt den Raum");
        $_SESSION['room'] = $_GET['room'];
    }
}

function get_messages($timestamp) {
   if (!file_exists(CHAT_FILE)) { die; }
   $data = file(CHAT_FILE);
   foreach ($data as $line) {
        $chunks = explode("\t", $line);
      if ($chunks[0] > $timestamp && $chunks[1] == $_SESSION['room']) {
            $new_msg[0][1] = $chunks[1];
            $new_msg[0][2] = $chunks[2];
            $new_msg[0][3] = $chunks[3];
            $new_msg[0][4] = $chunks[4];
        }
   }
    if (!is_array($new_msg)) { $new_msg = array(); }
    update_last_action(microtime_float(), $_SESSION['name']);
   foreach ($new_msg as $msg) {
        $xml .= '<message>';
        $xml .= '<room>' . $msg[1] . '</room>'; //room
        $xml .= '<time>' . $msg[2] . '</time>'; //time
        $xml .= '<user>' . $msg[3] . '</user>'; //user
        $xml .= '<mess>' . htmlspecialchars($msg[4]) . '</mess>'; //mess
        $xml .= '</message>';
   }
   return $xml;
}

if ($_REQUEST['smilie']) {make_smilie($_REQUEST['smilie']);}
function make_smilie($smilie) {
    header("Content-type: image/png");
    $text = $smilie;
    $Grafik = imagecreate(12, 12);
    $Hintergrundfarbe = imagecolorallocate($Grafik, 223, 239, 255);
    $Textfarbe = imagecolorallocate($Grafik, 225, 0, 0);
    $col = imagecolorallocate($Grafik, 0, 0, 0);
    imagefilledellipse($Grafik, 10, 10, 19, 19, $col);
    $col = imagecolorallocate($Grafik, 255, 204, 0);
    imagefilledellipse($Grafik, 10, 10, 17, 17, $col);
    imagestring($Grafik, 2, 1, 3,  $text, $Textfarbe);
    $Grafik = imagerotate($Grafik, 270, 0);
    imagepng($Grafik);
    imagedestroy($Grafik);
    $_REQUEST['smilie'] = "";
}

function update_last_action($timestamp, $name) {
   $users = get_user_array();
   if (!$handle = fopen(USER_FILE, "w+")) { die("Konnte User-Datei nicht anlegen!"); }
   if (!chmod(USER_FILE, 0700)) { die("Konnte User-Datei nicht schützen!"); }
   foreach($users as $user) {
      $chunks = explode("\t", $user);
      if (trim($chunks[1]) == trim($name)) {
            fwrite($handle, $timestamp . "\t" . $name . "\t" . $_SESSION['room'] . "\n");
            $_SESSION['last_action_time'] = $timestamp;
      } else {
            if (microtime_float() - $chunks[0] > 15.0) {
                remove_user($chunks[1]);
            } else {
                fwrite($handle, $user);
            }        
      }
   }
}
  
function write_message($timestamp, $room, $name, $msg) {
   if (!$handle = fopen(CHAT_FILE, "a")) { die("Konnte Chat-Datei nicht anlegen!"); }
   if (!chmod(CHAT_FILE, 0700)) { die("Konnte Chat-Datei nicht schützen!"); }
   fwrite($handle, $timestamp . "\t" . $room . "\t" . date("H:i:s", time()) . "\t" . $name . "\t" . htmlspecialchars(nl2br(wordwrap ($msg,42,' ',1))) . "\n");
   fclose($handle);
}
  
function get_userlist() {
   $data = get_user_array();
   $list = "";
   foreach($data as $entry) {
        $chunks = explode("\t", $entry);
        if (trim($chunks[2]) == $_SESSION['room']) {
            $xml .= '<userlist>';
            $xml .= '<username>' . $chunks[1] . '</username>'; //username
            $xml .= '<room>' . $chunks[2] . '</room>'; //room
            $xml .= '</userlist>';
        }
   }
    return $xml;
}
 
function get_user_array() {
   if (!file_exists(USER_FILE)) { return array(); }
   $data = file(USER_FILE);
   if (!is_array($data)) { $data = array(); }
   return $data;
}
  
function add_user($timestamp, $user) {
   $users = get_user_array();
   if (!$handle = fopen(USER_FILE, "w+")) { die("Konnte User-Datei nicht anlegen!"); }
   if (!chmod(USER_FILE, 0700)) { die("Konnte User-Datei nicht schützen!"); }
   foreach ($users as $name) {
      $chunks = explode("\t", $name);
        if (trim($chunks[1]) == trim($user)) {
            $msg = "<h3 class='error'>Benutzer ist bereits eingeloggt!</h3>";
            $_SESSION['auth'] = false;
        } else {
            if (microtime_float() - $chunks[0] > 15.0) {
                remove_user($chunks[1]);
            } else {
                fwrite($handle, $name );
            }
        }
    }
    if ($_SESSION['auth'] == true) {
        fwrite($handle, $timestamp . "\t" . $user . "\t" . $_SESSION['room']. "\n");
        write_message($timestamp, $_SESSION['room'], "Chatbot", "$user hat den Chat betreten");
    }
    fclose($handle);
}
  
function remove_user($user) {
   $users = get_user_array();    
   if (!$handle = fopen(USER_FILE, "w+")) { die("Konnte User-Datei nicht aktualisieren!"); }
   foreach ($users as $name) {
        $chunks = explode("\t", $name);
      if (trim($chunks[1]) != trim($user)) {
            fwrite($handle, $name);
        }
   }
   write_message(microtime_float(), $_SESSION['room'], "Chatbot", trim($user)." hat den Chat verlassen");
   fclose($handle);
   $users = get_user_array();
   if (count($users) == 0) {
        unlink(CHAT_FILE);
   }
}
  
function microtime_float() {
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}
 
Zuletzt bearbeitet: