Repository: codeigniter4/CodeIgniter4 (PHP)

Issue:
Bug: Can't connect to Redis with Session RedisHandler over TLS
### PHP Version

8.1

### CodeIgniter4 Version

4.3.1

### CodeIgniter4 Installation Method

Composer (using `codeigniter4/appstarter`)

### Which operating systems have you tested for this bug?

Windows, Linux

### Which server did you use?

apache

### What happened?

I use a MS Azure Redis server which needs to be connected over TLS. When i set the $sessionDriver to the RedisHandler in the App.php file i edited the savePath to: 
`tls://nameofserver.redis.cache.windows.net:6380?auth=password`

When i debug the '/framework/system/Session/Handlers/RedisHandler.php' and check the '$this->savePath'. i see the following output because of the preg_match():
 
`Array
(
    [host] => tls
    [port] => 6379
    [password] => 
    [database] => 0
    [timeout] => 0
)`

This results in the Redis server not connecting and it gives a RedisException error. 



### Steps to Reproduce

Add 'tls://' to the Redis database server url.

### Expected Output

`Array
(
    [host] => tls://nameofserver.redis.cache.windows.net
    [port] => 6380
    [password] => password
    [database] => 0
    [timeout] => 0
)`

### Anything else?

I managed to bypass this (hopefully temporary) by copying the system folder in our project folder and hardcoded the Redis Server URL.


Discussion: For now we connect to `Redis` without `Protocol`

I send PR #7187, @TOAD-Joey can try this changes?

@ddevsr Yes, great, this fixes the issue. 



Fix (patch):
diff --git a/system/Session/Handlers/RedisHandler.php b/system/Session/Handlers/RedisHandler.php
index 4492c4dbed32..9910637663f5 100644
--- a/system/Session/Handlers/RedisHandler.php
+++ b/system/Session/Handlers/RedisHandler.php
@@ -24,7 +24,8 @@
  */
 class RedisHandler extends BaseHandler
 {
-    private const DEFAULT_PORT = 6379;
+    private const DEFAULT_PORT     = 6379;
+    private const DEFAULT_PROTOCOL = 'tcp';
 
     /**
      * phpRedis instance
@@ -102,20 +103,21 @@ protected function setSavePath(): void
             throw SessionException::forEmptySavepath();
         }
 
-        if (preg_match('#(?:tcp://)?([^:?]+)(?:\:(\d+))?(\?.+)?#', $this->savePath, $matches)) {
-            if (! isset($matches[3])) {
-                $matches[3] = ''; // Just to avoid undefined index notices below
+        if (preg_match('#(?:(tcp|tls)://)?([^:?]+)(?:\:(\d+))?(\?.+)?#', $this->savePath, $matches)) {
+            if (! isset($matches[4])) {
+                $matches[4] = ''; // Just to avoid undefined index notices below
             }
 
             $this->savePath = [
-                'host'     => $matches[1],
-                'port'     => empty($matches[2]) ? self::DEFAULT_PORT : $matches[2],
-                'password' => preg_match('#auth=([^\s&]+)#', $matches[3], $match) ? $match[1] : null,
-                'database' => preg_match('#database=(\d+)#', $matches[3], $match) ? (int) $match[1] : 0,
-                'timeout'  => preg_match('#timeout=(\d+\.\d+|\d+)#', $matches[3], $match) ? (float) $match[1] : 0.0,
+                'protocol' => ! empty($matches[1]) ? $matches[1] : self::DEFAULT_PROTOCOL,
+                'host'     => $matches[2],
+                'port'     => empty($matches[3]) ? self::DEFAULT_PORT : $matches[3],
+                'password' => preg_match('#auth=([^\s&]+)#', $matches[4], $match) ? $match[1] : null,
+                'database' => preg_match('#database=(\d+)#', $matches[4], $match) ? (int) $match[1] : 0,
+                'timeout'  => preg_match('#timeout=(\d+\.\d+|\d+)#', $matches[4], $match) ? (float) $match[1] : 0.0,
             ];
 
-            preg_match('#prefix=([^\s&]+)#', $matches[3], $match) && $this->keyPrefix = $match[1];
+            preg_match('#prefix=([^\s&]+)#', $matches[4], $match) && $this->keyPrefix = $match[1];
         } else {
             throw SessionException::forInvalidSavePathFormat($this->savePath);
         }
@@ -135,7 +137,7 @@ public function open($path, $name): bool
 
         $redis = new Redis();
 
-        if (! $redis->connect($this->savePath['host'], ($this->savePath['host'][0] === '/' ? 0 : $this->savePath['port']), $this->savePath['timeout'])) {
+        if (! $redis->connect($this->savePath['protocol'] . '://' . $this->savePath['host'], ($this->savePath['host'][0] === '/' ? 0 : $this->savePath['port']), $this->savePath['timeout'])) {
             $this->logger->error('Session: Unable to connect to Redis with the configured settings.');
         } elseif (isset($this->savePath['password']) && ! $redis->auth($this->savePath['password'])) {
             $this->logger->error('Session: Unable to authenticate to Redis instance.');
diff --git a/user_guide_src/source/changelogs/v4.3.2.rst b/user_guide_src/source/changelogs/v4.3.2.rst
index f1b8e7c5dc72..4652ee995b85 100644
--- a/user_guide_src/source/changelogs/v4.3.2.rst
+++ b/user_guide_src/source/changelogs/v4.3.2.rst
@@ -27,6 +27,8 @@ Deprecations
 Bugs Fixed
 **********
 
+* Fixed : ``Session`` Redis connect to protocol TLS
+
 See the repo's
 `CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
 for a complete list of bugs fixed.
diff --git a/user_guide_src/source/libraries/sessions.rst b/user_guide_src/source/libraries/sessions.rst
index a7a63c1b48df..7f316c569276 100644
--- a/user_guide_src/source/libraries/sessions.rst
+++ b/user_guide_src/source/libraries/sessions.rst
@@ -613,7 +613,7 @@ RedisHandler Driver
 
 .. note:: Since Redis doesn't have a locking mechanism exposed, locks for
     this driver are emulated by a separate value that is kept for up
-    to 300 seconds.
+    to 300 seconds. With ``v4.3.2`` or above, You can connect ``Redis`` with **TLS** protocol.
 
 Redis is a storage engine typically used for caching and popular because
 of its high performance, which is also probably your reason to use the