Numeric values in php session variable

1

can the session variables in php only adopt string values or can I pass numeric values to this type of variable? I have a form with the login fields, password and a select option with a numeric value. can I pass the value of this select to a session variable?

    
asked by anonymous 29.06.2017 / 16:33

1 answer

2

In session you can pass almost everything, that is you can add:

session_start();

$_SESSION['int'] = 1;
$_SESSION['float'] = 0.1;
$_SESSION['string'] = 'str';
$_SESSION['object'] = new FooBar;

Of course in the case of a class only the values will be saved in the session, when trying to access the next page, however if the class exists it will be possible to use it as if it had instantiated.

The only that can not be added is Closure , if you do this:

session_start();

$_SESSION['closure'] = function () {};

Or this:

session_start();

$foo = array(
    'foo' => 1,
    'baz' => 'str',
    'bar' => function () {}
);

$_SESSION['closure'] = $foo;

It will cause an error like:

  

Fatal error: Uncaught exception 'Exception' with message 'Serialization of' Closure 'is not allowed' in [no active file]: 0 Stack trace: # 0 {main} thrown in [no active file] on line 0

About the form

  

I have a form with login, password, and a select option with a numeric value. can I pass the value of this select to a session variable?

Data passed via HTTP will always be "string" when received in PHP; if you want to convert them you can use things like:

  • $x = intval($_POST['...']) or $x = (int) $_POST['...']; ,
  • $x = (float) $_POST['...'];

And then write to the session

    
29.06.2017 / 16:36