dcsimg
www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: [RESOLVED] Chat Room (Timestamp)

  1. #1
    Join Date
    Feb 2016
    Posts
    30

    resolved [RESOLVED] Chat Room (Timestamp)

    Hi,

    Can anyone point me in the direction to add a timestamp to my PHP please.

    I have set up a column in mysql called 'time'

    here is my current code

    PHP Code:
    <script type="text/javascript">
     
     
    function 
    update()
    {
        $.
    post("server.php", {}, function(data){ $("#screen").val(data);});  
     
        
    setTimeout('update()'1000);
    }


    $(
    document).ready(
     
    function() 
        {
         
    update();
     
         $(
    "#button").click(    
          function() 
          {         
           $.
    post("server.php"
        { 
    message: $("#message").val()},
        function(
    data){ 
        $(
    "#screen").val(data); 
        $(
    "#message").val("");
        }
        );
          }
         );
        });
     
     
    </script> 
    thanks

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,429
    You have posted JQuery or similar type tool in PHP.

    Where is the PHP code?

    Also are you looking at using the local servertime, a specific timezone or zulu time as the timestamp?
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Feb 2016
    Posts
    30
    Yes sorry its because i was working between the two and forgot where i posted it, my bad.

    I would prefer to use Zulu time.

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,429
    If you want GMT (aka zulu time) then you need to set up in PHP a timezone reference to Europe/London and then obtain a date stamp, this then ensures that all chat times are based on the Greenwich Meridian and you can also write in a server time stamp in to any JavaScript you use so your client can keep sync with the server.

    PHP Code:
    <?php
    date_default_timezone_set
    ("Europe/London");
    $serverTime date("Y-m-d H:i:s");
    is an example of setting the server to generate a date stamp based on zulu time.

    To ensure that your javascript function in the users machine has a reference to the server time, you create a PHP element to write a variable in javascript with the date ref and then to keep it synced, use a timer value set by it and update it using the javascript time methods.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  5. #5
    Join Date
    Feb 2016
    Posts
    30
    Right... slightly lost.

    How do I implement that on the code posted above.

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,429
    You put it in the head of your PHP script and then you can use the variable that contains the stamp in your web page.

    If your user is not in the local timezone of the server, any time stamps you use on the users machine will be their local time unless you create a sync time routine that tracks the time from being set by the server. That needs to be an ISO type date and time stamp like in the previous post.

    In javascript, you can var serverTime = new Date("<php echo $serverTime;?>"); which then creates a date object that is set to the server time, you can then auto increment the value of the time in a localized routine and any drift corrected at the server.

    I am guessing from your response that you are new to this and TBH until you have a better understanding, a chat client maybe beyond your scope as you have allot of things to consider, like AJAX, you will need to be able to use a transport system that doesn't rely on the page refreshing and security of the server and a system of hashing that identifies a user and their associated IP address to stop spoofing and server injection.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  7. #7
    Join Date
    Feb 2016
    Posts
    30
    No it's a simple 2 user chat box which is protected. It's not going public I'm just wanting to take the timestamp from mysql and add it next to the inputted text. My browser does not refresh the messages are live and instant.

  8. #8
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,836
    One more thing. Don't use plain generic names for db column names. Make the name "Meaningful". To use "time" as a column name is not only perhaps problematic (since it could be a reserved word) but it is not very useful when looking at the structure of the database. Time of what? What Time is it? What does it represent?

    Use something like "start_time" or "end_time" or "add_time" or something that gives you a reference point as to the meaning of the time value you are saving.

    Same when setting up a contact table and trying to just use 'name' instead of 'first_name' & 'last_name' as a person's name value. Or address instead of 'street_1' & 'street_2'.

    These are just some suggestions as to how to name things. Use whatever makes sense to you, but do use something that adds context to the name.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  9. #9
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,429
    Unless your using HTTPS server, anything sent between computers is plain text.

    They only way that two computers can communicate is if one has the server or you use a remote server and the users have a chat interface with a log in.

    I have to say that from the very little you have said, it sounds like you have a number of major security concerns, especially where you are using PHP and MySQL services, without proper implementation and sanitizing routines, you're open to having the database compromised or destroyed and similar for the web server. Give a script kiddie a nanometre and they will park a trident class right up there.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  10. #10
    Join Date
    Feb 2016
    Posts
    30
    Okay so.

    Its not going out to anyone other than me using it for trials purposes and testing php.
    1. Im hosting on a developers hosting service such as (Codepen)
    2. There is a chat interface with a login
    3. there are no major security concerns what so ever (It's a private chat/project)
    4. If the database is compromised - Thats fine its a developers site which is used to test and trial code before actual use.
    5. Teach a mod about a trident class and a script kiddie will park it where he likes. :-)

  11. #11
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,429
    Really don't see the point of codepen, I tried looking up about it and I don't see how you have that level of access to the server without it being a security issue.

    According to codepen, its a front end development tool and you are trying to create backend server access stuff...

    CodePen is a social development environment for front-end designers and developers.
    I don't see how you have backend support.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  12. #12
    Join Date
    Feb 2016
    Posts
    30
    Im not using CodePen - I used it as a reference, I have frontend and backend access through my developers hosting site. Im playing about with various PHP/Java/HTML etc (Not limited) <<-- Used as reference.


    Take it with a pinch of salt 'trident'.. ;-)

  13. #13
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,429
    So what is it that you are having issues with? Its still not clear, you post some JQuery (which isn't JavaScript) in a PHP forum.

    What is it that isn't working, if its the code you posted, then this question belongs in the JavaScript Frameworks forum, if its something in the PHP code, you need to supply the PHP code you're having issues with.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  14. #14
    Join Date
    Feb 2016
    Posts
    30
    It's okay, I'll mark as solved. We've established already that code is in the wrong section and I've pretty much outlined what I was looking for. A timestamp to display next to submitted text in a chat window.

  15. #15
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,429
    Which all well and good BUT you never posted code for the HTML side of the chat window, the PHP backend and all these elements relate to the script you use to run the chat client.

    You might not think it would help but it does, people could give you examples all day long and still not resolve the problem which is why people post code in the forum (and not via those code sites because that code can change).
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center