What's new

Welcome to xCrud Community - Data Management and extended PHP CRUD

Join us now to get access to all our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, and so, so much more. It's also quick and totally free, so what are you waiting for?

Session problems with native CI4 sessions

sippeters

New member
Joined
Apr 20, 2024
Messages
5
Reaction score
0
Points
1
Location
Hueckelhoven
https://xcrud.me/threads/important-update-unauthorized-xcrud-software-versions.101/
Hello everyone,

I have set up a new CodeIgniter 4 project and installed XCrud without any further customizations.

Displaying simple tables works fine, but whenever I perform AJAX operations - e.g. switching to another page - I get a session error message:
"The verification key is out of date"

I need the CI4 sessions together with the XCrud sessions, but I always get errors and cannot find a solution.
My session name is the default value "PHPSESSID" in both systems.

I have searched a lot, but can't find a solution.

Can anyone give me a hint what I need to change to make this work?

Thx and best,
Christoph
 
Last edited:

sippeters

New member
Joined
Apr 20, 2024
Messages
5
Reaction score
0
Points
1
Location
Hueckelhoven
I'm sorry about using an unofficial version - I did not realize it's not original.

Now I replaced wrong version 1.7 by "xCrud Reload v1.0", but my problem stays the same.

Has anyone ever gotten XCrud to work together with CodeIgniter 4???
 

DaDo

Administrator
Staff member
Joined
Dec 1, 2021
Messages
108
Reaction score
23
Points
18
I'm sorry about using an unofficial version - I did not realize it's not original.

Now I replaced wrong version 1.7 by "xCrud Reload v1.0", but my problem stays the same.

Has anyone ever gotten XCrud to work together with CodeIgniter 4???
you need to add some modification on the class to get work on ci4
 

DaDo

Administrator
Staff member
Joined
Dec 1, 2021
Messages
108
Reaction score
23
Points
18
Can you give me a hint as to where I need to start?
PHP:
First define xcrud as namespace
    
    <?php
namespace Xcrud;

use Xcrud\Db;


    public $ci = null;
    



public static function get_instance($name = false, &$ci)
    {
        self::init_prepare();
        if (! $name)
            $name = sha1(rand() . microtime());
        if (! isset(self::$instance[$name]) || null === self::$instance[$name]) {
            self::$instance[$name] = new self();
            self::$instance[$name]->instance_name = $name;
            self::$instance[$name]->ci = &$ci;
        }
        self::$instance[$name]->instance_count = count(self::$instance);
        return self::$instance[$name];
    }

    public static function get_requested_instance(&$ci)
    {
        if (isset($_POST['xcrud']['instance']) && isset($_POST['xcrud']['key']) && isset($_POST['xcrud']['task'])) {
            self::init_prepare('post');
            $key = $_POST['xcrud']['key'] ? $_POST['xcrud']['key'] : self::error('Security key cannot be empty');
            $inst_name = $_POST['xcrud']['instance'] ? $_POST['xcrud']['instance'] : self::error('Instance name cannot be empty');
            $is_get = false;
        } elseif (isset($_GET['xcrud']['instance']) && isset($_GET['xcrud']['key']) && isset($_GET['xcrud']['task']) && $_GET['xcrud']['task'] == 'file') {
            self::init_prepare('get');
            $key = $_GET['xcrud']['key'] ? $_GET['xcrud']['key'] : self::error('Security key cannot be empty');
            $inst_name = $_GET['xcrud']['instance'] ? $_GET['xcrud']['instance'] : self::error('Instance name cannot be empty');
            $is_get = true;
        } else {
            self::error('Wrong request!');
        }
        $ci = &get_instance();
        $xcrud_session = $ci->session->userdata('xcrud_session');

          if (1 == 1) {
            self::$instance[$inst_name] = new self();
            self::$instance[$inst_name]->is_get = $is_get;
            self::$instance[$inst_name]->ajax_request = true;
            self::$instance[$inst_name]->instance_name = $inst_name;
            self::$instance[$inst_name]->ci = &$ci;
            self::$instance[$inst_name]->import_vars($key);
            self::$instance[$inst_name]->inner_where();
            return self::$instance[$inst_name]->render();
        } else
            self::error('<strong>The verification key is out of date</strong><br />
                This means that your browser cached a previous version of this page with an old key (for security reasons the verification key is generated every request)<br />
                Why? Maybe you pressed the back button in your browser or opened a bookmark from last session. <br /><strong>Just reload the page, nothing happened :)</strong>');
    }



and this is helper ci
    
    
    
    <?php
require_once (rtrim(FCPATH, '\\') . '/application/libraries/Xcrud/xcrud.php');
if (! function_exists('xcrud_get_instance')) {

    function xcrud_get_instance($name = false) {
        $ci = &get_instance();
        $ci->load->library('session');
        $ci->load->helper('url');
        Xcrud_config::$scripts_url = base_url('');
        header('Content-Type: text/html; charset=' . Xcrud_config::$mbencoding);
        $crud = Xcrud::get_instance($name, $ci);
        return $crud;
    }
}
if (! function_exists('xcrud_store_session')) {

    function xcrud_store_session() {
        $CI = &get_instance();
        $CI->load->library('session');
        $_SESSION['xcrud_sess'] = Xcrud::export_session();
    }
}
if (! function_exists('xcrud_restore_session')) {

    function xcrud_restore_session() {
        $CI = &get_instance();
        $CI->load->library('session');
        Xcrud::import_session($_SESSION['xcrud_sess']);
    }
}
 

sippeters

New member
Joined
Apr 20, 2024
Messages
5
Reaction score
0
Points
1
Location
Hueckelhoven
Thanks for your help, but it does not work and leads to many errors.

For example: $ci = &get_instance();" - this only works in CI3.
In CI4 I am not able to get the CI instance at this point - especially in AJAX-context.

I will give up - after many hours I believe that XCrud and CI4 are not compatible due to their different architecture.
 

mungle

New member
Joined
Dec 9, 2021
Messages
11
Reaction score
2
Points
3
Location
Dubai
Hi,
After reading your message I wanted to experiment with an alternative: Grocery CRUD but it's far away from the all functionality that Xcrud has.
Did you find a solution to use Xcrud in Codeigniter 4?
Thank you!
 

sippeters

New member
Joined
Apr 20, 2024
Messages
5
Reaction score
0
Points
1
Location
Hueckelhoven
No, I didn't find a solution.
As soon as you want to use the sessions of ci4, e.g. with shield or other libraries, xCrud no longer works together with CI4.
Nobody knew a solution, so in the end I also ended up with groceryCrud and had to implement the missing functions myself.
 

mungle

New member
Joined
Dec 9, 2021
Messages
11
Reaction score
2
Points
3
Location
Dubai
Oh... groceryCrud lacks of functionalities! I was looking to create tabs in my add/edit view but I have to code everything... in Xcrud is so simple!
I'm not a php expert so I can't push the project in a professional way... I hope somebody can dedicate time to improve it. I didn't found anything better than Xcrud!
 
Top Bottom