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?

3
  • i have already used a session method, but i keep getting errors every single time... pls help me out! Commented Jun 28, 2017 at 7:42
  • MD5 is considered broken for security purposes and is not sufficient for password hashing. Use password_hash() and password_verify() instead. If you're using a version of PHP prior to 5.5, you can use this compatibility pack. Commented Jun 29, 2017 at 18:24
  • Your code is likely vulnerable to SQL injection attacks. You should use prepared statements with bound parameters, via either the mysqli or PDO drivers. This post has some good examples. Commented Jun 29, 2017 at 18:25

1 Answer 1

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";
        }

    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

i have update home.php in answer try this .if it is not work than edit your question and show the error
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
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
if you are add data in session ` $this->session->set_userdata('username', 'password');` where is your username and password?
refere this answer store data in session
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.