The session is customer-based, not user-based and since the guest and logged in user are different as far as Magento knows it's not possible.
The easiest way would be to use a cookie. This is stored on users' device and will be available both when the user is logged on and off.
$cookie = Mage::getSingleton('core/cookie');
$cookie->set('test', 'cos tam' ,time()+3600,'/');
$cookie->set('test123', 'cos tam12345' ,time()+3600,'/');
3600 is the number of seconds the cookie will be stored from the time it's set. In this case it's an hour but you can always increase it.
If, however, you're storing sensitive data don't put it in a cookie. Create a database table with the fields
- ID
- Key
- IP
- Value
generate a random key for new visitors, store that in a cookie on their device and insert it in the table together with the value and users IP address. Now you can retrieve the data by checking for the cookie and its value and using the IP to ensure there's no session hijacking.
The script would look something like this
https://pastebin.com/5dieE17Z