PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 21, 2022

[FIXED] CakePHP 3.0 Configure::write() not retaining the key/value from one post to the next?

 February 21, 2022     cakephp, cakephp-3.0, php, session     No comments   

Issue

Each time I process a post, the code runs thru a function in my own MyController extends AppController class. That function calls this function to obtain a unique context number. The idea is that this number should be retained from one post to the next (or one run thru the Controller class function to the next). But instead the Configure::check() always fails and the unique32 is always created anew, so this function always returns 1.

Does anyone know how to make the Configure::write() get retained from one post to the next?

public function getUnique32() {
    if (!Configure::check('unique32')) {
        $unique32 = 0;
        echo "unique32 starts at 0";
    } else {
        $unique32 = Configure::read('unique32');
        echo "unique32 is $unique32";
    }
    if (++$unique32 >= 0xffffffff) {
        $unique32 = 0;
        echo "unique32 is reset to 0";
    }
    Configure::write('unique32', $unique32);
    return $unique32;
}

Solution

Class Configure does not persist data between page requests. Use instead class Session.

In CakePHP 3.x, your code should be written as follows:

public function getUnique32() {

    $session = $this->request->session();
    if (!$session->check('unique32')) {
        $unique32 = 0;
        echo "unique32 starts at 0";
    } else {
        $unique32 = $session->read('unique32');
        echo "unique32 is $unique32";
    }

    if (++$unique32 >= 0xffffffff) {
        $unique32 = 0;
        echo "unique32 is reset to 0";
    }

    $session->write('unique32', $unique32);
    return $unique32;
}

For CakePHP 2.x, you can replace one class for the other in your code.

From the Cookbook 3.x:

  • Accessing the Session Object
  • Configure Class


Answered By - Inigo Flores
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing