Search code examples
phpsessionphp-5.3php-5.4php-5.2

Execute a PHP method one time


I call first time this route where I put in session 0 :

public function userCaptcha(){
    $_SESSION['isFacebookRegistration'] = 0;
}

After that I call another method which is executed 2 times by server :

 public function index()
 {
    $this->session = $_SESSION['isFacebookRegistration'];
    error_log(print_r($_SESSION['isFacebookRegistration'],true), 3, "/tmp/error.log");
    $_SESSION['isFacebookRegistration'] = 3;
    return $this->render('template/index.twig');
 }

The view is :

{{ dump(session) }}

In the console for $_SESSION['isFacebookRegistration'], I get : 0 3, In the view only 3. So the question is, It's possible to send in view value 0 and after that to modify the value of $_SESSION['isFacebookRegistration'] in 3 ? I repeat that index() method is call 2 times by server.


Solution

  • You need this? If I understand you correctly, coz my english is not well

    private $flag = false;
    
    public function index()
    {
        $this->session = $_SESSION['isFacebookRegistration'];
        error_log(print_r($_SESSION['isFacebookRegistration'],true), 3, "/tmp/error.log");
    
        if ($this->flag) {
            $_SESSION['isFacebookRegistration'] = 3;
        } else {
            $this->flag = true;
        }
    
        return $this->render('template/index.twig');
    }
    

    Also you can pass an additional parameter (I think it will be better):

    public function index($changeSessionValue = false)
    {
        $this->session = $_SESSION['isFacebookRegistration'];
        error_log(print_r($_SESSION['isFacebookRegistration'],true), 3, "/tmp/error.log");
    
        if ($changeSessionValue) {
            $_SESSION['isFacebookRegistration'] = 3;
        }
    
        return $this->render('template/index.twig');
    }