0

Home.php this is my controller

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

class home extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('shakiladb');
    }

    public function index() {
        $this->load->view('index');
        //loading session library
        $this->load->library('session');

        //adding data to session
        $this->session->set_userdata('username', 'password');
        $this->load->view('session_view');
    }

    public function register() {
        $this->load->view('register');
    }

    public function checkdb() {
        //contact the model to run the query
        $this->shakiladb->savetodb();
    }

    public function unset_session_data() {
        //loading session library
        $this->load->library('session');

        //removing session data
        $this->session->unset_userdata('username');
        $this->load->view('session_view');
    } 

}

shakiladb.php this is my model

<?php

class shakiladb extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function savetodb() {
        //capture the username and password
        $username = $this->input->post('Username');
        $password = $this->input->post('Password');

        //query to save in the table
        //$query = $this->db->query("INSERT INTO table1 (USERNAME, PASSWORD) VALUES ('$username', '$password')");

        $validate = $this->db->query("SELECT USERNAME FROM table1 WHERE USERNAME = '".$username."'");
        $count = count($validate);

        if ($validate->num_rows() > 0) {
            echo "Not working";
        } else {
            $this->db->query("INSERT INTO table1 (USERNAME, PASSWORD) VALUES ('$username', md5('$password'))");
            echo "Works";
        }

    }
}

register.php this is my view

    <?php

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

    ?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<?php 
    echo $this->session->userdata('session');
?>
<div align="center">
<form id="reg">
    Username : <input type="text" name="Username" id="Username"><br><br>
    Password : <input type="password" name="Password" id="Password"><br><br>
    <input type="submit" name="btn" id="btn" value="OK">
    <input type="reset" name="Reset" name="Reset" value="Reset"><br><br>
</form>
</div>

<script type="text/javascript">

$(function() {
    function validation() {

        var username = $("#Username").val();
        var password = $("#Password").val();

        if (username == "") {
            alert("Please fill in the username field!");
            return false;
        }

        if (password == "") {
            alert("Please fill in the password field!");
            return false;
        }

        return true;

    }

$('#btn').click(function() {

    var method = validation();

    if (method == true) {
        $.ajax({
        url: "http://localhost/shark/home/checkdb",
        type: 'POST',
        data: $("#reg").serialize(),
        success: function(result) {
            if (result == "Not working") {
                alert("Already");
            }
            if (result == "Works") {
                alert("Not existing");
            }
        }
        });
        return false;
    }
    return false;
});

});

</script>

</body>
</html>

this is my code and i need to know how can i use sessions and fulfill the goal of retrieving data from the database?

  • i have already used a session method, but i keep getting errors every single time... pls help me out! – Shakila Lehan Jun 28 '17 at 07:42
  • MD5 is considered broken for security purposes and is not sufficient for password hashing. Use [`password_hash()`](http://us3.php.net/manual/en/function.password-hash.php) and [`password_verify()`](http://us3.php.net/manual/en/function.password-verify.php) instead. If you're using a version of PHP prior to 5.5, you can use [this compatibility pack](https://github.com/ircmaxell/password_compat). – Alex Howansky Jun 29 '17 at 18:24
  • Your code is likely vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Jun 29 '17 at 18:25

1 Answers1

0

Home.php this is your controller

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

class home extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('shakiladb');
    }

    public function index() {
        $this->load->view('index');
        //loading session library


        //adding data to session
        $this->session->set_userdata('username', 'password');
        $this->load->view('session_view');
    }

    public function register() {
        $this->load->view('register');
    }

    public function checkdb() {
        //contact the model to run the query
         $username = $this->input->post('Username');
        $password = $this->input->post('Password');
        $this->shakiladb->savetodb($username,$password);
    }

    public function unset_session_data() {
        //loading session library
        //removing session data
        $this->session->unset_userdata('username');
        $this->load->view('session_view');
    } 

}

shakiladb.php this is your model

<?php

class shakiladb extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function savetodb($username,$password) {
        //capture the username and password

        $password=md5($password);
        //query to save in the table
        //$query = $this->db->query("INSERT INTO table1 (USERNAME, PASSWORD) VALUES ('$username', '$password')");

        $validate = $this->db->query("SELECT USERNAME FROM table1 WHERE USERNAME = '".$username."'");
        $count = count($validate);

        if ($validate->num_rows() > 0) {
            echo "Not working";
        } else {
            $this->db->query("INSERT INTO table1 (USERNAME, PASSWORD) VALUES ('".$username."', '".$password."')");
            echo "Works";
        }

    }
}
Gopal Bhuva
  • 654
  • 2
  • 13
  • 20
  • i have update home.php in answer try this .if it is not work than edit your question and show the error – Gopal Bhuva Jun 28 '17 at 07:57
  • **A PHP Error was encountered Severity: Notice Message: Undefined property: CI_Loader::$session Filename: views/register.php Line Number: 12 Backtrace: File: C:\xampp\htdocs\codeigniter\application\views\register.php Line: 12 Function: _error_handler File: C:\xampp\htdocs\codeigniter\application\controllers\Home.php Line: 23 Function: view File: C:\xampp\htdocs\codeigniter\index.php Line: 315 Function: require_once** _this is one error_ – Shakila Lehan Jun 28 '17 at 08:01
  • **An uncaught Exception was encountered Type: Error Message: Call to a member function userdata() on null Filename: C:\xampp\htdocs\codeigniter\application\views\register.php Line Number: 12 Backtrace: File: C:\xampp\htdocs\codeigniter\application\controllers\Home.php Line: 23 Function: view File: C:\xampp\htdocs\codeigniter\index.php Line: 315 Function: require_once** _this is another error_ – Shakila Lehan Jun 28 '17 at 08:04
  • if you are add data in session ` $this->session->set_userdata('username', 'password');` where is your username and password? – Gopal Bhuva Jun 28 '17 at 08:06
  • 1
    refere this answer [store data in session](https://stackoverflow.com/questions/23078716/how-to-store-variable-in-session-codeignitor) – Gopal Bhuva Jun 28 '17 at 08:09
  • `session->userdata('session'); ?>
    Username :

    Password :



    ` **here**
    – Shakila Lehan Jun 28 '17 at 08:15
  • you are calling 2 views `$this->load->view('index');` and `$this->load->view('session_view');` in index function is totally wrong. first of all make your structure perfect so everyone can help you – Gopal Bhuva Jun 28 '17 at 08:29