PDA

View Full Version : (temporarily) save data to cookie or session


ub.ch
11-04-2015, 12:34 PM
Heya!

I built a plugin with a search in it, which I want to paginate.
The easiest way in my mind was to save the search term(s) either into a session or a cookie and then, when page two is requested, do the search query again.

Now I can't seem to be able to access either cookies or session.
Tried using $_SESSION or $_COOKIE or vbsetsession() but to little avail it did not work :(

Any recommendations on how to properly do this?

Any help is really appreciated :)

ps: with "not working" I mean, that I can write to either, but after reloading the page, the information is no longer saved

Dave
11-04-2015, 12:56 PM
A better way is to keep the search term in the URL (a $_GET parameter). Have you considered that?

ub.ch
11-04-2015, 02:05 PM
Yep I have, but it is multiple keywords / checkboxes and I don't want to "clutter up" the URL that way

Dave
11-04-2015, 02:20 PM
You could serialize those variables and put them in the URL in 1 parameter.
You could also base64_encode it.

ub.ch
11-04-2015, 02:42 PM
yep I know I could do this, but don't want to ;)
That would be the last resort; second to last would be to resend the form on every page change - but still, the question is: how can I use vbulletin sessions or cookies

Dave
11-04-2015, 02:57 PM
Alright well in order to set a session variable, you can use $vbulletin->session->set('key', 'value');
For cookies you want to use vbsetcookie('key', 'value');

ub.ch
11-04-2015, 09:13 PM
Thanks Dave!
How would I read them? Just using $_COOKIE / $_SESSION?

Dave
11-05-2015, 06:32 AM
I believe you can use $vbulletin->session->vars['key'] to access the session variable.
You can just use $_COOKIE['key'] to access the cookie value.

cellarius
11-05-2015, 10:27 AM
This is a vB page? Then do it the proper vB way. https://vborg.vbsupport.ru/showthread.php?t=221670

ub.ch
11-06-2015, 11:07 AM
thanks for your help!!

I believe you can use $vbulletin->session->vars['key'] to access the session variable.
You can just use $_COOKIE['key'] to access the cookie value.

mhh can't get both of them to work :(
I can save to the session, but refreshing the page yields no data.
Cookies are not working at all :/

This is a vB page? Then do it the proper vB way. https://vborg.vbsupport.ru/showthread.php?t=221670

I will have a look into it, still does not solve the problem I actually have.
I _don't_ want to have $_GET params; the page you linked to has actually no keywords or params to be searching for. Or am I missing something?

If I get it to work, I would still like to know how to save data to the session / cookie ;)

cellarius
11-07-2015, 03:41 AM
You can add as many get-parameters as you need to this method. See the Constructing pagenav section.

Anyway, that's not what you want, then. Although I don't quite understand why you would want to hide away the search parameters. I find that highly annoying as a user, since search results can't be linked to (like in vb with it's cached searches). In my opinion, there's good cause why for example Google does not do that.