Integrating Codeigniter 3 with phpmyadmin database and fullcalendar.io - php

Hello im using fullcalendar.io for my website, for the framework i use ci3, im new with ci.
So the problem is, can i just load the database fullcalendar.io just for user who logged in?
im planning to use email to connect it since i used email to login to my website..
here is my user database example :
here is my events on fullcalendar.io database :
here is my load function in controllers
function load()
{
$event_data = $this->fullcalendar_model->fetch_all_event();
foreach ($event_data->result_array() as $row) {
$data[] = array(
'id' => $row['id'],
'title' => $row['title'],
'start' => $row['start_event'],
'end' => $row['end_event']
);
}
echo json_encode($data);
}
and here is my fetch_all_event() code
function fetch_all_event()
{
$this->db->order_by('id');
return $this->db->get_where('events');
}
should i adding something or what? can anyone help please im just starting with ci, thank u

So the easiest way is you just have to store ur session data when u log in, store it at the page u login at.
here is mine :
if ($user) {
// jika usernya ada
if ($user['is_active'] == 1) {
if (password_verify($password, $user['password'])) {
$data = [
'email' => $user['email'],
'role_id' => $user['role_id']
];
$this->session->set_userdata($data);
and u can call that email in ur load model, here is my fetch_all_event function code on my model :
function fetch_all_event()
{
$email = $this->session->userdata('email');
$this->db->order_by('id');
return $this->db->get_where('events', ['email' => $email]);
}
this will solve that problem, kept that in mind ur email at database shouldnt do as primary or unique key.

Related

Session on codeigniter not working perfectly

I have an issue regarding session (userdata/flashdata) on my code.
Modal
public function loginCheck(){
$email = $this->input->post('email');
//encrypt password
$this->load->library("hashing");
$password = $this->hashing->incrypt($this->input->post('password'));
$this->params = array('email' => $email, 'password' => $password);
$user = $this->findById();
if(count($user)>0){
$data = array(
'email' => $user->email,
'isLoggedIn' => 1,
'user_id' => $user->id,
'user_type' => $user->user_type
);
$this->session->set_userdata($data);
return true;
}
return false;
}
And my Controller
public function login(){
$this->load->model('model_users');
//if posted login
$submit = $this->input->post('submit');
if($submit){
$this->load->model('model_users');
$rules = $this->model_users->rules;
$this->form_validation->set_rules($rules);
if($this->form_validation->run() == true){
// user credential from model
if($this->model_users->loginCheck()== true){
redirect("admin/site/index");
} else{
$this->session->set_flashdata('message', 'Oops! Invalid email and/ or password.');
redirect("admin/site/login");
}
}else{
$this->session->set_flashdata('message', 'Oops! Invalid email and/ or password.');
redirect("admin/site/login");
}
}
$this->loadPartialView("admin/login");
}
The session is not being set on CI 3.0. The function set_userdata() is not functioning well.
The manual session initializing is also having trouble.
Please try load Session Library into Controller and Modal before using it.
$this->load->library('session');
i dont think if this is a good answer, but have you call your session in view?
as i know ,
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.
it can be found in sesseion documentation
so in your view in div tag or other, its like
<?php echo $this->session->flashdata('message');?>
First you can add library of session.
There are two way of add library.
1) You can add in controller method.
2) autoload in autoload.php file.

Codeigniter how to get userid after login

I have successfully created a registration and login system.
I have used useremail and password in the login form and I want to display the username and other properties related to that logged in user.
What are the simplest and best practices to get the userid after a login in codeigniter?
As good coder create session at login time and use session at website wide.
public function login($username, $password) {
$user = $this->db
->select("username, name, phone")
->where(
[
'username' => $username,
'password' => md5($password)
]
)
->get("table_name")
->row();
if ($user) {
$logindata = [
'userid' => $user->username,
'name' => $user->name,
'phone' => $user->phone
];
$this->session
->set_userdata($logindata);
return true;
}
else {
return false;
}
}
Then after you can use anywhere in website
echo $this->session->userid;
I hope it will help you in general way.
Best practice would be that when you check the login details in your model after success you should create a session saving userid of that user and use that anywhere in your application for fetching respective data against that user. Take a look at following psuedo code for login user.
public function userAuthentication($username,$pass)
{
$this->db->select('id');
$this->db->where('username',$username);
$this->db->where('password',md5($pass));
$result = $this->db->get('users')->result();
if(!empty($result)){
$this->session->set_userdata('userlogged_in', $result[0]->id);
return true;
}else{
return false;
}
}
You can place this function in your model and apply it in your controller. I hope this helps.
You may create a session and set user id after successful login.

if else is not working

The model:
function validate()
{
$this->db->where('username',$this->input->post('username'));
$this->db->where('password',md5($this->input->post('password')));
$query = $this->db->get('memberships');
if($query->num_rows() == 1)
{
return TRUE;
}
}
function validate_admin()
{
$this->db->where('adminname',$this->input->post('username'));
$this->db->where('password',md5($this->input->post('password')));
$query = $this->db->get('admin');
if($query->num_rows() == 1)
{
return TRUE;
}
}
The controller
function validate_credentials()
{
$this->load->model('membership_model');
if($this->membership_model->validate())
{
$this->db->where('username',$this->input->post('username'));
$get_profile_info = $this->db->get('memberships');
if($get_profile_info->num_rows() > 0){
foreach ($get_profile_info->result() as $row)
{
$info = array('firstname' => $row->firstname,
'lastname' => $row->lastname,
'email_address' => $row->email_address
);
}
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'firstname' => $info['firstname'],
'lastname' => $info['lastname'],
'email_address' => $info['email_address'],
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('/site/main_menu');
}}
else if($this->membership_model->validate_admin())
{
echo "admin";
}
else
{
redirect('login');
}
}
The if else if statement is not working correctly. The program test the first condition and if it returns false skips the second condition even if that is TRUE and execute the else statement. I'm not sure what is going worng here.
Refactor your one controller method into different methods - one for Members and one for Admin to start. And because you are calling separate database tables would suggest having a separate model for each.
Because you are interacting with a database table getting the profile information should happen in a model (not the controller).
This is a personal preference but i would set the session data in a model as well. Also there might be issues with your foreach and then getting the value $info['first name'].
Validate the form data first before sending to your database. Its safer and its better for your users - if they forget to put in the password you want to show them the form again with a helpful message. http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
and remember when in doubt -- echo it out.

Codeigniter return variables from model to controller

I am in the process of creating an activation email for my registration process. I need to pass the variables containing the activation code and email address from my model back to my controller where I will send the email.
My registration form currently writes all signup data to the database and creates and activation code.
How can you pass the variables 'activation_code' and 'email' from my model back to my controller for use in my email.
Model:
...
$new_member_insert_data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'email_address' => $email,
'password' => hashPassword($salt, $password, $hash),
'activation_code' => activationCode($email, $hash.$salt),
'hash' => $hash
);
$insert = $this->db->insert('members', $new_member_insert_data);
return $insert;
}
Controller
$this->load->model('members_model');
if($this->members_model->create_member())//return variables here somehow
{
//get activation code + email variables
//send activation email
$this->session->set_flashdata('success', 'success');
redirect('home', 'location');
}
else
{
$viewdata['main_content'] = $category;
$this->load->view('includes/template', $viewdata);
}
You could simply return the array of values on success, or FALSE on failure:
$insert = $this->db->insert('members', $new_member_insert_data);
if ($insert) {
return $new_member_insert_data; // Same data
}
else {
return FALSE;
}
...but it doesn't really make sense, as you must be passing something to the model's method in order for it to be useful (or have access to some of these values beforehand, your given code is incomplete). Also, it can be confusing returning different data types and usually a bad idea.
Despite not having access to your full code here, I think the safest and most accurate way may be running another query after the INSERT runs. For instance, looking up the user by email_address (assuming it's supposed to be unique).

Codeigniter help with user registration script

:) This question is for anyone familiar with the Tankauth plugin.
I edited tankauth so that user profiles are populated without email validation.
In the registration form I added and validated two more fields named “first_name” and “last_name” which I want to add to the user_profiles table when the form is submitted.
Below is the tankauth function that adds the user id to the user_profiles table if user is activated. I commented out if ($activated). The problem is that I don’t know how to insert my new fields first_name and last_name to the db. I keep getting errors for anything I try.
function create_user($data, $activated = TRUE)
{
$data['created'] = date('Y-m-d H:i:s');
$data['activated'] = $activated ? 1 : 0;
if ($this->db->insert($this->table_name, $data)) {
$user_id = $this->db->insert_id();
//if ($activated)
$this->create_profile($user_id);
return array(
'user_id' => $user_id,
'first_name' => $first_name, //added by me
'first_name' => $first_name, //added by me
);
}
return NULL;
}
The above is connected with
private function create_profile($user_id)
{
$this->db->set('user_id', $user_id);
$this->db->set('first_name', $first_name); //added by me
return $this->db->insert($this->profile_table_name);
}
Can someone please share some guidance?
Try removing the private in private function create_profile
Also you might want to change your create_profile function to be:
function create_profile( $user_id ) {
$data = array(
'user_id' => $user_id,
'first_name' => $first_name // Hopefully this is being set correctly.
);
return $this->db->insert( $this->profile_table_name, $data );
}
If none of those work. Try using echo $this->db->last_query();
Hope that helps.

Categories