I am learning to set up a cookie and assign a user_Id to whoever visits my website. I want to keep a record of all user IDs in mySQL database. I tried something like this:

if(!isset($HTTP_COOKIE_VARS['user_id'])) 
 {   $cart_id = md5(uniqid(rand()));
     $COOKIE=setcookie("cart_id", $user_id, time() + 14400);
     if (!$COOKIE)
     {
     echo "To complete your order your browser must be set to accept cookies";     
     }     
 } 
 else 
 {     
 $user_id = $HTTP_COOKIE_VARS['user_id']; 
 }

I believe the results I got did not seem to be convincing because verytime I refreshed my browser, the value of the user_id changed. Then I tried this

session_start();
$user_id=session_id();

which I believe also did not work well.


For both methods, whenver I tried to save the $user_id variable in mySQL database, the string was always truncated even after increasing the field width to 250.

Any ideas on these two linked problems are highly appreciated.

Hi,
Please dont use cookie, Lot of problem will occur
Cookie is something which we can't handle in client machine.
Some client machine will allow us to create cookie and some don't

If you really want I have some other solution to keep track of user


Vinay

Hi,
Please dont use cookie, Lot of problem will occur
Cookie is something which we can't handle in client machine.
Some client machine will allow us to create cookie and some don't

If you really want I have some other solution to keep track of user


Vinay

Thank you for the advise on cookies. Could you please show me how to keep track of users per session. I mean even if they refresh their browsers, their identification should remain the same until they turn their browsers off.

Use their IP address. It's probably the simplist (but not 100%) way to keep track of users for anonymous data gathering.

But if there are two or three users sharing a router then you will track both as one user instead of two different users. I am I correct?

The real answer here is "It depends." While it's certainly true that those behind a router (or firewall even) will have the same external IP, it may not make much difference to the purpose of the script.

About cookies: Go ahead and use them. If a user turns off cookies he will not be able to access your login-required sections of the site. It's not worth the trouble to code around them just for the people that don't understand why cookies exist in the first place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.