John Davidson

php - Shared session table has two entries in the table for the native and laravel apps

0 comments
Message:


I have a native PHP app which authentication part is handled. I save the session to data base there.


public function write($sessionId, $sessionData)
{
// echo 'write called';
try {
$currentTime = time();
$payload = base64_encode($sessionData);
$userId = isset($_SESSION['login_RegNo'])? $_SESSION['login_RegNo'] : null;
$ip = $this->getIP();
$userAgent = $this->getUserAgent();
$stmt = $this->connection->prepare("REPLACE INTO sessions (`id`, `last_activity`, `payload`, `user_id`, `ip_address`, `user_agent`) VALUES(?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sisiss", $sessionId, $currentTime, $payload, $userId, $ip, $userAgent);
$stmt->execute();
$stmt->close();
//echo base64_decode ($payload);

return TRUE;
} catch (Exception $e) {
// echo 'write exception';
return FALSE;
}
}

I have a link (<a> tag) to the laravel app from this native app.
I have configured the same session table to the laravel app.


.env file


SESSION_DRIVER=database
SESSION_LIFETIME=120
SESSION_CONNECTION=mysql
SESSION_DOMAIN=

session.php


  'table' => 'sessions',

But When I click on the link a different entry is created in the table. Like in the picture.

I am new to the php ann laravel. Can someone explain what have I done wrong?



My Answer:
>getClientIp();_ $stmt = $this->pdo->prepare("REPLACE INTO sessions (id, user_id, ip_address, payload, last_activity) VALUES (:id, :user_id, :ip_address, :payload, :last_activity)");_ $stmt->bindParam(':id', $sessionId);_ $stmt->bindParam(':user_id', $userId);_ $stmt->bindParam(':ip_address', $ip);_ $stmt->bindParam(':payload', $payload);_ $stmt->bindParam(':last_activity', $currentTime);_ $stmt->execute();_ } catch (PDOException $e) {_ // Handle exception here_ }_ }

In the above code snippet, the `write` function is responsible for saving the session data to the database. It first encodes the session data using base64 encoding and then prepares an SQL statement to either insert a new session entry or update an existing one using the `REPLACE INTO` statement.

The session ID, user ID, IP address, encoded payload, and last activity timestamp are bound to the parameters of the SQL statement using `bindParam` method. Finally, the statement is executed to save the session data.

Make sure to handle any exceptions that may occur during the database operation to prevent unexpected behavior in your application.

Rate this post

4 of 5 based on 6141 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us