Jump to content

Recommended Posts

Posted

Hello,

I want to use the $session variable in functions, inside a template. According to the API this variable is available only in templates.

But all values of the $session variable get lost, even when I use 'global $session'.

is there any workaround for this problem?

 

Posted
1 hour ago, adrian said:

Like any other ProcessWire variable, the easiest way to access them inside a function is: 


wire('session')

 

In the function I wrote 

wire('session')->set('number', '12345');

and outside the function I tried :

echo $session->number

...but this variable was empty....

Posted

Try to use:

$session->get('number');
or
wire('session')->get('number');

// You can make a test

function setMySession($key = '', $value = '') {
    wire('session')->set($key, $value);
}
setMySession('foo', 'bar');
echo $session->get('foo');

Here is doc page about sessions: https://processwire.com/api/ref/session/

Posted
14 hours ago, adrian said:

@helmut2509 - it's working fine here. Are you actually calling the function where the "number" session value is set before you try echoing it?

5a00a90d7933e_ScreenShot2017-11-06at10_24_43AM.png.454af9ab3d02be635fbe48bec052ad9c.png

you are right, I have made a stupid error.

thanx, guys!

 

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...