Get IPaddress and Converting IPv6

I was save IP address format IPv6, value IPv6 after catch is

::1

the value IPv6 that I want is

2001:0db8:85a3:0000:0000:8a2e:0370:7334

the tutorial conversion that I was browsing comes from

https://www.dereuromark.de/2011/02/04/is-your-website-ipv6-ready/

then I have created some code in Controller, here this

<?php
// in UsersController.php
namespace App\Controller;

use App\Controller\AppController;
use App\Model\Table\UsersTable;
use Cake\Event\event;
use Cake\I18n\Time;
use Cake\ORM\TableRegistry;
use Cake\Network\Request;

class UsersController extends AppController
{
   public function beforeFilter(Event $event) 
   {
	parent::beforeFilter($event);
	$this->Auth->allow(['logout', 'add']);
   }
   public function add()
    {
      $user = $this->Users->newEntity();
      $user->Dibuat = (Time::now())->i18nFormat('dd-MM-yyyy HH:mm:ss');
      $user->IP = normalisasiIPv6($ip); //here function to convert IPv6
      if ($this->request->is('post')) {
	   $user = $this->Users->patchEntity($user, $this->request->getData());
            if ($this->Users->save($user)) {
                $this->Flash->success(__('Pengguna berhasil ditambahkan.'));

                return $this->redirect(['action' => 'index']);
            } 
            $this->Flash->error(__('Pengguna gagal disimpan, atau Administrator lebih dari 2.'));
       }
       $this->set(compact('user'));
    }
    //konversi IPv6
	function normalisasiIPv6($ip) 
        {
	   $ip = $this->request->clientIp();
	   if (strpos($ip, '::') !== false) {
	      $ip = str_replace('::', str_repeat(':0', 8 - substr_count($ip, ':')) . ':', $ip);
	    }
	   if ($ip[0] == ':') {
	      $ip = '0' . $ip;
	    }
	   $ip = explode(':', $ip);
	   foreach ($ip as $part) {
	       $part = str_pad($part, 4, '0', STR_PAD_LEFT);
	    }
		return implode(':', $ip);
	}
}
?>

<?php
// in AppController.php
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;


class AppController extends Controller
{
	
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('RequestHandler', [
            'enableBeforeRedirect' => false,
        ]);
		$this->loadComponent('Flash');
		$this->viewBuilder()->Theme('Kucing');
		//authenticate
		$this->loadComponent('Auth', [
			'authenticate' => [
			    'Form' => [
				'fields' => [
				    'username' => 'Email',
				    'password' => 'Password'
				  ]
			     ]
			],
			'loginAction' => [
				'controller' => 'Users',
				'action' => 'login'
			],
			//jika tidak sah, maka alihkan
			'unauthorizedRedirect' => $this->referer()
		]);
		//izinkan mode membaca
		$this->Auth->allow(['display', 'view', 'index']);
	}
	
	public function beforeFilter(Event $event)
	{
	   $this->Auth->allow(['index', 'view', 'display']);
	}
	
	public function beforeRender(Event $event)
	{
	   parent::beforeFilter($event);
	   $this->set('userInfo', $this->Auth->user());
	}
}
?>

after I’m running, the error message shown here

Notice (1024): Undefined property: ErrorController::$Auth in C:\xampp\htdocs\klinikucing\src\Controller\AppController.php on line 49 [CORE\src\Controller\Controller.php, line 388]

Warning (512): Unable to emit headers. Headers sent in file=C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php line=853 [CORE\src\Http\ResponseEmitter.php, line 48]

Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 148]

Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 181]

Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 181]

An Internal Server Error Occurred

I hope someone can help me, thanx

You’ve called normalisasiIPv6($ip) without ever bothering to initialize $ip. But the errors you’re showing would seem to indicate that there’s another problem somewhere. Your error logs should help to pinpoint that.

I have modified some codes below

<?php
namespace App\Controller;

use App\Controller\AppController;
use App\Model\Table\UsersTable;
use Cake\Event\event;
use Cake\I18n\Time;
use Cake\ORM\TableRegistry;
use Cake\Network\Request;

class UsersController extends AppController
{
   public function add()
    {
      $user = $this->Users->newEntity();
      $user->Dibuat = (Time::now())->i18nFormat('dd-MM-yyyy HH:mm:ss');
      $user->IP = $this->request->normalisasiIPv6(); //here I've modified
        if ($this->request->is('post')) {
		$user = $this->Users->patchEntity($user, $this->request->getData());
            if ($this->Users->save($user)) {
                 $this->Flash->success(__('Pengguna berhasil ditambahkan.'));

                 return $this->redirect(['action' => 'index']);
            } 
            $this->Flash->error(__('Pengguna gagal disimpan, atau Administrator lebih dari 2.'));
        }
        $this->set(compact('user'));
    }

   function normalisasiIPv6() 
   {
	$ip = $this->request->clientIp(); //define $ip from inheritence Use Cake\Network\Request
	if (strpos($ip, '::') !== false) {
		$ip = str_replace('::', str_repeat(':0', 8 - substr_count($ip, ':')) . ':', $ip);
       	}
		
	if ($ip[0] == ':') {
	   $ip = '0' . $ip;
	}
		
	$ip = explode(':', $ip);
		
	foreach ($ip as $part) {
	   $part = str_pad($part, 4, '0', STR_PAD_LEFT);
	}
		
	return implode(':', $ip);
    }
}

afte I was running again, the error message shown more again

Notice (1024): Undefined property: ErrorController::$Auth in C:\xampp\htdocs\klinikucing\src\Controller\AppController.php on line 49 [CORE\src\Controller\Controller.php, line 388]

Warning (512): Unable to emit headers. Headers sent in file=C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php line=853 [CORE\src\Http\ResponseEmitter.php, line 48]

Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 148]

Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 181]

Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 181]

An Internal Server Error Occurred

I’m appreciate for help, thanx

Why would you think normalisasiIPv6 is a function on the request object? Should just be $this->normalisasiIPv6.

You should probably also change beforeRender in your AppController to only call $this->set('userInfo', ...) if $this->Auth is set; it doesn’t seem to when rendering errors, which is making causing a second error and preventing you from seeing details of the first.