Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

luk's avatar
Level 1

How can I get and update the session id of the user in my php

In my PHP project I put 0 to the sessionID when the user is registered and I want to get the current logged in sessionid and update the value and save and use it for the web socket . and i used for the following functions for getting the current logged in session

public function getSessionID(){
		$ss = session_id();
		var_dump($ss);
		return $ss;

		
	}

for update the session id

 public function updateSession(){
	  	$stmt = $this->db->prepare("UPDATE `users` SET `sessionID` = :sessionID WHERE `id` :id");
	  	$stmt->bindParam(":sessionID",$this->sessionID,PDO::PARAM_STR);
	  	$stmt->bindParam(":id",$this->id,PDO::PARAM_INT);
	  	//var_dump($this->sessionID);
	  	$stmt->execute();


	  }

but when var_dump($ss) it look like "string 'ck3o7jj67av14ciri14pjvnkks' (length=26)" and also whenvar_dump($this->sessionID) in the updatesession method it look like "string 'ck3o7jj67av14ciri14pjvnkks' (length=26)" and it didn't change the value of the sessionID in the database . i don't know why it did not work!! please help me!

0 likes
8 replies
Sinnbeck's avatar

Can you show what this gives you?

 public function updateSession(){
	  	$stmt = $this->db->prepare("UPDATE `users` SET `sessionID` = :sessionID WHERE `id` :id");
	  	$stmt->bindParam(":sessionID",$this->sessionID,PDO::PARAM_STR);
	  	$stmt->bindParam(":id",$this->id,PDO::PARAM_INT);
	  	var_dump($this->id); //this
	  	$stmt->execute();

	  }
1 like
Sinnbeck's avatar

@habte And it is the user with id 11 you are looking at in the database?

Sinnbeck's avatar

@habte You are missing an = in the prepare statement.

$stmt = $this->db->prepare("UPDATE `users` SET `sessionID` = :sessionID WHERE `id` = :id");
1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@habte If it was solved, please mark a best answer to close the thread :)

1 like
luk's avatar
Level 1

@Sinnbeck ok I did it and I posted another issue can you help me ?

Please or to participate in this conversation.