I'd like to store data per user without any form of login system.
The user clicks an item and this item is attributed towards them (like a shopping cart, but these aren't products and nothing is sold). So the user can browse different items then basically add them to cart, effectively. I want it to work like a traditional session would, only not using a session. I read that this is discouraged in Wordpress.
What's the best way to achieve something like this in Wordpress then? Is the Transients API appropriate for this task? Or some other means?
The transients API is of no benefit to you for this particular purpose. They'd have to be specific to each user and soon enough your options table would be huge.
You need to set a cookie instead.
You can use JS (or better, jQuery) to call PHP script using ajax/post/get internal jQuery functions and then set a cookie using PHP.
Related
Based on user input value, I am calculating product price and price breakdown dynamically and showing the price to the user.
How can I pass an entire collection and breakdown to the next page (like a checkout page).
I can JSON encode whole collection and kept inside a form and passing by post request. In this case user might manipulate using browser developer tool.
What could be alternative way to pass big data to another route (checkout route) in laravel. I am logically stuck.
You can achieve this by using session or local storage also
Solution Explanation:
Session Storage: store the data in session with user id and get it in next page and if you work is done then clearing the session.
Store the data in local storage and retrieve in next page then clear the storage.
It is not a good practice to pass entire collection between the pages.
Let's say, you have a person stored with id as 1 in your database. Person has lot of other detail also stored like name, email, job etc etc
If passing data is concerned, you first fetch data of that person from database, pass to the next page and then via post get the same data back to you.
Whereas, just the id having value 1 is more than enough to identify that it is that person and you can get those detail back again.
Considering e-commerce and checkout page, when items are added in cart, it's a cart quote. The items are not checked out yet. So you can create a cart_quotes table to store that data and then just pass the id of quotation to the next page instead of entire details like products added to cart, discounts etc etc.
The next question comes : id can also be manipulated from the url? The answer is, laravel has signed urls using which you can avoid url tampering.
Last but something I would recommend, if you are building an e-commerce in laravel, there are already packages available which does everything for you. Check :
- [vanilo](https://vanilo.io/)
- [aimeos](https://aimeos.org/laravel-ecommerce-package/)
- [bagisto](https://bagisto.com/en/)
I'm looking for a way to create a page with items on it where each item has a button which can be clicked to "like" or "choose" the item. The website would then remember the choices and input them to a form on another page. Is there anyway of doing this?
At first glance if we are talking about a registered user, i would create fields in the database for the user's input and store its value everytime he makes a selection. Then its only a matter of putting those values wherever you want.
You have several choices. If it is persistent data, I'd suggest a database like MySQL. If you don't have access to that, then Google for "flat file database" and you'll find some great ideas.
If the data is persistent, but not a big deal if it gets deleted or spoofed, you can use cookies.
If the data is fleeting (it goes away at the end of the visit), then use sessions.
The best way to do is storing each result in a SESSION and reusing it in other form.
Scenario No 1
On page1.php I use php with MySQL to get a product's data to the page. When a user wants to proceed to page, 2 he clicks on a button.
On page2.php there is also some info about the product, mainly for checkout purposes. For this data to be displayed I make use of the ID of the product so page2.php is actually page2.php?id=123. Therefore, using again PHP and MySQL I get the data I need.
Scenario No 2
On page1.php I use php with MySQL to get a product's data to the page. The needed values for page2.php are stored in sessions. When a user wants to proceed to page 2, he clicks on a button.
On page2.php now, the information about the product is shown using sessions. This time page2.php is actually page2.php that it is shown on the address bar.
I prefer doing this with scenario 1. The user will be able to copy/paste or send through a button the page to a friend for direct access. I don't think that one more hit to the db is a problem.
What is your opinion ?
I would agree that scenario 1 is better. You should use a session for its intended purpose, carrying state of a particular user, not misuse it as some form of caching mechanims in my opinion.
That's why you use the database in the first place - to have efficient access to your data. Why try to build a cache around the thing that should actually improve the efficiency if used?
If access ever becomes a bottleneck and you really need to look into caching, then there are still better mechanisms than to use your session for this purpose.
No. 1 is definitely the better approach. The second approach would only make sense if the product is attached to the user's session (for instance, when going through an order wizard). And even then, it's still better to just attach the product ID to the session, and fetch the product information from the database on each new page.
A customer wants a button that says "Relate" that would be kind of like the Like button in Facebook. It would show how many people clicked the button inside an article. Is there an easier way to do this besides creating a table in MySQL and use PHP?
I suppose you could use a CGI script written in bash that read/writes a plain text file that stores the click count. But one way or another you're going to have to have some kind of server-side handling and data storage. Doesn't have to be PHP/MySQL, but you'll have to use SOMETHING.
If you want to use data gathered across multiple sessions by multiple people, you're going to need to store that data. Whether you store it using databases, textfiles, or carve it in a tree is up to you.
Yep, instead of creating a new table, just add a column likes INTEGER(11) to articles table. Whenever someone clicks the "relate" button just add count by one.
You may also want to disable button after user has clicked it. You may want to store the info liked:article_id in cookie to make sure user do not adds fake "relates", unless he clears cookie. But tracking logged in user is different (but more appropriate) game.
Working on PHP application, that uses DataTables (https://datatables.net) on several layouts.
Can I somehow reset all DataTable's filters and search data, after end user logged out from application? In other words, to clear all cookies that DataTables library created, if it's possible with PHP functions..
Main idea is to reset application to it's main state after user is logged out from application.
Thank you in advance!
The documentation says: (https://datatables.net/examples/basic_init/state_save.html)
The built in state saving method uses the HTML5 localStorage and
sessionStorage APIs for efficient storage of the data.
It means you cannot reach it with PHP.
However you can change the storage of filter parameters to cookie or server side.
Alternative options of using cookies or saving the state on the server
hrough Ajax can be used through the stateSaveCallback and
stateLoadCallback options.
You should write code which:
- save the filter parameters to cookie what you can remove on logout with PHP
or
- save the filter parameters to database via AJAX and you also can remove it on logout with PHP
example is here:
https://datatables.net/reference/option/stateSaveCallback
In case anyone out there is interested in a solution to this:
As some people rightly pointed out, there's no way to delete localStorage from PHP because PHP works only on the server and localStorage is client-based.
However, a way to achieve what OP wants is make sure that the page shown after logout has the JS needed to delete localStorage. For example, say that after logout, the user gets redirected to comebacksoon.php; what you need is to make sure that in the html of this comebacksoon page, you include:
<script>
localStorage.removeItem(keyYouWantToDelete);
</script>
It should be noted that if the user closes the window before comebacksoon.php is loaded, localStorage won't be deleted, but this approach has worked for me in most cases.