I m trying to set Session in codeigniter. when a person try to login with email id and password.
1st) using email id find his/her role. then return that result in session and redirect to respective link. the code I typed below. Could you please solve this issue?
Controller
public function login_validation() {
$this -> load -> library('form_validation');
$this -> form_validation -> set_rules('email', 'Email', 'required|trim');
$this -> form_validation -> set_rules('password', 'Password', 'required ');
if ($this -> form_validation -> run()) {
$this -> load -> model('model_database');
$sessiondata['var_userrole'] = $this -> model_database -> login_session();
$this -> session -> set_userdata($sessiondata);
if ($var_userrole == admin) {
redirect('welcome/admin');
} else if ($var_userrole == staff) {
redirect('welcome/staff');
} else if ($var_userrole == student) {
redirect('welcome/student');
} else {
redirect('welcome/login');
}
Model
public function login_session() {
$email = $this -> input -> post('email');
$login_id = $this -> db -> query("SELECT var_userrole FROM tbl_login where var_email = '" . $email . "'");
return $login_id -> row_array();
}
hi first of all $var_userrole is not defined anywhere which you are checking in if statements
Controller
public function login_validation() {
$this -> load -> library('form_validation');
$this -> form_validation -> set_rules('email', 'Email', 'required|trim');
$this -> form_validation -> set_rules('password', 'Password', 'required ');
if ($this -> form_validation -> run()) {
$this -> load -> model('model_database');
$var_userrole = $this -> model_database -> login_session();// was undefined
$sessiondata['var_userrole'] = $var_userrole
$this -> session -> set_userdata($sessiondata);
if ($var_userrole == admin) {
redirect('welcome/admin');
} else if ($var_userrole == staff) {
redirect('welcome/staff');
} else if ($var_userrole == student) {
redirect('welcome/student');
} else {
redirect('welcome/login');
}
Model
public function login_session() {
$email = $this -> input -> post('email');
$login_id = $this -> db -> query("SELECT var_userrole FROM tbl_login where var_email = '" . $email . "'");
//$row = $login_id -> row() // as object return $row->var_userrole;
$row = $login_id -> row_array();
return $row['var_userrole']
}
well if you are developing something new for auth before that check some auth system in CI like ion_auth has group which you can mange and check user from which group its belong Ion auth
Related
I got this problem trying to get id by comparing user input with data in database. I am new in this and I tried some solution which not help at all.
Below is my controller code:
public function assign(Request $request)
{
$courselist = new Courselist;
$coursecode = $request->input('coursecode');
$courseid = DB::select("select id from course where course_code = '$coursecode'");
$courselist -> course_id = $courseid;
$courselist -> coordinator = $request->input('coordinatorname');
$courselist -> internal = $request->input('imname');
$courselist -> external = $request->input('exname');
$courselist -> dean = $request->input('deanname');
$courselist -> save();
return back()-> with('alert-success','Data successfully added!');
}
$course = DB::table("your table name")->where("course_code ", "=", $coursecode)->first();
$courselist -> course_id = $course->id;
try this i hope it will work.
In my user table i have a role column its values Teacher and Student. I want to insert data if user role teacher. Otherwise give a message "Only for teacher".
Here is my controller:
$validator = Validator::make($request->all(), [
'title' => 'required|exists:users,role==[Teacher]',
]);
if ($validator->fails()) {
Flash::success('Only for Teacher');
return redirect(route('works',['class_id'=>$class_id]));
}else{
$works = new assainments();
$works -> title = $text;
//$works -> file = $fileName;
$works -> class_id = $class_id;
$works -> users_id = Auth::user()->id;
$works -> save();
Flash::success('Your works has been posted');
return redirect(route('works',['class_id'=>$class_id]));
But it's not working. It's only Show the error message "Only for Teacher"
laravel 5.2
The exists validator rules work given table , column and extra where conditions. See the more definition :
https://laravel.com/docs/5.2/validation#rule-exists
But you can solve this on another way by checking a if condition :
if( \Auth::user()->role == 'Student' )
{
Flash::success('Only for Teacher');
return redirect(route('works',['class_id'=>$class_id]));
}
I am trying to send an email through email function in email_model, but email is not being sent
class email_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function email($name) {
$this->db->select('*');
$this->db->from('dna_crm_emails');
$this->db->where('email_name',$name);
$query = $this->db->get();
return $query->result();
}
}
Any reference or help will be much appreciated.
Regards
The email function in email_model just retrieve data upon given email_name
what do you need is to send mail through mail class as Mudshark mentioned
you need to create a function at your controller , it'll be like this :
function sendMail($sender_email, $sender_name, $target_email, $subject, $message_data) {
$this -> load -> library('email');
// whatever email you want it to be as a sender
//just make sure it end with your domain name to not get blocked
// ex : 'noreply#yourdomain.com','Support'
$this -> email -> from($email, $sender);
$this -> email -> to($target_email);//the email you're targeting
$this -> email -> subject($subject); // message subject
$message = "This is message data : ". $message_data;
$this -> email -> message($message); // message body as plain text
$this -> email -> send();
}
you could use more advanced options though like sending mail as html , by creating a view message.php with designed html for your email message and parsing an array of data to it.
your function would be like this :
function sendMail($sender_email, $sender_name, $target_email, $subject, $message_data) {
$this -> load -> library('email');
$config['mailtype'] = 'html';
$this -> email -> initialize($config); // to define email as html type
$this -> email -> from($email, $sender);
$this -> email -> to($target_email);//the email you're targeting
$this -> email -> subject($subject); // message subject
$this->email->message( $this->load->view( 'message', $message_data, true )); //html message
$this -> email -> send();
}
How do you get the user profile after you have finish verification in linkedin? I've been googling around and found some tutorials but none of them are working, or should I say I can't get them to work. Below is my controller.
function __construct(){
parent::__construct();
$this->config->load('linkedin');
$this->data['consumer_key'] = $this->config->item('api_key');
$this->data['consumer_secret'] = $this->config->item('secret_key');
$this->data['callback_url'] = site_url() . 'main/linkedin_display';
}
function linkedin_request(){
$this->load->library('linkedin', $this -> data);
$token = $this->linkedin->get_request_token();
$oauth_data = array(
'oauth_request_token' => $token['oauth_token'],
'oauth_request_token_secret' => $token['oauth_token_secret']
);
$this->session->set_userdata($oauth_data);
$request_link = $this->linkedin->get_authorize_URL($token);
header("Location: " . $request_link);
}
function linkedin_display{
// get user details(first name, email etc) here
}
Add the following function to your linkedin library, in order to get data
function getData($url, $access_token)
{
$request = OAuthRequest::from_consumer_and_token($this->consumer, $access_token, "GET", $url);
$request->sign_request($this->method, $this->consumer, $access_token);
$auth_header = $request->to_header("https://api.linkedin.com");
$response = $this->httpRequest($shareUrl, $auth_header, "GET");
return $response;
}
Then create a function in your controller as follows:
/**
* Fetch linkedin profile
*/
function myprofile()
{
$auth_data = $this->session->userdata('auth');
$this->load->library('linkedin', $this->data);
$status_response = $this->linkedin->getData('http://api.linkedin.com/v1/people/~', unserialize($auth_data['linked_in']));
print_r($status_response);
}
This should work for you.
I have this remap:
public function _remap($method, $params = array()) {
if (method_exists($this, $method)) {
if ($this -> uri -> segment(1)) {
$this -> index($this -> uri -> segment(1));
} else {
$this -> index();
}
} else {
return call_user_func_array(array($this, $method), $params);
}
}
On my local machine it works fine, but on my test-production it only redirects to index() no matter WHAT I input as querystring. Will someone please help me debug this?
The routes.php is equal on both servers.
If i try to change my index function to only output the requested uri like this:
function index($id = null)
{
$this->output->set_output('id: ' . $id);
return;
only "id : " gets output. Hence the $id is lost somewhere in transition, but I really don't know why
SOLUTION
in system/cms/config/config.php I had $config['uri_protocol'] ) 'PATH_INFO', but on my production server, this didn't work, so I changed it to 'AUTO' And it worked.
in system/cms/config/config.php I had $config['uri_protocol'] = 'PATH_INFO', but on my production server, this didn't work, so I changed it to 'AUTO' And it worked.