#SESSION_SET

Used to insert additional data into the #SESSION tag.

  • New in : SPIP 2.0

#SESSION_SET is employed by writing #SESSION_SET{variable, value}, inserting the value into the PHP array in the form of $GLOBALS['visiteur_session']['variable'] = 'value';

The inserted data can be retrieved at any time using #SESSION{variable}.

So this tag can be used to add any data you wish to the session that is opened when a visitor accesses a SPIP site. It can be used unconditionally to store elements that you would like to have access to throughout the open session, without having to rerun a loop or any other specific procedure.

The syntax

The basic syntax is very simple:
#SESSION_SET{variable,value}
The ’variable’ element is any name that will enable you to reference the data later on, and ’value’ is its corresponding content. But a simple example should be able to cut a long story short here.

For example, suppose that I’m a great fan of elephants, and I would like to broadcast that riveting piece of information at any time I wish ... So:
#SESSION_SET{mypassion,elephants} will assign ’elephants’ as ’my passion’.

Accessing this later is also very simple: #SESSION{mypassion} will display ’elephants’.

Practical application

Perhaps the example above isn’t really solid enough for you. Usage of the #SESSION_SET{variable,value} tag is particularly relevant when access to a site is made using identification (of the ’login public’ type) in order to add certain data that is linked in one way or another to the person currently logged in.

If you display the #SESSION tag on a page, you will see that it contains a large volume of data, notably related to the identified author or visitor. The display of any one of these data is done directly using #SESSION, unless you have added some extra fields.

For #SESSION{extra}, you will be presented with the display in text format of the contents of that field, structured as below as an example:

a:2{s:10:"id_salary";s;1:"1";s:7:"college";s:5:"Cadre";}

Of course, this allows you to check that what you are looking for does actually exist amongst the stored session variables, even if the display leads something to be desired... [1]

What can we do to improve this? Apply #SESSION_SET, of course, but with the #EXTRA tag!!

I would like to display the college that the user attended:

In an AUTEURS loop, I’m going to add the college that the identified person attended to my session:

<BOUCLE_auteur(AUTEURS){id_auteur=#SESSION{id_auteur}}{tout}>#SESSION_SET{college,(#EXTRA|college)}
</BOUCLE_auteur>

And there you have it ! I can now display the college on any page within the website simply by writing:

[(#SESSION{college})]

This can be used by just itself or in a loop, either to display the college name itself or as a loop criteria, e.g.

<BOUCLE_articles(ARTICLES) {id_rubrique}{titre_mot = #SESSION{college}} {par num titre, date} {doublons} {pagination 20}>

What else could we do?

Remember that SPIP 2.0.x now allows us to open connections to databases that are external to SPIP, even on remote servers ... What could be simpler, then, than to add one or more pieces of data from such databases to the current open session ... One (or several) loops upon opening the session, a handful of #SESSION_SET{variable,value} calls, and I quickly have at my disposal all the elements I need to tailor and customise my site with the data I have available to me ...

Footnotes

[1reminder: to display the detailed contents of the tag in raw mode (for verification), you can use the script:
[<pre>(#SESSION*|unserialize|var_export{1})</pre>]

Author Mark Published : Updated : 29/06/23

Translations : català, English, français, Nederlands