vercel-community / php

🐘 PHP Runtime for ▲ Vercel Serverless Functions (support 7.4-8.3)
https://php.vercel.app
MIT License
1.24k stars 285 forks source link

How to enable php session #468

Open areaboy opened 1 year ago

areaboy commented 1 year ago

Here is my vercel.json file


{
  "functions": {
    "api/*.php": {
      "runtime": "vercel-php@0.6.0"
    }
  },
  "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

f3l1x commented 1 year ago

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.

areaboy commented 1 year ago

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

denusklo commented 1 year ago

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

Transyfor commented 9 months ago

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

denusklo commented 9 months ago

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

urbanwatch commented 2 months ago

same problem

484869326 commented 2 weeks ago

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