I want to be able to use codeigniter db escape because it hashes my passwords OK but in the password row it adds two quotes on either side Example 'demo1234'
My salt row does not show it which is OK just the password. On password how do I use my db escape but not having the quotes show up on password row in db.
public function addUserToDatabase() {
$this->load->helper('date');
$username = $this->input->post('username');
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)); // Works OK
// Salt does not work if don't use db escape.
$password = $this->db->escape(sha1($salt . sha1($salt . sha1($password)))); // 'demo1234' shows quotes either side off password
$data = array(
'user_id' => "1",
'user_group_id' => "1",
'username' => $username,
'email' => $email,
'salt' => $salt,
'password' => $password,
'ip' => $this->input->ip_address(),
'status' => "1",
'date_added' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$this->db->insert('user', $data);
}
I worked it out I had to do it like this below, all done.
public function addUserToDatabase() {
$this->load->helper('date');
$username = $this->input->post('username');
$email = $this->input->post('email');
$this->db->escape($password = sha1($salt . sha1($salt . sha1($this->input->post('password')))));
$this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9));
$data = array(
'user_id' => "1",
'user_group_id' => "1",
'username' => $username,
'email' => $email,
'salt' => $salt,
'password' => $password,
'ip' => $this->input->ip_address(),
'status' => "1",
'date_added' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$this->db->insert('user', $data);
}
Related
$data = array(
array(
'username' => 'bharatbhai#gmail.com',
'password' => '12345'
),
array(
'username' => 'test#gmail.com',
'password' => '8520'
),
array(
'username' =>'abc#gmail.com',
'password' => '123123'
)
);
and my condition is here
my task is only match one username to one password
but i got error
$check_email = false;
if(array_search($email,array_column($data,'username')) !== FALSE){
$check_email = true;
}
$check_pass = false;
if(array_search($password,array_column($data,'password')) !== FALSE){
$check_pass = true;
}
if(!empty($check_email) && !empty($check_pass)){
echo "<h2>Email and Password matched</h2>";
}
else{
echo '<h2>Oh no email and password not matched.</h2>';
i am match my condition using array_column funcation.
You need to update your logic which checks if both username and password are same in each sub array. I made quick logic you should apply to your code.
<?php
$email = "bharatbhai#gmail.com";
$password = "12345";
$data = array(
array(
'username' => 'bharatbhai#gmail.com',
'password' => '12345'
),
array(
'username' => 'test#gmail.com',
'password' => '8520'
),
array(
'username' =>'abc#gmail.com',
'password' => '123123'
)
);
$hasSameMailAndPass = false;
foreach($data as $key => $value){
if($value["username"] == $email and $value["password"] == $password){
$hasSameMailAndPass = true;
}
}
if($hasSameMailAndPass){
echo "<h2>Email and Password matched</h2>";
} else {
echo '<h2>Oh no email and password not matched.</h2>';
}
?>
I think you can perform a multilevel conditional check.
Actually it seems you search the existancce of the email and of the password, but not actually for the same user.
Does this email exits? yes or no?
does this passwrod exists? yes or no?
Actually should be the following:
does this email exists? yesr or no?
if yes, does that email fit with this password? yes or no?
So you could check if a user exists and has right credentials
$data = array(
array(
'username' => 'bharatbhai#gmail.com',
'password' => '12345'
),
array(
'username' => 'test#gmail.com',
'password' => '8520'
),
array(
'username' =>'abc#gmail.com',
'password' => '123123'
)
);
/**
* #param array $login array('email'=> 'loginemail', 'password'=> 'loginpwd')
* #return bool
*/
function canBeAuthenticate(array $data, array $login)
{
$hasMail = false;
$userIndex = null;
# check if email exists in system
# if not, not even check pwd and return false
# if exites, get ID of user and then check pwd
foreach ($users as $index => $userData) {
if ( $userData == $login['email'] ) {
$hasMail = true;
$userIndex = $index;
}
}
if (!$hasMail) {
echo 'Email not in system';
return false;
}
if ($data[$userIndex]['password'] == $login['password']) {
echo 'Match';
return true;
}
echo 'wrong password';
return false;
}
I have two tables. the first table is tb_mhs and the second user.
how do you make the id_user in the tb_mhs table the same as the id_user in the user table?
thank you very much for your attention
tb_mhs
user
Controller Code :-
$email = $this->input->post('email');
$nama = $this->input->post('nama');
$password = SHA1($this->input->post('password'));
$data1 = array(
'email'=>$email,
'password'=>$password,
'nama'=>$nama,
'level'=>2,
'status'=>0
);
$i=$this->input;
$npm = $this->register_m->create('user',$data1);
$data = array(
'email' => $i->post('email'),
'password' => SHA1 ($i->post('password')),
'npm' => $i->post('npm'),
'nama' => $i->post('nama'),
'j_kelamin' => $i->post('j_kelamin'),
'kelas_id' => $i->post('kelas_id'),
'angkatan_id' => $i->post('angkatan_id'),
'internal_id' => $i->post('internal_id'),
'eksternal_id' => $i->post('eksternal_id'),
'latitude' => -6.873776,
'longitude' => 107.575639,
'berkas' => $i->post('berkas'),
);
// $insert = $this->register_m->create('user',$data1);
$insert1 = $this->register_m->create('tb_mhs',$data);
$this->session->set_flashdata('sukses', 'Data Registrasi Berhasil di Tambahkan');
redirect(base_url('register/viewdataregistrasi'), 'refresh');
}
}
Model
public function create($table, $data)
{
$query = $this->db->insert($table, $data);
return $this->db->insert_id();
}
CI has Query Helper function call $this->db->insert_id(), it returns the insert ID number when performing database inserts.
After the line
$npm = $this->register_m->create('user',$data1);
Add this code
$newUserID = $this->db->insert_id();
Then update your $data array like this
$data = array[
'id_user' => $newUserID,
'email' => $i->post('email'),
'password' => SHA1 ($i->post('password')),
'npm' => $i->post('npm'),
'nama' => $i->post('nama'),
'j_kelamin' => $i->post('j_kelamin'),
'kelas_id' => $i->post('kelas_id'),
'angkatan_id' => $i->post('angkatan_id'),
'internal_id' => $i->post('internal_id'),
'eksternal_id' => $i->post('eksternal_id'),
'latitude' => -6.873776,
'longitude' => 107.575639,
'berkas' => $i->post('berkas'),
];
Rest of code is good.
For more information Read.
I have problem with my session login in codeigniter 3. I can't get data from database to insert session. Whats wrong with my code?
My Controller
function __construct(){
parent::__construct();
$this->load->model('m_login');
}
function index(){
$this->load->view('v_login');
}
function aksi_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$where = array(
'username' => $username,
'password' => md5($password)
);
$cek = $this->m_login->cek_login("admin",$where)->num_rows();
if($cek > 0){
$data_session = array(
'username' => $cek['username'],
'nama' => $cek['nama']
'status' => "login"
);
$this->session->set_userdata($data_session);
redirect(base_url("admin"));
}else{
echo "Username dan password salah !";
}
}
}
My Model
<?php
class M_login extends CI_Model{
function cek_login($table,$where){
return $this->db->get_where($table,$where);
}
}
1.m_login->cek_login
this functions returns a SQL QUERY OBJECT
2.the SQL QUERY OBJECT ->num_rows() function returns a INT ($cek)
3.so $cek is a INT . so u cant use like an array ($cek['username'])
so Please try this code
function aksi_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$where = array(
'username' => $username,
'password' => md5($password)
);
$cek = $this->m_login->cek_login("admin",$where);//$cek is a Query Object
$rows = $cek->num_rows();//$rows is a INT
if($rows > 0){
$cek = $cek->row_array();//now u get an Array
$data_session = array(
'username' => $cek['username'],
'nama' => $cek['nama']
'status' => "login"
);
$this->session->set_userdata($data_session);
redirect(base_url("admin"));
}else{
echo "Username dan password salah !";
}
}
Try This
if($cek > 0){
$cek = $cek->row_array();
$data_session = array(
'username' => $cek['username'],
'nama' => $cek['nama']
'status' => "login"
);
$this->session->set_userdata($data_session);
redirect(base_url("admin"));
}else{
echo "Username dan password salah !";
}
Try like this...
In codeigniter row_array() takes the first matched row in array format.So your function aksi_login() must be like this...
function aksi_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$where = array(
'username' => $username,
'password' => md5($password)
);
$result = $this->m_login->cek_login("admin",$where);
$cek = $this->m_login->cek_login("admin",$where)->num_rows();
if($cek > 0){
$cek = $result->row_array();//no your records are in array format having matched row
$data_session = array(
'username' => $cek['username'],
'nama' => $cek['nama']
'status' => "login"
);
$this->session->set_userdata($data_session);
redirect(base_url("admin"));
}else{
echo "Username dan password salah !";
}
}
For more refer here..https://www.codeigniter.com/userguide3/database/results.html
In my codeigniter database I have 2 tables called user and user_group. When I create new user I need it to be able to get the first id from user_group which is administrator. And the user_group_id for administrator is 1
It currently gets the second row which is user_group_id number '10'
Problem Can Not Get The First Row ID Of User Group
Here is my insert model
public function insert() {
$password = $this->input->post('password');
$username = $this->input->post('username');
$email = $this->input->post('email');
$data = array(
'username' => $username,
'password' => $this->hash($password),
'status' => "1",
'ip' => $this->input->ip_address(),
'user_group_id' => $this->db->insert_id(),
'date_added' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$this->db->set($data);
$this->db->insert('user');
return $this->db->insert_id('user_id');
}
I was able to get the user id as per normal and I hard coded the user_group_id this is how I did it below on my Model.
public function user() {
$this->db->escape($username = $this->input->post('username'));
$this->db->escape($email = $this->input->post('email'));
$this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9));
$this->db->escape($password = sha1($salt . sha1($salt . sha1($this->input->post('password')))));
$data = array(
'username' => $username,
'password' => $password,
'status' => "1",
'salt' => $salt,
'ip' => $this->input->ip_address(),
'user_group_id' => "1",
'date_added' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$this->db->set($data);
$this->db->insert('user');
return $this->db->insert_id();
}
Here is my code so far, it all works except when I try to make 'company' = $company there's something im missing, and id love to know what
if($query) // if the user's credentials validated///
{
$this->db->where('username');
$this->db->select('company');
$company = $this->db->get('user');
$data = array(
'username' => $this->input->post('username'),
'company' => $company
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
}
else
{
$this->index();
}
Both the other answers fix one of the two errors in your code, this is an addition.
if($query) {
$username = $this->input->post('username');
$this->db->select('company')->where('username', $username);
$result = $this->db->get('user')->row_array();
$data = array(
'username' => $username,
'company' => $row['company'],
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
} else {
$this->index();
}
See what I did there? You don't need to use result_array() then grab $query_result[0] as row_array() does that for you. And with a little method chaining thrown in for good measure you can clean up your syntax.
There is a missing comma after "$company".
EDIT: Based on the discussion in the comments, I've rewritten your code to (hopefully) get the value of the company:
if($query) {
$username = $this->input->post('username');
$this->db->where('username', $username);
$this->db->select('company');
$result = $this->db->get('user');
// Here we assume that the query succeeded.
// You should probably double-check.
$company = $result->result_array[0]['company'];
$data = array(
'username' => $username,
'company' => $company,
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
} else {
$this->index();
}
result_array is a function, not a variable. Try
if($query) {
$username = $this->input->post('username');
$this->db->where('username', $username);
$this->db->select('company');
$query_result = $this->db->get('user');
// Here we assume that the query succeeded.
// You should probably double-check.
$result= $query_result->result_array();
$data = array(
'username' => $username,
'company' => $result[0]['company'],
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
} else {
$this->index();
}