0

This is my problem: I wanted to make a form that would, with every submission, increment a string variable, to give a different code to every person that fills the form. I used this to make it happen:

session_start(); 
if(empty($_SESSION['count']))
    $_SESSION['count'] = 0;  
$order2 =  $_SESSION['count']+1; 
$_SESSION['count'] =  **$order2**; 
$med="MED0x";
$resultado = $med . $order2;

If I'm in one browser, and fill up the form several times, the variable increments just fine. But when I change browsers, the variable resets, and it returns to Med0x1

How can I solve this?

4
  • 4
    Welcome to web development Commented Oct 13, 2014 at 0:50
  • 1
    There is no reliable way to identify a unique user. The common method is using both a Cookie and the IP address, but you can have more than one user in the same IP and you can have the same user using multiple IPs or changing IPs. The Cookie as you noticed yourself doesn't spread to all browsers and it can be also ignored or deleted. Commented Oct 13, 2014 at 0:59
  • 1
    @john-conde "web development" is not stateless by definition. Commented Oct 13, 2014 at 1:23
  • @tacone: indeed, but session variables are not the way to achieve a user independent state... Commented Oct 13, 2014 at 12:36

2 Answers 2

2

If you're also using MySQL, you could set up a table with an AUTO_INCREMENT column, then insert a new record each time the form is requested, and use LAST_INSERT_ID() to return the new number.

Sign up to request clarification or add additional context in comments.

1 Comment

If for some reason, a database is not an option, then you could write a number to a flat file and increment it instead.
1

Short answer: it is impossible since this is exactly the purpose of a session variable/cookie...

A session variable is a cookie: it is stored at the client side of a user's browser. Furthermore note that a browser can thus decide whether it accepts a cookie. Some browsers or browser plugins for instance discard all cookies from servers they don't trust... And furthermore many browsers assign expire dates on cookies such that after a few days, the data is recycled...

You can store data persistently using a file or use a database like postgreSQL or MySQL (there are many database- or file-like ways to store a file, an exhaustive enumeration is probably out of the scope of SO).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.