Level 25
Try this
$variationId = session()->get('variationId');
if (!session()->has('variationId')) {
session()->put('variationId', rand([0,1]));
}
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
$variationId = session('variationId');
if (!$variationId) {
session(['variationId', rand[0,1]);
}
I'm setting up some server side A/B testing with google optimise. So need to set a session variation value if it doesn't exist, or retrieve it when it does.
The above doesn't work though. It keeps randomising it every time I refresh the page.
I can't workout from the docs whether using the session helper is the same syntax as using $request->session and then adding has or pull or get etc?
Try this
$variationId = session()->get('variationId');
if (!session()->has('variationId')) {
session()->put('variationId', rand([0,1]));
}
Please or to participate in this conversation.