I have Two Controller.
1.Login Controller
2. NewsLine Controller
Login Controller Below Code:
$query = $this->login_model->select_login($_POST);
if ($query) {
$user = array(
'uname' => $query['uname'],
'pwd' => $query['pwd']
);
$this->session->set_userdata($user);
$_SESSION['id'] = $user['id'];
redirect('Newsline');
}
My Question is How To $_SESSION['id] pass to Newsline Controller
As mentioned in CodeIgniter documentation to set the session data you an do following,
$newdata = array(
'username' => 'uname',
'email' => 'uname#some-site.com'
);
$this->session->set_userdata($newdata);
Now this is how you an retrive the data,
$session_id = $this->session->userdata('email');
In your case your code should look like this,
if ($query) {
$user = array(
'uname' => $query['uname'],
'pwd' => $query['pwd'],
'id' => $query['id']
);
$this->session->set_userdata($user);
redirect('Newsline');
}
To retrive ID on NewsLine controller,
$session_id = $this->session->userdata('id');
Related
So i'm trying to learn using codeigniter 3, and i'm trying to get the id from users table, the name of the id column on users table is user_id, and i have another table called lowongan, it has id_user to get the id(user_id) from the users table. So when i tried to input and submit that input to be saved into the database, the id_user is null, but i already set it like this
$id_user = $this->session->userdata('user_id');
$data = array(
'id_user' => $id_user
);
But it's still null how can i fix this??
The model :
<?php
class M_lowongan extends CI_Model{
public function show_lowongan(){
return $this->db->get('lowongan');
}
public function input_data($data, $table){
$this->db->insert($table, $data);
}
}
The Controller :
public function store_lowongan(){
$id_user = $this->session->userdata('user_id');
$title = $this->input->post('title');
$lokasi = $this->input->post('lokasi');
$level_pekerja = $this->input->post('level_pekerja');
$pengalaman_kerja = $this->input->post('pengalaman_kerja');
$pendidikan = $this->input->post('pendidikan');
$alamat = $this->input->post('alamat');
$no_wa = $this->input->post('no_wa');
$no_telp = $this->input->post('no_telp');
$min_gaji = $this->input->post('min_gaji');
$max_gaji = $this->input->post('max_gaji');
$job_desc = $this->input->post('job_desc');
$data = array(
'id_user' => $id_user,
'title' => $title,
'lokasi' => $lokasi,
'level_pekerja' => $level_pekerja,
'pengalaman_kerja' => $pengalaman_kerja,
'pendidikan' => $pendidikan,
'alamat' => $alamat,
'no_wa' => $no_wa,
'no_telp' => $no_telp,
'min_gaji' => $min_gaji,
'max_gaji' => $max_gaji,
'job_desc' => $job_desc
);
$this->m_lowongan->input_data($data, 'lowongan');
redirect ('dashboard');
}
do you already create the session? If not, you can create like this:
$row = $this->Auth_model->get_by_username($this->input->post('username'));
$session = array(
'id_users' => $row->id_users,
'name' => $row->name,
'username' => $row->username,
'email' => $row->email,
'usertype' => $row->usertype,
'photo' => $row->photo,
'photo_thumb' => $row->photo_thumb,
);
$this->session->set_userdata($session);
After that you can easily call the session like:
$this->session->name
public function register() {
if (isset($_POST['register'])) {
$u = $_POST['uname'];
$this->load->database();
$this->load->database();
$this->db->select('uname');
$this->db->from('login');
$this->db->where(array('uname' => $u));
$query1 = $this->db->get();
if (!$query1->num_rows() == 1) {
$data = array(
'fname' => $_POST['fname'],
'lname' => $_POST['lname'],
'dob' => $_POST['dob'],
'gender' => $_POST['gender'],
'email' => $_POST['email']
);
$this->load->database();
$this->db->insert('user', $data);
$data1 = array(
//'User_idUser'=>$_POST[$query1],
'uname' => $_POST['uname'],
'upass' => $_POST['upass']
);
$this->db->insert('login', $data1);
} else {
$_SESSION["ex"] = "User All Ready Exists";
}
}
$this->load->view('register');
}
I want to get auto increment id of user table to save in login table as a foreign key to identify which user is currently logging. the code is running perfectly.
the user table is a parent table and
login table is a child table.
User following code to get ID of last insert operation.
$insert_id = $this->db->insert_id();
And use $inser_id to add it to any table you want.
So if I open the commented line of your code,
$data1 = array(
'User_idUser' => $insert_id, // Assuming that UseridUser is your columne to store the Autoincremented user id from previous insert query.
'uname' => $_POST['uname'],
'upass' => $_POST['upass']
);
I have a problem in this part of my code, where i want to insert it my model named user_model with a function of checkUser2. How can i insert this data from my database, i have no error but i cannot var_dump the data that i want to insert into my database.
public function facebook_request(){
$this->load->library('facebook/src/facebook', array('appId' => '1027885817316593', 'secret' => '6db175699897b514bf4881955b2944bb'));
$this->user = $this->facebook->getUser();
if ($this->user) {
$data['user_profile'] = $this->facebook->api('/me?fields=id,first_name,last_name,email,link,gender,locale,picture');
// Get logout url of facebook
$data['logout_url'] = $this->facebook->getLogoutUrl(array('next' => base_url() . 'index.php/oauth_login/logout'));
$data = array(
'oauth_provider'=> 'facebook',
'oauth_uid' => $data['user_profile']['id'],
'first_name' => $data['user_profile']['first_name'],
'last_name' => $data['user_profile']['last_name'],
'email' => $data['user_profile']['email'],
'gender' => $data['user_profile']['gender'],
'locale' => $data['user_profile']['locale'],
'picture' => $data['user_profile']['picture']['data']['url'],
'link' => $data['user_profile']['link']
);
$userData = $this->user_model->checkUser2($data);
var_dump($data);
// Send data to profile page
$this->load->view('admin/profile.php', $data);
}
else {
// Store users facebook login url
$data['login_url'] = $this->facebook->getLoginUrl();
$this->load->view('auth/profile.php', $data);
}
with model :
function checkUser2($userData){
$this->db->insert('tbl_oauth', $userData);
return $this->db->insert_id();
}
here is my table screenshot details:
I have to create a REST API for mobile backend using CakePHP. After following the instructions here I have been able to setup the requirements for creating a REST API. The problem is that I am getting a null in my output when I use the _serialize variable. Here is my code:
class InvitationController extends AppController {
public $components = array('RequestHandler');
public function index() {
}
public function add() {
if ($this->request->is('post')) {
$this->loadModel('Organizer');
$numPeople = $this->request->data['numpeople'];
$share = $this->request->data['myshare'];
$organizer = $this->request->data['organizer'];
$organizerMobile = $this->request->data['organizermobile'];
$this->set('organizerMobile', $organizerMobile);
$deadline = $this->request->data['deadline'];
$links = array();
date_default_timezone_set("Asia/Calcutta");
$now = date('Y-m-d');
$lastOrganizer = $this->Organizer->find('first', array(
'order' => array('Organizer.id' => 'desc')
));
$checkOrganizer = $this->getOrganizerDetails($lastOrganizer);
$pass = //something;
// save data in organizers table
$this->organizer->create();
$this->organizer->save(
array(
'name' => $organizer,
'mobile' => $organizerMobile,
'p_id' => 1,
'share' => $share,
'deadline' => $deadline,
'password' => $pass,
'group_cardinality' => $numPeople,
'created_on' => $now,
)
);
$message = 1;
$this->set(array(
'message' => $message,
'_serialize' => array($message)
));
}
}
}
When I make a request to POST /invitation.json I get null in the ouput with status 200. On the other hand if I simply do echo json_encode($message) in the Controller or in the View I get the correct output. I think I am doing something wrong. Please help!
That's wrong:
'_serialize' => array($message)
Pay attention to the manual, it shows it correctly in the provided examples.
'_serialize' => array('message')
I'm running a 2.2.2 CakePHP Application, everything works as desired. Now I'm developing a Android App for it and therefore need to create the interfaces between those two apps. That's why I need to login users manually. So I created a whole new controller, the AndroidController, in order to bundle everything at one place. First thing to do would be the Login-Action. So I setup the following controller:
<?php
App::uses('AppController', 'Controller');
/**
* Android Controller
*
* #package app.Controller
*/
class AndroidController extends AppController {
public $components = array('RequestHandler','Auth');
public $uses = array('User');
public function beforeFilter() {
$this->Auth->allow();
}
public function login() {
//For testing purposes
$postarray = array('_method' => 'POST','data' => array('User' => array('email' => 'user#gmail.com', 'password' => 'THISisDEFINITELYaWRONGpassword')));
$id = $this->tryToGetUserID($postarray['data']['User']['email']);
if($id == 0){
//return Error json, unknown User
$this->set('result', array(
'tag' => 'login',
'success' => 0,
'error' => 1,
'error_msg' => 'Unknown User'
));
}else{
// if ($this->request->is('post')) {
$postarray['data']['User'] = array_merge($postarray['data']['User'], array('id' => $id));
$this->User->id = $id;
if ( $this->Auth->login($postarray['data']['User'])) {
// Login successfull
$this->User->saveField('lastlogin', date(DATE_ATOM));
$user = $this->User->find('all', array(
'recursive' => 0, //int
'conditions' => array('User.id' => $id)
));
$loggedInUser = array(
'tag' => 'login',
'success' => 1,
'error' => 0,
'uid' => '??',
'user' => array(
'name' => $user['0']['User']['forename'].' '.$user['0']['User']['surname'],
'email' => $user['0']['User']['email'],
'created_at' => $user['0']['User']['created'],
'updated_at' => $user['0']['User']['lastlogin']
)
);
$this->set('result', $loggedInUser);
} else {
// Login failed
$this->set('result', array(
'tag' => 'login',
'success' => 0,
'error' => 2,
'error_msg' => 'Incorrect password!'
));
}
// }
}
}
public function tryToGetUserID($email = null) {
$user = $this->User->find('list', array(
'conditions' => array('User.email' => $email)
));
if(!empty($user)){
return array_keys($user)['0'];
}else{
return 0;
}
}
}
You need to know that this method will be called as a POST request, but for testing purposes I manually created a post-array. In future I will use the $_POST array.
So, what happens: The Login with a registered user works, but it works every time! Even though the password is wrong or missing! The program never reaches the part in code with the "Login failed" comment.
Am I missing something here..?
Thank you!
If you take a closer look at the documentation you will notice that AuthComponent::login() will ...
In 2.x $this->Auth->login($this->request->data) will log the user in with whatever data is posted