tags:

views:

37

answers:

2
+1  Q: 

Session questions

Hi,

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 to 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

+1  A: 

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.

Soufiane Hassou
i am not sure how to start the session and how can I verify whether its working or not. should i start the session on the page where i show the log in page or in the page after they hit the submit button?
Jay
@Jay: Simply use `session_start()`. If you want to verify if the session has been already started (to avoid to start it again), then check the value returned by `session_id()`; the returned value will be an empty string, if the session has not been started, yet.
kiamlaluno
+1  A: 

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

kiamlaluno
I am still working on this. will post my feedback once I am done. thanks again!
Jay