Get a unique session identifier in the encoder

I am trying to get the session id using session_id (); But I find out that it will be regenerated every 5 minutes.

So, I got a trick to set a random number to a user session variable. eg,

$uniqueId = uniqid(rand(), TRUE);
$this->session->set_userdata("my_session_id", md5($uniqueId)); 

Now the question is where should I put this code. If I put this code in my controller constructor, it will be executed for each request. and will give me a different session id for each request.

How to set this session variable only once? and it will not change until the destroy () session exists.

+4
source share
4 answers
  1. , . - . session_regenerate_id()? .

  2. , .

  3. . , . .

  4. , , # 3 $_SESSION['guest']=USER_IP , IP. isset($_SESSION['guest']), .

  5. XSS, , IP, .

+3

constructor , already set or not. , set it do nothing. ..

$uniqueId = uniqid(rand(), TRUE);//generates random number 
if(!$this->session->has_userdata('my_session_id'))//if session is not set then it sets (if your session has already value then this step will be skip out)
{
$this->session->set_userdata("my_session_id", md5($uniqueId)); 
}
+1

php:

$session_id = session_id();

now $session_id - .

+1

config.php ,

$config['sess_expiration'] =  0;//Session does not expire
$config['sess_time_to_update'] = 0;//Disable session ID regeneration

__construct()

public function __construct(&$params){
     // DO NOT forget this
     parent::__construct($params);
}

Then when you get the session, you will use

$this->session->userdata('id');
0
source

Source: https://habr.com/ru/post/1672346/


All Articles