Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Brian McCall
Brian McCall
3,281 Points

Creating Flashcard site. How to manage and store 'guest' flashcards so they can share them?

Well I am working on a little flashcards side project and I want anon guests to be able to come to the site and create a set of flashcards then be able to send someone else a link so they can also look at that person's set. Right now since I am the only one using it all of the cards are stored in the DB. Each card gets stored with a Unique A.I. number, set name, question and answer. My idea is to add a new column to the DB called user and if the person is logged in their username will go there and if no one is logged in it would generate a random key (i.e user125135) and store that number first in a PHP session so when adding cards it makes sure that all the cards that user submits will have the same userID attached, then in the user column. Then when they click on 'Learn' it will query the DB and grab all sets with that particular username. How should the links be created so a person can share them. would I have to store the unique link in the DB also so if the link is shared my query will know which sets to pull up? Thanks in adv!

I am using PHP, mysql and javascript/jquery

5 Answers

Andrew Shook
Andrew Shook
31,709 Points

A problem I see is storing anon users as users in the db and giving them a unique user id. That could lead to potential problems if you get spammed because you start eating up potential user names in the future ( or you have to go into the db and start deleting stuff). What I would do is check to is if the user is logged in using $_Session and If they are not then give all anon users the same user id. This way your user ids don't get eaten up. However, even that presents a problem since anon users will either be able to edit all anon content or none at all since they all share the same user id. So, if there is a typo, or something, it can't be fixed. Personally I wouldn't let people make flash cards unless they were logged in as a user.

Brian McCall
Brian McCall
3,281 Points

Thanks for the ideas. But that would really limit the ability for someone to share their set with a non registered user.

Unless you could get someone else's sets if you had the link. (kind of like google drive does). But then I have no idea how to implement that.

Andrew Shook
Andrew Shook
31,709 Points

If you make all the content on the site publicly visible then all they would have to do is share the link and people could see it. Then to edit you just make sure that the author/creator user id is equal to the user logged in. What I'm picturing here is like a CodePen for flash cards, is that about right?

add another column...something like public_hash which has a unique, yet small and readable hash like youtube's and use urlrewrite in apache to make it snazzy..eg. www.myflash.com/bn12mnd3 ...this would be a link...if you are not familiar with url_rewrite, this translates to something like www.myflash.com/?flash=bn12mnd3....check out this id generateor..it's readable and nice: http://www.hashids.org/php/

Andrew Shook
Andrew Shook
31,709 Points

but how would you unsure anon users content can only be edited by them and no one else?

Brian McCall
Brian McCall
3,281 Points

I think this is kind of what I was thinking. That hashid would also get stored into the guests $_session information, this would restrict editing to a person that has that hashid in their $_session.

Andrew Shook
Andrew Shook
31,709 Points

Problem with putting it in the session is that if they clear their browser cache then they no longer have access to edit their content.

Brian McCall
Brian McCall
3,281 Points

Thanks for the ideas.

oh...you want editing capabilities...did not know. There are two solutions: Number 1. assume they have a static ip address i.e. the ip address does not change, and hence, bind the ip address to their cards. 2. assume they won't clean their cookies, and store a cookie with a uid with a very long expiry time. I can't think of anything else btw....or there's another way, a more legitimate one: when they publish a set of cards..put up a splash message, or a modalbox as we call it. output a non-public url which allows the visitor to edit the particular set of cards... for eg. I generate 10 cards. I get two URLs. Number 1. a public url (www.ac.com/?v=q12e)..this is to view the cards...its for all people. it can be shared. Another URL will be the secret url (www.ac.com/?edit=rtgf8r7etg896ds97gf6ds76gdfg). this url will allow the person to edit the flashcards. ask him to open it in a new window and bookmark it, or something...or maybe ask him to enter his email so that you can mail him the link.

Andrew Shook
Andrew Shook
31,709 Points

Those are the only to way's that I can think of and they would both likely fail. Non-public url would be the best but that is likely to fail as well.

Andrew, whether you've known it or not, but most anonymous websites have used one of these three methods. There is no other. You can add facial recognition to your website, but it's not much accurate.

Andrew Shook
Andrew Shook
31,709 Points

I'm not saying your wrong, your solutions are spot on, The point I'm to make is that this might not be the best uses case for anonymous user website. Simplest solution is have them register as a user. Tha'st why Facebook, Twitter, Google Drives, Dropbox, Instagram, Flickr, CodePen ... etc do it.