有时在其他访问者中设置会话

I'm using session to determine how many times our site's visitors download a file. If the value of downinc is equal to 3, then we force them to register on the site.

Say for example I have pos_source_code.php page with a download button, then on first time request the downinc session is set 1.

if (!isset($_SESSION['downinc'])) $_SESSION['downinc'] = 1;

When the visitor click the download button on pos_source_code.php page, he/she will be redirected to another page called download.php. I then change the value of downinc to increment by 1. But before that, I checked the session if it is set or not, else it should download the file:

if (!isset($_SESSION['downinc'])) {
    $error = "Invalid download. No Session.";
    // some code here
}else{
    $_SESSION['downinc'] = $_SESSION['downinc'] + 1;
    //some code here to download the file.
}

But sometimes, some of my visitors emailed me that they cannot download the file.

Is there something wrong with my code? BTW, I'm using drupal 7. So I don't need to call session_start().

Update:

Just want to add that I am using memcached. Don't know if this is related to memcached.

Because this is something that your users are reporting and that you (presumably) have not been able to reproduce, you need more data to troubleshoot. Your code looks fine. I suspect this might be user error (such as the user being impatient and re-clicking the link several times so that the session value increments too much before the download starts), which you may need to identify and figure out how to code around. I would recommend creating a log file that you can use to correlate user complaints to troubleshoot the issue. Something like this:

file_put_contents( "log.txt", date( "Y-m-d H:i:s" ) . " - " . $_SERVER['REMOTE_ADDR'] . " - " . $_SESSION['downinc'] . "
", FILE_APPEND );

This will write a timestamp, the remote IP, and the current session value to a text file. When a user contacts you with this issue, you can view the saved values and see if, for instance, the session value incremented within rapid succession, or if perhaps it somehow went straight to 3 but never had a value of 1 or 2, or whatever else may look odd.

But you certainly need more data to troubleshoot this if you can't reproduce it on your own.