qadenza
qadenza

Reputation: 9293

How to reset a php session to starting value at certain point?

  <?php
    session_start();
    if(isset($_SESSION['enter']))
    $_SESSION['enter']=$_SESSION['enter']+1;
    else
    $_SESSION['enter']=1;

    if ($_SESSION['enter']=7) {
      unset($_SESSION['enter']);
        $_SESSION['enter']=1;  // here I want to count from beginning if there are seven enters
    }
    $enter = $_SESSION['enter'];
    $enter = sprintf("%03d", $enter);
    echo $enter; //keep always 001
    ?>

So, I want to count page enters from 1 to 7 and then back to 1... and so on, but in the above case it always stays on 1.

Any help.

Upvotes: 1

Views: 45

Answers (2)

jeroen
jeroen

Reputation: 91744

This is your problem:

if ($_SESSION['enter']=7) {

You are not comparing the values, but assigning it and that always returns a true value, causing the code after it to run.

Just change it to:

if ($_SESSION['enter']===7) {

In this case you can also skip the if and do:

$_SESSION['enter'] = ($_SESSION['enter'] % 7) + 1;

in your first if statement. More about the modulus operator.

Upvotes: 1

Mitya
Mitya

Reputation: 34556

if ($_SESSION['enter']=7) {

==, not =

Upvotes: 3

Related Questions