login codeigniter 4 code example

Example 1: codeigniter 4 login example

<html>
   <head></head> <!-- move code from welcome_message.php's html <head> to here -->
   <body>
        <!-- move code from welcome_message.php's style to here, 
             or use separate file for css and apply to <head>
        -->
     
        <div class="wrap">
            <?php echo $content; ?>
        </div>
   </body>
</html>

Example 2: codeigniter 4 login example

<?php
 
if ( ! function_exists('render'))
{
    function render(string $name, array $data = [], array $options = [])
    {
        return view(
            'layout',
            [
                'content' => view($name, $data, $options),
            ],
            $options
        );
    }
}

Example 3: codeigniter 4 login example

<?php namespace Config;
 
use CodeIgniter\Events\Events;
 
Events::on('post_controller_constructor', function() {
    helper('render');
});

Example 4: CODEIGNITER codeigniter 4 auth

<?php

namespace App\Controllers;

use App\models\Users_model;

class Signin extends BaseController {

    public function index() {
        return view('signin/index');
    }

    public function authenticate() {
        if ($this->exists($_POST['email'], $_POST['password']) != NULL) {
            $session = session();
            $session->set('email', $_POST['email']);
            return $this->response->redirect(site_url('signin/profile'));
        } else {
            $data['msg'] = 'wrong';
            return view('signin', $data);
        }
    }

    public function profile() {
        return view('signin/profile');
    }

    private function exists($email, $password) {
        $model = new Users_model();
        $account = $model->where('email', $email)->first();
        if ($account != NULL) {
            if (password_verify($password, $account['password'])) {
                return $account;
            }
        }
        return NULL;
    }

}

Example 5: CODEIGNITER codeigniter 4 auth

<?php

namespace App\models;

use CodeIgniter\Model;

class Users_model extends Model {

    protected $table = 'users';
    protected $primaryKey = 'id';
    protected $allowedFields = ['first_name', 'last_name', 'email', 'password'];

}

Example 6: codeigniter 4 login example

<?php namespace App\Controllers;
 
use CodeIgniter\Controller;
 
class Home extends Controller
{
    public function index()
    {
        return render('welcome_message');  
    }
}

Tags:

Php Example