I’m working on a website that uses over 200 variables that need to be sent from page to page. Each variable holds a small amount of data. The largest is probably a text string of around 200 characters. I use WordPress for part of the site but not all of it, so the variables need to be sent to WordPress at some point. I already have it working in PHP but I use a MySql database to store the variables. The variables change often, so there are queries happening often. Apparently this would not work well if the site had many users.
So my question is, what would be the best way to store and transfer over 200 variables from page to page on a website without it being too taxing on a server? I’m using PHP but could possibly use JavaScript.
I’m assuming JavaScript localStorage would be the best option, since the variables would be stored locally, but it would be hard for me to convert my site to pure JavaScript. I tried it yesterday and got quite far but some aspects were tricky for me, since I am only a hobby coder. I could do it if it was essential but I’d rather use PHP if I could. It would set me back a while.
Other options I’m aware of are SESSION variables and POST variables. Out of the two I’m assuming POST variables would be better. Would over 200 POST variables be taxing on the server if there were many users online?
Another option would be cookies, but I think I’d have to store lots of my variables in a few cookies not to exceed the cookie limit. Would this be feasible?
Any help would be greatly appreciated! Thanks!
>@simon_matthews#1649106 Other options I'm aware of are SESSION variables
>@simon_matthews#1649110 I believe sessionStorage is similar to localStorage?
>@ginerjm#1649114 I have no idea what sessionStorage or LocalStorage is.
>@ginerjm#1649117 And this 'browser storage' is available to JS,
let stored = localStorage.data
> perhaps a newer version of JS than I am aware of?
> And works with Chrome?
>@simon_matthews#1649119 Session variables was my first choice, since they are so easy to use, but I'm worried that over 200 variables could cause a performance issue.
>@simon_matthews#1649138 So it looks like my best option would be to store my variables in a single JSON database column server side and then move them to a single JSON sessionStorage client side. My first job then should be to convert all my database columns into a single JSON column.
``<i>
</i>const response = await fetch('https://api3.go4webdev.org/tsk/all')
const data = await response.json()<i>
</i>
``
> Is there a way to use sessionStorage to store the variables and still use PHP to build the page? In other words, can the variables be taken from sessionStorage and sent to PHP to build the page, or will the page have to be built in JavaScript?
>@simon_matthews#1649209 Started converting my site to work with the JSON variables. How do I insert values into a JSON MySql row using a prepared statement?
``<i>
</i>INSERT INTO tsk (tsk_subject,tsk_desc)
SELECT tsk_subject,tsk_desc
FROM json_populate_record (NULL::tsk, $1)
RETURNING tsk_id<i>
</i>
``
> Thanks for replying! The site allows users to customize photo greetings cards, much like moonpig. The variables store the positions and sizes of photos and text along with the content of the text. These change as the user customizes the card. All the user will see is the card template, so it's all going on in the background.
0.1.9 — BETA 5.18