php icon indicating copy to clipboard operation
php copied to clipboard

How to enable php session

Open areaboy opened this issue 2 years ago • 7 comments

Here is my vercel.json file


{
  "functions": {
    "api/*.php": {
      "runtime": "[email protected]"
    }
  },
  "routes": [
    { "src": "/api/(.*)",  "dest": "/index.php" }
  ]
}

here is my session script api/session1.php

<?php
  
session_start();
$_SESSION['user'] = 'Ann Cooler';
  
?>

api/session2.php

<?php
  
session_start();
echo $_SESSION['user'];
  
?>

The session is not working at all

areaboy avatar Jun 30 '23 11:06 areaboy

Hi @areaboy. It's a feature, when you request your different lambdas they have different user space. You can imagine them like different docker containers. You need to use some shared space, for example redis, if you need multiple lambdas. Or you can use one lambda (one index.php) and require different session.php file according routes.

f3l1x avatar Jul 02 '23 12:07 f3l1x

Thanks for getting back to me. Please do you have a php sample code and vercel.json sample configuration. Am New to Vercel. Thanks

areaboy avatar Jul 02 '23 13:07 areaboy

I have encouter this issue before but I end up using databse as session driver. just need to provide your db credential and SESSION_DRIVER=database is fine

denusklo avatar Jul 14 '23 09:07 denusklo

How would I go about doing that? I'm very new to php and Vercel so I don't fully understand where to write that. Thank you

Transyfor avatar Nov 22 '23 16:11 Transyfor

@Transyfor the place for me to put db credential and SESSION_DRIVER=database is file .env at the root directory

denusklo avatar Nov 22 '23 18:11 denusklo

same problem

urbanwatch avatar Jul 08 '24 22:07 urbanwatch

suggest
'driver' => env('SESSION_DRIVER', 'file'), replace 'driver' => env('SESSION_DRIVER', 'cookie'),

484869326 avatar Aug 27 '24 09:08 484869326