Q . How to create session cookie in JavaScript and PHP with session expiry time.

Brad Johnson. asked, Nov 22 ' 2016

I am creating cookie with js and it sets the expiry time. I want it should have session expiry time.

<script>
    function setcookies(name, value)
    {
        var now = new Date();
        var time = now.getTime();
        var expireTime = time + 1000 * 36000;
        now.setTime(expireTime);
        document.cookie = name + "=" + value + ";expires=" + now.toGMTString() + ";path=/";
    }

    setcookies('js_cookie', 'js_value');
</script>

 

  • PHP
  • cookie

1 Answers

Dont put expiry time if you want to create session cookie in both php and javascript.

JAVASCRIPT

<script>
    function setcookies(name, value){
        document.cookie = name + "=" + value + ";path=/";
    }
    setcookies('js_cookie', 'js_value');


    //OR
    function setcookies1(name, value){
        document.cookie = name + "=" + value + ";expires=1;path=/";
    }
    setcookies('js_cookie1', 'js_value1');
</script>

PHP

<?php
setcookie('php_cookie', 'php_value');

 

Leave a Ansewer