开发者

Session questions

I am having trouble in understanding of how I need to do this:

  • Have list of items on my home page and when users click on that, I take them to a page where i ask for name and email id(both are optional fields) and when users hit submit, I will take them to other page where they get to see all the details. the reason for that name and emails fields are t开发者_如何学编程o store the ip address in mysql and also the url(has the item id and date) they are on.

To do this, i am having trouble in doing it in program.

  1. I am thinking to start a session/store cookie once they hit submit. after that I want to store the ip address, item id, date and name/email(if they filled in) in mysql db
  2. Store everything in the mysql db

Also, how can I avoid anyone going to the page directly(the one I show after they submit) ? Is there any way can I include some condition on that page and redirect them to this log in page ?

Please help me out.

regards


Since you set session variables when the user hits the submit buttons, you can test if one of those variables is set or not and redirect accordingly.

You can also do it with POST, use the page as an action to your form, and whenever someone accesses that page you test if $_POST variables (from the form) are set or not.


As the data seem to be necessary only for the immediate use, I think that a session is the right answer in this case.
If you would then use a database query, which data would you store to associate the data to the correct user? As you said, both the data you ask are optional; even in the case there would not be optional, how do you handle the case two different users report the same name and email (it could also be the same user using two different browsers).

For temporary data like that, the session is always the better choice (except few exceptions, maybe).

I was forgetting the other question.
Also in that case, I would use a session variable. Sessions variables are the solution for values that you want to keep between different forms, without the need to keep moving them between the server, and the client side.

For more information about sessions, see PHP: Sessions

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜