Setting $_SESSION into a $_COOKIE

Hi, I want to set a $_SESSION with a cookie so that the session can last longer so I do not get undefined index notices in a short space of time. Is the code below correct or do I need to modify something?

$inTwoMonths = 60 * 60 * 24 * 60 + time();
        setcookie('id', 'ID', $inTwoMonths);


        //line 20-23
        if (isset($_POST['id'])) {

        $_SESSION['id'] = $_POST['id'];

        }

        //line 30
        <?php echo $_SESSION['id'] ?>

A session should live until the browser is closed. If you are loosing your session variables during a session, maybe there’s something wrong? Do you do session_start() at the top of each script?

Yes there is a session_start() in each script

You can use a cache system or a key value database such as redis with predis etc… there is many solutions to this problem.