In Laravel 4.2, I have declared a session:
session_start();
$code=rand(100000,999999);
$_SESSION["captcha"]=$code;
Then I want to get the data that I have stored, and use it in a Laravel controller to check if the user entered the right code before I enter it inside the database.
Here is my code:
public function store2()
{
// i was trying to get the value
// session_start();
// $getsCapt = $_SESSION["captcha"];
$rules = array(
'username' => 'required|min:2|max:50|regex:/^[a-zA-Z0-9\-\s]+$/',
'description' => 'required|min:1|max:100|regex:/^[a-zA-Z0-9\-\s]+$/',
'usertype' => 'required|numeric'
);
$validator = Validator::make(Input::all(), $rules);
// process the login
if ($validator->fails())
{
return Redirect::to('createsu')
->withErrors($validator)
->withInput(Input::except('password'));
}
else
{
// store
$su = new SystUser;
$su->SystemUserTypeID = Input::get('usertype');
$su->SystemUserName = Input::get('username');
$su->SystemUserPassword = 'changeme';
$su->SystemUserDescription = Input::get('description');
$su->timestamps = false;
$su->save();
// redirect
Session::flash('message', 'Successfully created system user!');
return Redirect::to('createsu');
}
}
In Laravel you don't have to start session explicitly.
You can do something like:
To Set In Session
$captcha='whatever';
Session::put('captcha', $captcha);
To Get From Session
if(Session::has('captcha'){
$captcha = Session::get('captcha');
}
Related
i have a code that works perfectly for login for every mobile users,until a user tries to login but he cant here is the login code,
Note, this happens to only this user
public function MobileLogin(Request $request)
{
error_log($request);
$n = $request->get('username');
$p = $request->get('password');
error_log($p);
error_log($n);
error_log('i got the values');
if (Auth::attempt( array(
'UserName' => $request->get('username'),
'password' => $request->get('password')
) )) {
$user = Auth::user();
$id = Auth::id();
error_log($id); // i am able to see this value on the log which means user is authenticated
$n = 'ok';
return Response::json($user); // but i couldn't get this instead
} else {
$n ='Invalid Username or Password Please Try Again'; // i get this
return Response::json($n);
}
}
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.
I am working in laravel 5.1 and my update profile was working but will not encrypted and not working now.
When I try to update the user table will also password_confirmation field and causes a conflict in the database. I do not understand.
In the form says successfully but the database does not update any
Code
public function updatePassword() {
$passwordData = Input::except('_token');
$validation = Validator::make($passwordData, User::$passwordData);
if ($validation->passes()) {
array_forget($passwordData,'password_confirmation');
User::where(array(
'password' => Hash::make(Input::get('password'))
));
Session::flash('password', 'Perfil editado com sucesso');
return Redirect::to('backend/perfil/password');
} else {
return Redirect::to('backend/perfil/password')->withInput()->withErrors($validation);
}
}
user
public static $passwordData = array(
'password' => 'required|confirmed',
'password_confirmation' => 'required'
);
Follow this simple steps to get rid of anything
Step 1 : Get the password from the form
$PasswordData = Input::all();
Step 2 : Validate your password
Validator::extend('pwdvalidation', function($field, $value, $parameters) {
return Hash::check($value, Auth::user()->password);
});
Step 3 : Define the validation rule in your User Model
public static $rulespwd = array('OldPassword' => 'required|pwdvalidation',
'NewPassword' => 'required|confirmed|alphaNum|min:5|max:10',
'NewPassword_confirmation' => 'required',
);
Note : You shall define your own rule according to your need
Step 4 : If the rule is passed, then update else throw error messages to your view
$validator = Validator::make($PasswordData, User::$rulespwd, $messages);
if ($validator->passes()) {
$user = User::find(Auth::user()->id);
$user->password = Input::get('NewPassword');
$user->save();
return Redirect::to(Session::get('urlpath') . '/changepassword')->withInput()->with('Messages', 'The Password Information was Updated');
} else {
return Redirect::to(Session::get('urlpath') . '/changepassword')->withInput()->withErrors($validator);
}
to make it a bit short. I just made a registration form fully working with a controller, the routes and the view. Now I know it's common sense to use a Model for it and in the controller only call the method in the model. So i thought okay lets fix that. Now when I register an account I get a blank page. I bet the redirect is going wrong but I can't fix it maybe you can?
RegisterController.php
public function doRegister(){
$user = new User();
$user->doRegister();
}
User.php (model)
public function doRegister()
{
// process the form here
// create the validation rules ------------------------
$rules = array(
'username' => 'required|unique:users',
'email' => 'required|email|unique:users',
'password' => 'required|min:5',
'serial_key' => 'required|exists:serial_keys,serial_key|unique:users'
);
// create custom validation messages ------------------
$messages = array(
'required' => 'The :attribute is important.',
'email' => 'The :attribute is not a legit e-mail.',
'unique' => 'The :attribute is already taken!'
);
// do the validation ----------------------------------
// validate against the inputs from our form
$validator = Validator::make(Input::all(), $rules);
// check if the validator failed -----------------------
if ($validator->fails()) {
// get the error messages from the validator
$messages = $validator->messages();
// redirect our user back to the form with the errors from the validator
return Redirect::to('/register')
->withErrors($validator)
->withInput(Input::except('password', 'password_confirm'));
} else {
// validation successful ---------------------------
// our duck has passed all tests!
// let him enter the database
// create the data for our duck
$duck = new User;
$duck->username = Input::get('username');
$duck->email = Input::get('email');
$duck->password = Hash::make(Input::get('password'));
$duck->serial_key = Input::get('serial_key');
// save our user
$duck->save();
// redirect with username ----------------------------------------
return Redirect::to('/registered')->withInput(Input::old('username'));
}
}
you need to make $user->doRegister(); a return statement
in your RegisterController you have to do
public function doRegister(){
$user = new User();
return $user->doRegister();
}
try this
return Redirect::to('/registered')
->with('bill_no', Input::get('username'));
in the '/registered' controller,..
use this
$username = Session::get("username");
above worked for me,...
I managed to save a new password or change a password for a logged in user.
public function saveNewPassword() {
$rules = array(
'old_password' => 'required',
'password' => 'required|confirmed|different:old_password',
'password_confirmation' => 'required|different:old_password|same:password_confirmation'
);
$user = User::findOrFail(Auth::user()->id);
// Validate the inputs
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withInput();
} else {
$password = Input::get( 'password' );
$passwordConfirmation = Input::get( 'password_confirmation' );
if(!empty($password)) {
if($password === $passwordConfirmation) {
$user->password = $password;
$user->password_confirmation = $passwordConfirmation;
}
} else {
unset($user->password);
unset($user->password_confirmation);
}
// Save if valid. Password field will be hashed before save
$user->save();
}
// Get validation errors (see Ardent package)
$error = $user->errors()->all();
if(empty($error)) {
Session::flash('message', 'Successfully saved!');
return Redirect::back();
} else {
Session::flash('error', $error);
return Redirect::back();
}
}
The problem I have is, how to check the Old Password, that is equal to the current password? Any Ideas? Does Confide has his own methods for changing passwords?
I use this sollution for changing the password. In your rules you have one error: password_confirmation should be the same as password not password_confirmation.
Here is the complete and tested function:
public function changePassword($id){
$rules = array(
'old_password' => 'required',
'new_password' => 'required|confirmed|different:old_password',
'new_password_confirmation' => 'required|different:old_password|same:new_password'
);
$user = User::find(Auth::user()->id);
$validator = Validator::make(Input::all(), $rules);
//Is the input valid? new_password confirmed and meets requirements
if ($validator->fails()) {
Session::flash('validationErrors', $validator->messages());
return Redirect::back()->withInput();
}
//Is the old password correct?
if(!Hash::check(Input::get('old_password'), $user->password)){
return Redirect::back()->withInput()->withError('Password is not correct.');
}
//Set new password to user
$user->password = Input::get('new_password');
$user->password_confirmation = Input::get('new_password_confirmation');
$user->touch();
$save = $user->save();
return Redirect::to('logout')->withMessage('Password has been changed.');
}
This also works if you dont work with Confide.
From the github of confide:
Integrated with the Laravel Auth and Reminders component/configs.
So I would guess using the Auth::validate() method will do the trick.