Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

codeigniter

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->db_session->set_userdata('request_url', $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
like image 963
Maulik patel Avatar asked Aug 12 '11 10:08

Maulik patel


People also ask

How to use session data in CodeIgniter?

Sessions data are available globally through the site but to use those data we first need to initialize the session. We can do that by executing the following line in constructor. $this->load->library('session'); After loading the session library, you can simply use the session object as shown below.

How to give routes in CodeIgniter?

Routing rules are defined in routes. php file at the location application/config. In this file you'll see $route array, it permits you to specify your own routing criteria. Routes can be classified in two ways, either using Wildcards or Regular Expressions.

How to start session in CodeIgniter 4?

Initializing a Session Sessions will typically run globally with each page load, so the Session class should be magically initialized. To access and initialize the session: <? php $session = \Config\Services::session($config);

What is session in CodeIgniter 3?

The Session class permits you maintain a user's “state” and track their activity while they browse your site. CodeIgniter comes with a few session storage drivers: files (default; file-system based) database.


1 Answers

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.

like image 55
cenanozen Avatar answered Oct 26 '22 06:10

cenanozen