www.webdeveloper.com
Results 1 to 7 of 7

Thread: cookies wth php and javascript

Hybrid View

  1. #1
    Join Date
    Jul 2006
    Posts
    373

    cookies wth php and javascript

    Hi,

    i want to create a cookie using javascript but also being able to read it with php. Is it possible? for example:

    Code:
    In javascript:
    
    create cookie called session
    
    In php:
    
    if(session cookie exists) { echo "whatever"; }
    Thank you.

  2. #2
    Join Date
    Jul 2006
    Posts
    373
    Forget that question, i just found out it is indeed possible and how to do it. However now i need to make the cookie get cleared as the user leaves the page where it was created. Can you give me a hint?

    Thank you.

  3. #3
    Join Date
    Jul 2006
    Posts
    373
    Okay, i figured out the answer to my last question too. Now i have a new problem. I used the codes below and they work fine in firefox and chrome, but as usual not in Explorer (its a piece of ****, you know)

    Code:
    <script>document.cookie ='chat=sesion; path=index.php';</script>
    <script language="javascript">
    function clearCookie(){
     document.cookie='chat=;expires=Thu, 01-Jan-70 00:00:01 GMT;';
    }
    window.onunload = clearCookie;
    </script>
    I would appreciate a cross-browser solution.

    Thanks.

  4. #4
    Join Date
    Oct 2012
    Posts
    3
    I'm not totally sure but perhaps IE requires some sort of required format? For testing purposes, use this code to set a cookie (taken from w3schools' website),
    Code:
    function setCookie(c_name,value,exdays) {
        var exdate=new Date();
        exdate.setDate(exdate.getDate() + exdays);
        var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
        document.cookie=c_name + "=" + c_value;
    }

  5. #5
    Join Date
    Jul 2006
    Posts
    373
    Thank you, i had already fixed. You have to use path=/ instead of path=index.php.

  6. #6
    Join Date
    Jul 2006
    Posts
    373
    Sorry again but for some reason tis code is not working anymore:

    <script language="javascript">
    function clearCookie(){
    document.cookie ='chat=; path=/';
    }
    window.onunload = clearCookie;
    </script>
    its supposed to clear the cookie named chat when closing the window but its not clearing it. Could you show me an alternative?

    Thank you.

  7. #7
    Join Date
    Jul 2006
    Posts
    373
    Sorry, i think i should have specified that i need the cookie to be deleted as either the TAB or the window is closed.

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



Recent Articles