token generate for forgot password - codeigniter

I create forgot password module.
MY Question: How to generate token for forgot password using codeigniter. i used codeignitor framework. below code
Controller:
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('user_model');
$this->load->library('session');
}
public function forgotpassword() {
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_emails');
if ($this->form_validation->run() == FALSE) {
$data = array(
'page_title' => 'Forgot Password',
'page_name' => 'user/forgotpassword'
);
$this->load->view('user_template', $data);
} else {
$result = $this->user_model->forgotpassword($_POST);
if (!empty($result)) {
echo '<h1 class="text-center">Thank You</h1>';
}
}
}
}
Model:
class User_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function forgotpassword($data) {
return $this->db->get_where('user', array('email' => $data['email']))->row_array();
}
}
View:
<section class="container">
<section class="login-form">
<?php echo form_open('User/forgotpassword'); ?>
<section>
<h2><span style="color: red">For</span>The<span style="color: red">Love</span>Of<span style="color: red">Food</span>Trucks</h2>
</section>
<div class="text-danger">
<?php echo validation_errors('<li>', '</li>'); ?>
</div>
<input type="hidden" name="token" value=" <?php echo sha1(date("Y/m/d h:i:sa")); ?>">
<label>Email</label>
<input type="text" name="email" id="email" class="form-control" placeholder="Email Address" value="<?php echo set_value('email'); ?>"required=""/>
<br/>
<button type="submit" name="submit" class="btn btn-block btn-danger">Submit</button>
<?php echo form_close(); ?>
</section>
</section>
Please Help me..................................................................................................

There are many ways to generate a token using PHP here is one of them:
$forgotten_code = sha1(uniqid(rand()));
Please note that this is just the answer to your question on 'how to generate the code' but the process involving the recover of the password is more complicated then you might think.
I can advise you to use a class such as ion auth 2 which is written to be used with Codeigniter and you can findit here:
https://github.com/benedmunds/CodeIgniter-Ion-Auth
This class will cover all the user authentication, login, password recovery and much more:
You do not need to reinvent the weel
By the way you have a typo here:
'Email Address', 'required|valid_emails'
You need to correct it like this:
'Email Address', 'required|valid_email'

Related

How to hide data shown in the url - laravel

I have created a login page in which user login in with their credentials i.e patientId and contactNumber but after being logged in, the CSRF token is also displaying login credentials along with the token.Also I am using APIs for login and other stuff.
This is the output I am getting:
http://127.0.0.1:8000/login1?_token=BugYniw96HnJ6C8gjjcpzSruW0CwDdq8JW7kD7Oz&patientId=33488&contactNumber=08732837489
This is my login blade file:
<form method="GET" action="{{route('login1')}}" name="myForm" class="login100-form validate-form" >
<input type="hidden" name="_token" value="{{ csrf_token()}}">
<span class="login100-form-title">
User Login
</span>
<div class="wrap-input100 validate-input" data-validate="Mr.No is required">
<input class="input100" name="patientId" id="patientId" placeholder="Enter MR Number" >
<span class="focus-input100"></span>
<span class="symbol-input100">
<i class="fa fa-user" aria-hidden="true"></i>
</span>
</div>
<div class="wrap-input100 validate-input" data-validate="Contact Number is required">
<input class="input100" name="contactNumber" id="contactNumber" placeholder="Enter Contact Number">
<span class="focus-input100"></span>
<span class="symbol-input100">
<i class="fa fa-lock" aria-hidden="true"></i>
</span>
</div>
<div class="container-login100-form-btn">
<button class="login100-form-btn" type="submit">
Login
</button>
</div>
<div class="text-center p-t-136">
<a class="txt2" href="#">
</a>
</div>
</form>
This is a web route file:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MainController;
use App\Http\Middleware\VerifyCsrfToken;
Route::get('/', function () {
return view('login1');
});
Route::get('/login1', [MainController::class, 'successlogin'])->name('login1');
This is my controller file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\SessionClass;
use Illuminate\Support\Facades\Http;
use App\Http\Controllers\HostClass;
use Illuminate\Support\Facades\Session;
class MainController extends Controller
{
public function successlogin(Request $req)
{
$host = new HostClass();
$obj = new SessionClass();
$obj->sethalfpatientId($req->patientId);
$response = Http::post($host->getserverIp().'/patientInformation',[
"patientId"=> $req->patientId,
"contactNumber"=> $req->contactNumber,
"orgId"=>"332",
"sessionId"=> "3"
]);
$data = json_decode($response, true);
if($data == null){
echo "error";
$notification = array(
'message' => 'User Does not Exists!',
'alert-type' => 'error'
);
return back()->with($notification);
}
else{
$obj->setpatientId($data['patientId']);
$obj->setcontactNumber($data['contactNumber']);
$response2 = Http::post($host->getserverIp().'/searchPatientReports',[
"patientId"=> $obj->getpatientId(),
"departmentId"=> "128"
]);
$data2 = json_decode($response2, true);
$response3 = Http::post($host->getserverIp().'/patientVisits',[
"patientId"=> $obj->getpatientId()
]);
$data3 = json_decode($response3, true);
Session::put('user', $data);
$listappointment = ($data['listAppointments']);
return view('dashboard', compact(['data','data2','data3','listappointment']));
}
}
use POST method so the data dont show in the url
<form method="POST" action="{{route('login1')}}" name="myForm" class="login100-form validate-form" >
And change the route to accept post method
Route::post('/login1', [MainController::class, 'successlogin'])->name('login1');

MethodNotAllowed Exception on form submit

routes.php
use App\Http\Controllers\Task;
use Illuminate\Http\Request;
Route::get('/', function () {
$tasks = Task::orderBy('created_at', 'asc')->get();
return view('tasks', [
'tasks' => $tasks
]);
});
Route::get('Login', 'Login#index');
View: loginform.blade.php
<form method="post" action="http://localhost/blog/public/Login">
<!-- Task Name -->
<div class="form-group">
<div class="col-sm-6">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
{!! Form::label('usernamelabel','Username', ['class'=>'col-sm-3 control-label']) !!}
{!! Form::text('username', '', ['class'=>'form-control','id'=>'username']) !!}
</div>
<div class="col-sm-6">
{!! Form::label('passwordlabel', 'Password', ['class'=>'form-control control-label']) !!}
{!! Form::text('password', '', ['class'=>'form-control','id'=>'password']) !!}
</div>
</div>
<!-- Add Task Button -->
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button type="submit" class="btn btn-default">
<i class="fa fa-plus"></i> Login
</button>
</div>
</div>
{!! Form::close() !!}
Controller: Login.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Models\userloginModel;
use Illuminate\Http\Request;
class Login extends Controller{
protected $request;
public function index(Request $request)
{
echo view('login.loginform');
$foo = new userloginModel();
echo $foo->username = $request->username;
echo $foo->password = $request->password;
}
}
I have try all solutions from Stackoverflow and laracast but i failed to solve this please some one help me from this i am new with laravel..
Your error is in method, u trying to make a post request and your route are receiving a get request, try this:
Route
<?php
//...
Route::get('Login', 'Login#index');
Route::post('Login', 'Login#login');
?>
Controller
<?php
//...
public function index()
{
return view('login.loginform');
}
public function login(Request $request)
{
$foo = new userloginModel();
echo $foo->username = $request->username;
echo $foo->password = $request->password;
}
?>

Display error message from Admin_Controller to any view

I would like to be able to display data message from my Admin_Controller in the core folder core/Admin_Controller.php to then show up on my login view.
I can only seem to get it working with session flash data but would not like to use flash data.
So what would be best method on getting the $data['error_warning'] message to from my Admin_Controller to be able to work on my Login controller and view.
<?php
class Admin_Controller extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->library('user');
Modules::run('admin/error/permission/check');
$ignore = array(
'login',
'logout'
);
if (!in_array($this->router->fetch_class(), $ignore)) {
if ($this->session->userdata('user_id') == FALSE) {
$data['error_warning'] = 'You have tried to directly access controller without logging on! Please login.';
redirect('admin');
}
}
}
}
Login Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends Admin_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}
public function index() {
$data['title'] = 'Administration';
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$username = $this->input->post('username');
if (isset($username)) {
$data['username'] = $username;
} else {
$data['username'] = '';
}
$password = $this->input->post('password');
if (isset($password)) {
$data['password'] = $password;
} else {
$data['password'] = '';
}
$this->form_validation->set_rules('username', 'Username', 'required|callback_validate');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run($this) == FALSE) {
$this->load->view('template/common/login.tpl', $data);
} else {
redirect('admin/dashboard'.'/'.$token);
}
}
public function validate() {
$this->load->library('user');
if ($this->user->login() == FALSE) {
$this->form_validation->set_message('validate', '<i class="fa fa-exclamation-triangle"></i> Does not match any of our database records!');
return false;
} else {
return true;
}
}
}
Login View
<?php echo Modules::run('admin/common/header/index');?>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1 col-xs-12">
<div class="panel panel-default" style="margin-top: 12.5%;">
<div class="panel-heading"><strong><i class="fa fa-key"></i> Enter Details To Login </strong></div>
<div class="panel-body">
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php $data = array('class' => 'form-horizontal');?>
<?php echo form_open('admin', $data);?>
<?php if ($error_warning) { ?>
<div class="alert alert-danger text-center"><i class="fa fa-exclamation-triangle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<?php if ($this->session->flashdata('error')) { ?>
<div class="alert alert-danger text-center"><i class="fa fa-exclamation-triangle"></i> <?php echo $this->session->flashdata('error'); ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('Username', 'username', $data);?>
<div class="col-sm-10">
<?php
$data_username = array(
'id' => 'username',
'name' => 'username',
'class' => 'form-control',
'placeholder' => 'Username',
'value' => $username
)
;?>
<?php echo form_input($data_username);?>
</div>
</div>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('Password', 'password', $data);?>
<div class="col-sm-10">
<?php
$data_password = array(
'id' => 'password',
'name' => 'password',
'class' => 'form-control',
'placeholder' => 'Password',
'value' => $password
)
;?>
<?php echo form_password($data_password);?>
</div>
</div>
<div class="text-right">
<button type="submit" class="btn btn-primary">Login Now</button>
</div>
</div>
<div class="panel-footer text-right">
Not registered on admin ?
click here
<?php echo form_close();?>
</div>
</div>
</div>
</div>
<?php echo Modules::run('admin/common/footer/index');?>
Try defining a class properties in your Admin_Controller, like this:
<?php
class Admin_Controller extends MX_Controller {
// Change here
protected $ignore;
protected $data;
public function __construct() {
parent::__construct();
$this->load->library('user');
Modules::run('admin/error/permission/check');
$this->ignore = array(
'login',
'logout'
);
if (!in_array($this->router->fetch_class(), $ignore)) {
if ($this->session->userdata('user_id') == FALSE) {
$this->data['error_warning'] = 'You have tried to directly access controller without logging on! Please login.';
redirect('admin');
}
}
}
}
Also, as above, change each reference to either $data or $ignore to be $this->data or $this->ignore. You'll also need to do this inside your Login class too. Always, when referencing class properties (that is, variables declared outside a method within a class), you do so by $this->PROPERTY_NAME.

form_submit codeigniter not working

So I am having trouble with the form_submit in codeigniter. I have a controller named welcome with a method named email. Whenever I hit my submit button the page refreshes but then just appends ?firstname=&email=&message=&submit=Submit to my url but doesn't carry out the method. Any reason this may be happening?
below is my html code.
<form role="form">
<div class="form-group">
<label>Name</label>
<?php $this->load->helper("form"); ?>
<?php echo validation_errors('<p class = "error">'); ?>
<?php echo form_open('welcome/email');
$data = array('type'=>'text','class'=>'form-control', 'name'=>'firstname');
echo form_input($data);
?>
</div>
<div class="form-group">
<label>Email</label>
<?php
$data = array('type'=>'email','class'=>'form-control', 'name'=>'email');
echo form_input($data);
?>
</div>
<div class="form-group">
<label>Message</label>
<?php
$data= array('type'=>'text','name'=>'message','class'=>'form-control','rows'=>7);
echo form_textarea($data);
?>
</div>
<div class="pull-right">
<!--<button type="submit" class="btn btn-custom btn-lg" action="welcome/email">Submit</button> -->
<?php
echo form_submit('submit','Submit');
echo form_close();
?>
</div>
This is my controller
<?php /*if ( ! defined('BASEPATH')) exit('No direct script access allowed');*/
class Welcome extends CI_Controller {
function index(){
$this->load->view('index-sidebar');
}
function email(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email Address','required|valid_email');
$this->form_validation->set_rules('firstname', 'Name', 'required|min_length[2]|max_length[30]');
$this->form_validation->set_rules('message', 'Message', 'required|min_length[5]|max_length[200]');
if ($this->form_validation->run()==FALSE){
$this->load->view('message not sent');
}
else{
$this->load->library('email');
$this->email->from(set_value('email'),set_value('name'));
$this->email->to("mhansen1989#gmail.com");
$this->email->subject('tutoring');
$this->email->message(set_value('message'));
$this->email->send();
echo $this->email->print_debugger();
$this->load->view('success');
}
}
}
there is no action parameter in your form tag.how do you submit the form?
try this
<form action="<?= base_url().'welcome/email'?>" role="form" method="post">
so here
if ($this->form_validation->run()==FALSE){
$this->load->view('message not sent');
}
put in your correct view name where it says 'message not sent'
$this->load->helper("form");
load your helpers in the controller -- better yet do it config/autoload
and this
$data = array('type'=>'text','class'=>'form-control','name'=>'firstname');
echo form_input($data);
in the form you repeat $data over and over. technically it will work but its going to mess up at some point so make them different names
$first = array('type'=>'text','class'=>'form-control','name'=>'firstname');
echo form_input($first);
and you need something to show the values in the form again if the validation is false. check out http://www.codeigniter.com/user_guide/libraries/form_validation.html#repopulatingform

Form Validation Showing 2 Callback Messages

I am trying to set my own callback message for codeigniter. But when I try to make my own it will show both messages codeigniter one and my custom one. Here is a link to my errors are showing up the top error is one that I would like to use http://postimg.org/image/v9ilejmnt/
How am I able to make it only show my custom error message?
I also use hmvc so I have had to add run($this) in the form validation to make callback work.
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('user');
$this->load->library('form_validation');
$this->lang->load('common/login', 'english');
if($this->session->userdata('user_id')) {
redirect('dashboard');
} else {
return false;
redirect('login');
}
}
public function index() {
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]|xss_clean|callback_validate');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
if($this->form_validation->run($this) == false) {
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['action'] = site_url('login');
$this->load->view('common/login', $data);
} else {
redirect('dashboard');
}
}
function validate() {
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->user->login($username, $password)) {
return true;
} else {
$this->error['warning'] = $this->lang->line('error_login');
return !$this->error;
}
}
}
View
<?php echo modules::run('common/header/index');?>
<div class="container">
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-md-4 col-md-offset-4 col-sm-offset-4 col-sm-4">
<div class="panel panel-default">
<div class="panel-heading"><h2 class="panel-title">Administration Login</h2></div>
<div class="panel-body">
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<form action="<?php echo $action;?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-user"></i> </span>
<input type="text" name="username" value="" placeholder="Username" class="form-control" size="50" />
</div>
<?php echo form_error('username', '<div class="text-danger">', '</div>'); ?>
</div>
<div class="form-group">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" name="password" value="" placeholder="Password" class="form-control"/>
</div>
<?php echo form_error('password', '<div class="text-danger">', '</div>'); ?>
</div>
<div class="form-group">
<div class="text-right">
<button type="submit" class="btn btn-primary"><i class="fa fa-key"></i> Login</button>
</div>
</div>
</form>
</div><!--/. Panel Body -->
</div><!--/. Panel Panel Default -->
</div>
</div>
</div>
<?php echo modules::run('common/footer/index');?>
you forgot to set error message for your your callback.
$this->form_validation->set_message('validate', 'invalid username or password');
return FALSE;
or you can do
$this->form_validation->set_message('validate', $this->lang->line('error_login'));
return FALSE;
in your view file you are showing errors for each fields (CI form validation messages) as well as your own <?php echo $error_warning; ?>
you should use following code
function validate() {
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->user->login($username, $password)) {
return true;
} else {
$this->form_validation->set_message('validate', $this->lang->line('error_login'));
return FALSE;
}
and remove this block from view
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
}
and remove this too
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
I worked it out today. When want to use both validation methods custom and form validation can not use callbacks.
public function index() {
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
if($this->form_validation->run($this) == false) {
$data['title'] = $this->lang->line('heading_title');
$data['text_heading'] = $this->lang->line('text_heading');
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['action'] = site_url('login');
$this->load->view('common/login', $data);
} else {
if($this->validate()) {
redirect('dashboard');
} else {
$data['title'] = $this->lang->line('heading_title');
$data['text_heading'] = $this->lang->line('text_heading');
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['action'] = site_url('login');
$this->load->view('common/login', $data);
}
}
}

Resources