0

Please go easy on me, this may just be an easy fix but I've been stuck on this for days now.

I keep getting undefined variable. Once a user logins in it won't work. I'm not sure if I'm using sessions correctly or forgetting code.

panel.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Panel extends CI_Controller {
    public function index()
    {
        $this->load->view('login');
    }

  public function login(){
    $this->load->model('user_model');
    $username = $this->input->post('username');
    $password = strtoupper(sha1($this->input->post('password')));
    $user_id = $this->user_model->login($username, $password);
    if($user_id){
      $user_data = array(
                'user_id' => $user_id,
                'username' => $username,
                'logged_in' => true
            );
      $this->session->set_userdata($user_data);
      redirect('panel/home');
    } else {
      redirect('');
    }
  }
    public function home(){
        $this->load->view('welcome_message');
    }
}

welcome_message.php

<div id="container">
    <h1>Welcome to CodeIgniter!</h1>

    <div id="body">

        <p><?php echo $username; ?></p>

        <p>The page you are looking at is being generated dynamically by CodeIgniter.</p>

        <p>If you would like to edit this page you'll find it located at:</p>
        <code>application/views/welcome_message.php</code>

        <p>The corresponding controller for this page is found at:</p>
        <code>application/controllers/Welcome.php</code>

        <p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
    </div>

    <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo  (ENVIRONMENT === 'development') ?  'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>

Error

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: username

Filename: views/welcome_message.php

Line Number: 75

Backtrace:

File: C:\xampp\htdocs\application\views\welcome_message.php
Line: 75
Function: _error_handler

File: C:\xampp\htdocs\application\controllers\panel.php
Line: 29
Function: view

File: C:\xampp\htdocs\index.php
Line: 315
Function: require_once

This is the error I keep getting

Cameron
  • 719
  • 1
  • 6
  • 16
  • Please dont __roll your own__ password hashing, specially not using MD5() or SHA1(). I am sure Codeigniter must have a mechanism of using `password_hash()` or something better than what you are doing – RiggsFolly Nov 08 '18 at 20:03
  • What do you mean by `Wont Work` – RiggsFolly Nov 08 '18 at 20:03
  • username keeps saying undefined. – Cameron Nov 08 '18 at 20:16
  • Thats because you have not passed any parameters to the view! And that has nothing to do with the session as you are not trying the get username from the session anyway – RiggsFolly Nov 08 '18 at 20:20
  • Yeah I pretty much knew that was the problem, but I don’t now how to fix it. I’ve tried passing in $user_data but then it just says $user_data undefined. – Cameron Nov 08 '18 at 20:23
  • It seems that loading the view and passing in $user_data works but redirect does not. – Cameron Nov 08 '18 at 21:08

0 Answers0