In database i have username, password, id, admin and etc. but my admin value not show. Why?
http://prntscr.com/ewuiz9
http://prntscr.com/ewuj4o
// Get username
$username = $this->input->post('username');
// Get and encrypt the password
$password = md5($this->input->post('password'));
// Login user
$user_id = $this->user_model->login($username, $password);
if($user_id){
// Create session
$user_data = array(
'admin' => $admin,
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);
$this->session->set_userdata('logged_in', $user_data);
// Set message
$this->session->set_flashdata('user_loggedin', 'You are now logged in');
redirect('posts');
} else {
// Set message
$this->session->set_flashdata('login_failed', 'Login is invalid');
redirect('users/login');
}
}
}
Because you have set it like
$user_data = array(
'admin' => $admin,
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);
$this->session->set_userdata('logged_in', $user_data);
You can access it like on another controller.
$userdata = $this->session->userdata('logged_in');
echo $userdata['username'];
If you just need want to access like
echo $this->session->userdata('username');
Then you just need to set it like
$user_data = array(
'admin' => $admin,
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);
$this->session->set_userdata($user_data);
Make sure you have autoloaded session so can have access to it over website
config/autoload.php
$autoload['libraries'] = array('session');
On config.php make sure you have set the session path and session folder permission 0700
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = APPPATH . 'cache/session/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
Thank you.
In model i found that:
// Log user in
public function login($username, $password){
// Validate
$this->db->where('username', $username);
$this->db->where('password', $password);
$result = $this->db->get('users');
if($result->num_rows() == 1){
return $result->row(0)->id;
} else {
return false;
}
}
i change to:
// Log user in
function login($username, $password){
$this->db->select('id,admin,username,password,email');
$this->db->from('users');
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get();
if($query->num_rows()==1){
return $query->result();
} else{
return false;
}
}
Related
My All Data has been updated successfully but Image not update and old image not remove. I am using Query Builder.
Can anyone tell me what to do?
My Controller are given below
I want to know something new.
public function update(Request $request,$id)
{
$validatedData = $request->validate([
'name' => 'required|max:255',
'title' => 'required',
'details' => 'required',
]);
$data = array();
$data['name'] = $request->name;
$data['title'] = $request->title;
$data['details'] = $request->details;
$data['status'] = $request->status;
$image=$request->photo;
if ($image) {
$image_name = str_random(20);
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name.'.'.$ext;
$upload_path = 'public/upload/event';
$image_url = $upload_path.$image_full_name;
$success = $image->move($upload_path,$image_full_name);
if ($success) {
$data['photo'] = $image_url;
$img = DB::table('events')->where('id',$id)->first();
$image_path = $img->photo;
$done = unlink($image_path);
$user = DB::table('events')->where('id',$id)->update($data);
if ($user) {
$notification = array(
'messege' => 'Data Updated Successfully',
'alert-type' => 'success'
);
return redirect('admin/event')->with($notification);
}else{
return redirect()->back();
}
}
}else{
return redirect()->back();
}
//return redirect('admin/event')->with($notification);
}
I have made session check, I am using:
if ($data!==false) {
$this->load->view("a");
}
But the both code is still running (true and false), and that session also run, so i got my screen was a half code false a half code true :")
Controller
public function aksi_login()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$where = array(
'username' => $username,
'password' => $password
);
$cek = $this->product_model->cek_login("admin",$where)->num_rows();
if($cek > 0){
$data_session = array(
'nama' => $username,
'status' => "login"
);
$this->session->set_userdata($data_session);
redirect(base_url("admin"));
}else{
echo "Username dan password salah !";
}
}
View
<?php if($this->session->userdata("data_session") !== false)
{$this->load->view("login");} ?>
False and true statement is running together so my screen is splited become 2 side.
I am trying to make an API with Passport. If a user tries to login or signs up with Socialite, find user than generate access_token then redirect to the frontend with access_token in URL parameters.
I tried to register and login than generate access_token with user email and the default password, which is not suitable for security.
try {
$serviceUser = Socialite::driver($service)->stateless()->user();
} catch (\Exception $e) {
return redirect(config('app.client_url').'/auth/social-callback?error=Unable to login using '.$service.'. Please try again'.'&origin=login');
}
$email = $serviceUser->getEmail();
$name = $serviceUser->getName();
$user = $this->getExistingUser($serviceUser, $email, $service);
$newUser = false;
if (!$user) {
$newUser = true;
$user = new User;
$user->name = $name;
$user->email = $email;
$user->username = Str::random(10);
if ($service === 'facebook') {
$user->image = $serviceUser->avatar;
}
$user->verify = true;
$user->save();
}
if ($this->needsToCreateSocial($user, $service)) {
Social::create([
'user_id' => $user->id,
'social_id' => $serviceUser->getId(),
'service' => $service
]);
}
$http = new Client;
$response = $http->post(config('app.url').'/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => '2',
'client_secret' => 'oBKWxgF2fDvrxwA05ciapwy4JYKaHxzhGzr6D24X',
'username' => $email,
'password' => 'gebdandi',
'scope' => '',
],
]);
$body = json_decode((string) $response->getBody(), true);
$accessToken = $body['access_token'];
return redirect(config('app.client_url').'/social-callback?token='.$accessToken.'&origin='.($newUser ? 'register' : 'login'));
I can't find any solution in the documentation.
after reading all documentation i found solution in this documentation
i did like this
edit authserviceprovider like this
public function boot()
{
$this->registerPolicies();
Passport::routes();
Passport::personalAccessClientId(1);
Passport::tokensExpireIn(now()->addDays(15));
Passport::refreshTokensExpireIn(now()->addDays(30));
}
added code in controller like this
$accessToken = $user->createToken('access_token')->accessToken;
thanks for laravel Team for provide good documentation
Now I'm developing system documentation using Codeigniter. But the problem is Not logged in Problems switching between pages. login and Welcome To switch on like this forever.
public function index()
{
if($this->input->post(NULL)){
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->m_login->check_login($username,$password);
if(!empty($result)){
$data_account = array();
foreach($result as $row){
$data_account = array(
'account_id' => $row->id,
'username' => $row->user_name,
'password' => $row->password,
'fullname' => $row->fullname,
'email' => $row->email,
'tel' => $row->tel,
'detail' => $row->detail
);
}
$this->session->set_userdata('data_account',$data_account);
$this->__login();
//create_log_activity($this->account_id, date("Y-m-d H:i:s"));
redirect('welcome', 'refresh');
exit;
}else{
$this->load->view('login');
}
}else if($this->session->userdata('data_account')){
redirect('welcome', 'refresh');
exit;
}else{
$this->load->view('login');
}
}
public function logout(){
$this->session->unset_userdata('data_account');
redirect('','refresh');
}
I want to know how to solve the problem, thanks in advance.
Use
if($_SERVER['REQUEST_METHOD'] == "POST"){
instead of
if($this->input->post(NULL)){
I am having trouble, getting the logged in user ID from the database...
This is my controller code :
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if ($query){
$this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
This is my model function where I return the user id from the database:
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users');
foreach ($query->result() as $row)
{
$user_id = $row->id;
}
return $user_id;
}
Everything works perfect, except that $user_id returns empty... What am I doing wrong?
Try:
$user_id= $this->users_model->get_userID($this->input->post('email'));
try modifying the model function to this -
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users')->row_array(); // retrieves only first-row.
return $query['id'];
}
In controller the function needs just a bit of modification as follows -
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if($query){
//$user_id was not assigned before :)
$user_id = $this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
hope it helps,
faisal ahmed
blog / site