Visualizzazione dei risultati da 1 a 8 su 8

Discussione: Chat in php?

  1. #1

    Chat in php?

    Salve a tutti ho un errore con la variabile $_username, perché deve essere settata secondo il php. In pratica ho preso una chat in stile facebook per installarla nel mio sito web, ma non varia a seconda dell'utente e dei suoi amici.
    Eccovi il codice:
    Codice PHP:
    <?php
    session_start
    ();
    $_SESSION['$_username'] = "$_username// Must be already set
    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd" >

    <html>
    <head>
    <title>Sample Chat Application</title>
    <style>
    body {
        background-color: #eeeeee;
        padding:0;
        margin:0 auto;
        font-family:"Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif;
        font-size:11px;
    }
    </style>

    <link type="text/css" rel="stylesheet" media="all" href="css/chat.css" />
    <link type="text/css" rel="stylesheet" media="all" href="css/screen.css" />

    <!--[if lte IE 7]>
    <link type="text/css" rel="stylesheet" media="all" href="css/screen_ie.css" />
    <![endif]-->

    </head>
    <body>
    <div id="main_container">

    [url="javascript:void(0)"]Chat con $_amico[/url]
    [url="javascript:void(0)"]Chat con $_amico[/url]


    </div>

    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/chat.js"></script>

    </body>
    </html>
    Vi ringrazio in anticipo per il vostro aiuto.
    Saluti, cicciaramba.

  2. #2
    Scusami ma al di fuori del mancato ; e delle "" su quella che sembra una variabile con quello che hai postato è abbastanza difficile capire il problema, anche perchè non è che sei stato molto chiaro riguardo al problema.
    Fare o non fare....non c'è provare!

  3. #3
    Originariamente inviato da Sbidiguda
    Scusami ma al di fuori del mancato ; e delle "" su quella che sembra una variabile con quello che hai postato è abbastanza difficile capire il problema, anche perchè non è che sei stato molto chiaro riguardo al problema.
    E' vero avevo mancato i simboli, ma il fatto è che vorrei che la chat variasse in base all'utente, quindi ai suoi amici e credevo che così potesse andare, ma ora non lo credo più.
    Forse il problema è che non inserisco il file di connessione al database, secondo te è per questo?
    E' la prima volta che faccio una chat di tipo privato e non pubblico.

  4. #4
    Cosa intendi per "variare in base all'utente e ai suoi amici" ?
    Fare o non fare....non c'è provare!

  5. #5
    Originariamente inviato da Sbidiguda
    Cosa intendi per "variare in base all'utente e ai suoi amici" ?
    Intendo che deve vedere il nome utente di chi la sta provando e i suoi rispettivi amici nel database, per poi vedere se sono online.
    Grazie in anticipo per l'aiuto.

  6. #6
    Come ti ho detto prima, con il codice che hai postato non lo fai. Quello e tutto HTML con qualche richiamo a funzioni js. Molto probabilmente la gestione della chat è tutta dentro chat.js (richiamato a fondo pagina).
    Fare o non fare....non c'è provare!

  7. #7
    Secondo te dove posso inserire la variabile in chat.js o in jquery.js?
    Ecco il codice di chat.js:
    Codice PHP:
    var windowFocus true;
    var 
    username;
    var 
    chatHeartbeatCount 0;
    var 
    minChatHeartbeat 1000;
    var 
    maxChatHeartbeat 33000;
    var 
    chatHeartbeatTime minChatHeartbeat;
    var 
    originalTitle;
    var 
    blinkOrder 0;

    var 
    chatboxFocus = new Array();
    var 
    newMessages = new Array();
    var 
    newMessagesWin = new Array();
    var 
    chatBoxes = new Array();

    $(
    document).ready(function(){
        
    originalTitle document.title;
        
    startChatSession();

        $([
    windowdocument]).blur(function(){
            
    windowFocus false;
        }).
    focus(function(){
            
    windowFocus true;
            
    document.title originalTitle;
        });
    });

    function 
    restructureChatBoxes() {
        
    align 0;
        for (
    x in chatBoxes) {
            
    chatboxtitle chatBoxes[x];

            if ($(
    "#chatbox_"+chatboxtitle).css('display') != 'none') {
                if (
    align == 0) {
                    $(
    "#chatbox_"+chatboxtitle).css('right''20px');
                } else {
                    
    width = (align)*(225+7)+20;
                    $(
    "#chatbox_"+chatboxtitle).css('right'width+'px');
                }
                
    align++;
            }
        }
    }

    function 
    chatWith(chatuser) {
        
    createChatBox(chatuser);
        $(
    "#chatbox_"+chatuser+" .chatboxtextarea").focus();
    }

    function 
    createChatBox(chatboxtitle,minimizeChatBox) {
        if ($(
    "#chatbox_"+chatboxtitle).length 0) {
            if ($(
    "#chatbox_"+chatboxtitle).css('display') == 'none') {
                $(
    "#chatbox_"+chatboxtitle).css('display','block');
                
    restructureChatBoxes();
            }
            $(
    "#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
            return;
        }

        $(
    " <div />" ).attr("id","chatbox_"+chatboxtitle)
        .
    addClass("chatbox")
        .
    html('<div class="chatboxhead"><div class="chatboxtitle">'+chatboxtitle+'</div><div class="chatboxoptions">[url="javascript:void(0)"]-[/url] [url="javascript:void(0)"]X[/url]</div><br clear="all"/></div><div class="chatboxcontent"></div><div class="chatboxinput"><textarea class="chatboxtextarea" onkeydown="javascript:return checkChatBoxInputKey(event,this,\''+chatboxtitle+'\');"></textarea></div>')
        .
    appendTo($( "body" ));
                   
        $(
    "#chatbox_"+chatboxtitle).css('bottom''0px');
        
        
    chatBoxeslength 0;

        for (
    x in chatBoxes) {
            if ($(
    "#chatbox_"+chatBoxes[x]).css('display') != 'none') {
                
    chatBoxeslength++;
            }
        }

        if (
    chatBoxeslength == 0) {
            $(
    "#chatbox_"+chatboxtitle).css('right''20px');
        } else {
            
    width = (chatBoxeslength)*(225+7)+20;
            $(
    "#chatbox_"+chatboxtitle).css('right'width+'px');
        }
        
        
    chatBoxes.push(chatboxtitle);

        if (
    minimizeChatBox == 1) {
            
    minimizedChatBoxes = new Array();

            if ($.
    cookie('chatbox_minimized')) {
                
    minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
            }
            
    minimize 0;
            for (
    j=0;j<minimizedChatBoxes.length;j++) {
                if (
    minimizedChatBoxes[j] == chatboxtitle) {
                    
    minimize 1;
                }
            }

            if (
    minimize == 1) {
                $(
    '#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
                $(
    '#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
            }
        }

        
    chatboxFocus[chatboxtitle] = false;

        $(
    "#chatbox_"+chatboxtitle+" .chatboxtextarea").blur(function(){
            
    chatboxFocus[chatboxtitle] = false;
            $(
    "#chatbox_"+chatboxtitle+" .chatboxtextarea").removeClass('chatboxtextareaselected');
        }).
    focus(function(){
            
    chatboxFocus[chatboxtitle] = true;
            
    newMessages[chatboxtitle] = false;
            $(
    '#chatbox_'+chatboxtitle+' .chatboxhead').removeClass('chatboxblink');
            $(
    "#chatbox_"+chatboxtitle+" .chatboxtextarea").addClass('chatboxtextareaselected');
        });

        $(
    "#chatbox_"+chatboxtitle).click(function() {
            if ($(
    '#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
                $(
    "#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
            }
        });

        $(
    "#chatbox_"+chatboxtitle).show();
    }


    function 
    chatHeartbeat(){

        var 
    itemsfound 0;
        
        if (
    windowFocus == false) {
     
            var 
    blinkNumber 0;
            var 
    titleChanged 0;
            for (
    x in newMessagesWin) {
                if (
    newMessagesWin[x] == true) {
                    ++
    blinkNumber;
                    if (
    blinkNumber >= blinkOrder) {
                        
    document.title x+' says...';
                        
    titleChanged 1;
                        break;    
                    }
                }
            }
            
            if (
    titleChanged == 0) {
                
    document.title originalTitle;
                
    blinkOrder 0;
            } else {
                ++
    blinkOrder;
            }

        } else {
            for (
    x in newMessagesWin) {
                
    newMessagesWin[x] = false;
            }
        }

        for (
    x in newMessages) {
            if (
    newMessages[x] == true) {
                if (
    chatboxFocus[x] == false) {
                    
    //FIXME: add toggle all or none policy, otherwise it looks funny
                    
    $('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink');
                }
            }
        }
        
        $.
    ajax({
          
    url"chat.php?action=chatheartbeat",
          
    cachefalse,
          
    dataType"json",
          
    success: function(data) {

            $.
    each(data.items, function(i,item){
                if (
    item)    { // fix strange ie bug

                    
    chatboxtitle item.f;

                    if ($(
    "#chatbox_"+chatboxtitle).length <= 0) {
                        
    createChatBox(chatboxtitle);
                    }
                    if ($(
    "#chatbox_"+chatboxtitle).css('display') == 'none') {
                        $(
    "#chatbox_"+chatboxtitle).css('display','block');
                        
    restructureChatBoxes();
                    }
                    
                    if (
    item.== 1) {
                        
    item.username;
                    }

                    if (
    item.== 2) {
                        $(
    "#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
                    } else {
                        
    newMessages[chatboxtitle] = true;
                        
    newMessagesWin[chatboxtitle] = true;
                        $(
    "#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
                    }

                    $(
    "#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
                    
    itemsfound += 1;
                }
            });

            
    chatHeartbeatCount++;

            if (
    itemsfound 0) {
                
    chatHeartbeatTime minChatHeartbeat;
                
    chatHeartbeatCount 1;
            } else if (
    chatHeartbeatCount >= 10) {
                
    chatHeartbeatTime *= 2;
                
    chatHeartbeatCount 1;
                if (
    chatHeartbeatTime maxChatHeartbeat) {
                    
    chatHeartbeatTime maxChatHeartbeat;
                }
            }
            
            
    setTimeout('chatHeartbeat();',chatHeartbeatTime);
        }});
    }

    function 
    closeChatBox(chatboxtitle) {
        $(
    '#chatbox_'+chatboxtitle).css('display','none');
        
    restructureChatBoxes();

        $.
    post("chat.php?action=closechat", { chatboxchatboxtitle} , function(data){    
        });

    }

    function 
    toggleChatBoxGrowth(chatboxtitle) {
        if ($(
    '#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') == 'none') {  
            
            var 
    minimizedChatBoxes = new Array();
            
            if ($.
    cookie('chatbox_minimized')) {
                
    minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
            }

            var 
    newCookie '';

            for (
    i=0;i<minimizedChatBoxes.length;i++) {
                if (
    minimizedChatBoxes[i] != chatboxtitle) {
                    
    newCookie += chatboxtitle+'|';
                }
            }

            
    newCookie newCookie.slice(0, -1)


            $.
    cookie('chatbox_minimized'newCookie);
            $(
    '#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','block');
            $(
    '#chatbox_'+chatboxtitle+' .chatboxinput').css('display','block');
            $(
    "#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
        } else {
            
            var 
    newCookie chatboxtitle;

            if ($.
    cookie('chatbox_minimized')) {
                
    newCookie += '|'+$.cookie('chatbox_minimized');
            }


            $.
    cookie('chatbox_minimized',newCookie);
            $(
    '#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
            $(
    '#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
        }
        
    }

    function 
    checkChatBoxInputKey(event,chatboxtextarea,chatboxtitle) {
         
        if(
    event.keyCode == 13 && event.shiftKey == 0)  {
            
    message = $(chatboxtextarea).val();
            
    message message.replace(/^\s+|\s+$/g,"");

            $(
    chatboxtextarea).val('');
            $(
    chatboxtextarea).focus();
            $(
    chatboxtextarea).css('height','44px');
            if (
    message != '') {
                $.
    post("chat.php?action=sendchat", {tochatboxtitlemessagemessage} , function(data){
                    
    message message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
                    $("
    #chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+username+':</span><span class="chatboxmessagecontent">'+message+'</span></div>');
                    
    $("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
                });
            }
            
    chatHeartbeatTime minChatHeartbeat;
            
    chatHeartbeatCount 1;

            return 
    false;
        }

        var 
    adjustedHeight chatboxtextarea.clientHeight;
        var 
    maxHeight 94;

        if (
    maxHeight adjustedHeight) {
            
    adjustedHeight Math.max(chatboxtextarea.scrollHeightadjustedHeight);
            if (
    maxHeight)
                
    adjustedHeight Math.min(maxHeightadjustedHeight);
            if (
    adjustedHeight chatboxtextarea.clientHeight)
                $(
    chatboxtextarea).css('height',adjustedHeight++'px');
        } else {
            $(
    chatboxtextarea).css('overflow','auto');
        }
         
    }

    function 
    startChatSession(){  
        $.
    ajax({
          
    url"chat.php?action=startchatsession",
          
    cachefalse,
          
    dataType"json",
          
    success: function(data) {
     
            
    username data.username;

            $.
    each(data.items, function(i,item){
                if (
    item)    { // fix strange ie bug

                    
    chatboxtitle item.f;

                    if ($(
    "#chatbox_"+chatboxtitle).length <= 0) {
                        
    createChatBox(chatboxtitle,1);
                    }
                    
                    if (
    item.== 1) {
                        
    item.username;
                    }

                    if (
    item.== 2) {
                        $(
    "#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
                    } else {
                        $(
    "#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
                    }
                }
            });
            
            for (
    i=0;i<chatBoxes.length;i++) {
                
    chatboxtitle chatBoxes[i];
                $(
    "#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
                
    setTimeout('$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);'100); // yet another strange ie bug
            
    }
        
        
    setTimeout('chatHeartbeat();',chatHeartbeatTime);
            
        }});
    }

    /**
     * Cookie plugin
     */

    jQuery.cookie = function(namevalueoptions) {
        if (
    typeof value != 'undefined') { // name and value given, set cookie
            
    options options || {};
            if (
    value === null) {
                
    value '';
                
    options.expires = -1;
            }
            var 
    expires '';
            if (
    options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                var 
    date;
                if (
    typeof options.expires == 'number') {
                    
    date = new Date();
                    
    date.setTime(date.getTime() + (options.expires 24 60 60 1000));
                } else {
                    
    date options.expires;
                }
                
    expires '; expires=' date.toUTCString(); // use expires attribute, max-age is not supported by IE
            
    }
            
    // CAUTION: Needed to parenthesize options.path and options.domain
            // in the following expressions, otherwise they evaluate to undefined
            // in the packed version for some reason...
            
    var path options.path '; path=' + (options.path) : '';
            var 
    domain options.domain '; domain=' + (options.domain) : '';
            var 
    secure options.secure '; secure' '';
            
    document.cookie = [name'='encodeURIComponent(value), expirespathdomainsecure].join('');
        } else { 
    // only name given, get cookie
            
    var cookieValue null;
            if (
    document.cookie && document.cookie != '') {
                var 
    cookies document.cookie.split(';');
                for (var 
    0cookies.lengthi++) {
                    var 
    cookie jQuery.trim(cookies[i]);
                    
    // Does this cookie string begin with the name we want?
                    
    if (cookie.substring(0name.length 1) == (name '=')) {
                        
    cookieValue decodeURIComponent(cookie.substring(name.length 1));
                        break;
                    }
                }
            }
            return 
    cookieValue;
        }
    }; 
    Per favore aiutami.
    Grazie in anticipo a te e a tutti quelli del forum.

  8. #8
    Il codice di jquery.js e' troppo lungo e non si può inserire tutto: verrebbe a pezzi e, quindi, incomprensibile.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.