简体   繁体   中英

Yii Save Form Before Login

I have a page where a user can post a comment. To increase user engagement, I want to let them type their comment even if they are not logged/registered and only log them in once they submit.

What is the best / standard way to achieve this?

Currently, when accessing a logged-in only page, I am using Yii's loginRequired method which Redirects the user browser to the login page. Before the redirection, the current URL (if it's not an AJAX url) will be kept in {@link returnUrl} so that the user browser may be redirected back to the current page after successful login. Redirects the user browser to the login page. Before the redirection, the current URL (if it's not an AJAX url) will be kept in {@link returnUrl} so that the user browser may be redirected back to the current page after successful login.

This however only saves the URL, not the POST data.

On a side note, I am using Yii-Rights.

I've not heard of a built in way to do this. But when you click the link I'm assuming your hitting some sort of comment saving URL then this is redirecting to the login page?

Your going to want to actually hit the action function first, so it has to allow guest access into the function then do the redirect manually, rather than letting Yii do it with access rules and such.

Once in your own function you'll want to tie that data to that visitor some how, the usual way would be in $_SESSION but that will get regenerated when they login. Your other option is cookie data.

You could set the comment data in one or two cookies, redirect to the login page with a return URL of your cookie save function, perhaps even with a flag to tell it to check in COOKIE rather than POST. And continue from there.

Hope that helps, or have I missed the point entirely?

Update
OK after a test, it appears SESSION data persist over session ID regeneration. So you can simply store in the session.

I like Ian Atkins method, but it brings with it concerns of spam bots, which you'd have to combat with sort of Captcha or other method. Whereas if you store it in the session nothing actually gets put in the database until a user has logged in or registered.

It's up to you really, depends on how big your site will be (attractive to spam bots) and how much flexibility you need in showing multiple comments before authenticating a user.

Create a GUID in the $_SESSION for every visitor that isn't logged in. Use the GUID as a temporary user ID to save the comment. I would probably do the saving via AJAX, and in the callback send them to the login/registration page. Use the GUID in the $_SESSION to convert comments made before logging/registering into fully qualified comments with their new/existing user ID.

On the display side, you could easily keep comments side by side from both users with ID's and users who never register. It makes your 'users' and 'comments' tables a little more complex, but I think it's perfectly doable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM