Home > Back-end >  Sessions not working on CDN but works on localhost
Sessions not working on CDN but works on localhost

Time:06-20

Code works well on localhost, but when i deployed it on hosting that is using CDN, the PHP session is not working.

On page1, I have form that is using token

<form method='post' action='page2'>
   <input type='hidden' name='token' value='someRandomToken'>
</form>

On page2, it will validate the token

if($_SESSION['token'] == $_POST['token'])
{
    do something
}

It looks like the $_SESSION['token'] is blank

EDIT

I guess the problem is not because of CDN and hosting, I guess it's because of PHP version? I am using PHP Version 7.2.34 and still can't make SESSION and COOKIES work

CodePudding user response:

The error is because of placement of $_SESSION["variable"]

It must be set before you print / echo anything.

I thought only the session_start() must be place above any prints.

<?php

//do not print / echo anything above 

session_start();
$_SESSION["variable"] = "some value";
  • Related