获取php中的下一个数字[关闭]

I'm not sure what the easiest way to go about this is, I just want one button on a page called assign.

When you visit the page, it says 001, once you click assign, the page refreshes and it says 002 and so on. Do I need a DB to keep track of the current number?

Is there an easier way to do this with javascript that does not need a DB?

If the sequence incremented per user:

<?php
// Make sure session is initialized
session_start();

function onButtonPress(){
    if(!isset($_SESSION['visited'])) $_SESSION['visited'] = 1;
    $_SESSION['visited'] ++;
}

function getValue(){
    return isset($_SESSION['visited']) ? $_SESSION['visited'] : 1;
}
?>

If you want the sequence to increment across all users:

<?php
function onButtonPress(){
    var $counter = 1;
    if(file_exists("./counter.txt")) {
        $counter = (int)file_get_contents("./counter.txt");
    }
    $counter++;
    file_put_contents("./counter.txt", $counter);
}

function getValue(){
    return file_exists("./counter.txt") ? (int)file_get_contents("./counter.txt") : 1;
}
?>