开发者

Can't use session variable in routes.php file in codeigniter?

开发者 https://www.devze.com 2023-03-27 21:11 出处:网络
I am use following code to retrieve the session variable in routes.php if($this->db_session->userdata(\'request_url\')!=\"\")

I am use following code to retrieve the session variable in routes.php

if($this->db_session->userdata('request_url')!="")
{
$route['user/(:any)'] = "search_user_name/redirect_url/".$_SESSION['request_url'];
$this->db_session->unset_userdata('request_url');
}
else {
    $route['user/(:any)'] = "search_user_name/index/$1";
}

the session variable would be set into template/header.php

$this-&开发者_StackOverflowgt;db_session->set_userdata('request_url', $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);


You can not use db_session in routes.php because routes.php is parsed before db_session is loaded. Maybe you should create a base controller and redirect from the constructor of the base controller.


Correct me if iam wrong. You can use hooks. Codeigniter user guide hooks


You can use database in routes and put your routes url in database. Here is an example:

require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$table2 =  $db->dbprefix.'lang';
$query2 = $db->get( $table2 );
$result2 = $query2->result();
foreach( $result2 as $row )
{   
    $fields = $db->list_fields($table2);
    $findme   = 'code';
    foreach($fields as $field):
        $pos = strpos($field, $findme);
        if($pos !== false and $row->$field != ''):
            $route[''.$row->$field.''] = 'main/setlang/$1';
        endif;
    endforeach;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消