Username is store in my session. I am working on leave management module.
My Leave module's table has these fields
User Name, Leave Type, Duration, Status, & Action.
Apart from username all details I can insert and list in list view.
But my user name is store in session.
How can i get it from session and store in database In Username field??
Code in my view file
#if(Session::has('key'))
<?php $username = Session::get('key')['username']; ?>
#endif
<input type="hidden" name="username" value="<?php echo $username ?>">
Code In My controller file
public function leaveApplication(Request $request)
{
$leave = new LeaveManagement();
$leave->username = $request->get('username');
$leave->leaveType = $request->get('leaveType');
$leave->startDate = $request->get('startDate');
$leave->endDate = $request->get('endDate');
$leave->fromLeave = $request->get('fromLeave');
$leave->fromHalfDayLeaveType = $request->get('fromHalfDayLeaveType');
$leave->toLeave = $request->get('toLeave');
$leave->toHalfDayLeaveType = $request->get('toHalfDayLeaveType');
$leave->fullDayLeave = $request->get('fullDayLeave');
$leave->typeOfLeave = $request->get('typeOfLeave');
$leave->reasonForLeave =$request->get('reasonForLeave');
$leave->status = 'Pending';
$leave->save();
return redirect('leave');
}
You can use Laravel global session helper: session(keyName).
To get username from session and store it in database:
$username = session('username');
$leave = new Leave();
$leave->username = $username;
$leave->save();
For more information on Laravel session see Laravel Session
you can get object from your session and access to its attributes.some thing like:
$user = session()->get('user')
$username = $user->username
or after logging you can access your user's username with:
auth()->user()->username
After login, Auth maintains user table fields in it. You can access it like that -
echo Auth::user()->name or echo Auth::user()->email
It will return the expected values as : "usersname" and "useremail#domain.com".
If you are not using Auth, then you can get username from your session and save it in DB like that -
$user = session()->get('user')
$username = $user->username;
$leave = new LeaveManagement();
$leave->username = $username;
$leave->save();
Hope this will help you.
Related
I have this mechanism of checking the logged-in user role in a blade page so that I can give him/ her the selected pages but I find after a user has logged in and stayed idle for a long time and when he is back and wants to continue in the page he is getting an error
ErrorException
Trying to get property 'id' of non-object (View: /var/www/html/poss_v1/resources/views/dashboard/student/index.blade.php)
and my code is here below
<?PHP
$user = Auth::user();
$user_id = $user->id; //----here is the problem it is missing the id when the session has ended--//
$role_user = App\Models\RoleUser::where('user_id', $user_id)->first();
$role_name = App\Models\Role::where('id', $role_user->role_id)->first();
$role = $role_name->name;
?>
#php
if(Auth::check()){
$role_user_id = App\Models\RoleUser::where('user_id', auth()->id())->first()->role_id;
$role = App\Models\Role::where('id', $role_user_id)->first()->name;
}
#endphp
In Laravel blade file you should check logged in user in this way.
#php
if(Auth::check()){
$user = Auth::user();
$user_id = $user->id;
$role_user = App\Models\RoleUser::where('user_id', $user_id)->first();
$role_name = App\Models\Role::where('id', $role_user->role_id)->first();
$role = $role_name->name;
}
#endphp
To increase the lifetime of your session, you may modify the env variable SESSION_LIFETIME to whatever minutes you wish.
And to make sure your user doesn't get this error, you may check if the session is still alive with Auth::check()
I have an order and I want to after save, get in session current id of order for next page reports.
use Session;
public function store(Request $request)
{
$order = new Order($request->all());
$order->user_id = auth()->user()->id;
$order->title = $request->title;
$order->body = $request->body;
$order->id = $request->session()->get('id');
$order->description = $request->description;
$order->save();
session(['order_id' => $order_id]);
return redirect()->route('reports.index')->with('order_id', $request->id);
}
In notes page has a input hidden for get session of article id.
<input type="hidden" class="form-control" value="{{ Session::get('order_id') }}" name="id" id="id">
But I see input hidden. It is blank . "".
I'm confused here
session(['order_id' => $order_id]);
return redirect()->route('reports.index')->with('order_id', $request->id);
when you're returning something with "->with()" it stores in session so why use session() ? and $order_id is not defined in your method.
and best way to put something in session is like this
Session::put('order_id');
I think if you do this
->with('order_id', $order->id); // You will get the current order id created
I use function onUserAfterSave and $isnew, but I can't get phone and address of user registered in Joomla 3.X. I only can get name, username, email, but no data from profile.
To access the user profile data you need to use JUserHelper class
jimport( 'joomla.user.helper' );
$user = JFactory::getUser();
$userProfile = JUserHelper::getProfile( $user->id ); //Get userprofile data
//You can get all the user input of profile data. If you want to get country of user use
echo "Country :" . $userProfile->profile['country'];
Amit Ray,
I use in my plugin:
public function onUserAfterSave($user, $isnew, $success, $msg)
{
//die(print_r($user));
if ($isnew){
.............
$pluginParams = new JRegistry($plugin->params);
$name = $user['name'];
$name_usuario = $user['username'];
$email_user = $user['email'];
$id_usuario = $user['id'];
jimport( 'joomla.user.helper' );
$user = JFactory::getUser();
$userProfile = JUserHelper::getProfile( $id_usuario );
//You can get all the user input of profile data. If you want to get country of user use
$user_phone = $userProfile->profile['phone'];
$user_address = $userProfile->profile['address1'];
But $user_phone and $user_address no have data... whats is error? Thanks
I'm starting with Codeigniter and i have a question. How (if possible) to create authorization api to other sites (no facebook, twitter etc.).
I have three sites - CI system, IP.Board and WordPress, and my point is a integrating all those systems in one auth api.
I heared about Oauth and OpenId, but I don't know how this things has postponement to CI. Is complicated?
Hello It can be possible
I can suggestion you can use temporary table store that table logged user id and current date& time and also ip address ,and store that one in to Sharing Cookies Across Multiple Domains/send hidden stored id to your multiple site.based on id and ip and time you can implement logic
note: logout and session time out times you can delete the record form table
I have written small example plz check it once . it my be helpful you
Session will most likely not to work. I thought of using JSON but I failed to figure out how to make the Javascript to be dynamic as I always prefer to use jQuery (if you have an idea, feel free to share). In the end, I decided to use PHP along with MySQL and cURL.
We will first start by creating a table to handle the login session:
CREATE TABLE IF NOT EXISTS `sso_login` (`user_id` int(11) NOT NULL)
Purpose of the table is to store logged in users and remove then upon signing out.
Next is the PHP script which will insert new user id upon signing in (file name: register.php):
<?php
/**
*
* #
*/
$userId = $_GET['id'];
$sql = sprintf("INSERT INTO sso_login VALUES ('%s')", $userId);
mysql_query($sql);
?>
Simple, isn’t it? All it does is inserting the user id into the table.
Next is the file which delete the user id when user is signing out (file name: drop.php):
<?php
/**
*
*
*/
$userId = $_GET['id'];
$sql = sprintf("DELETE FROM sso_login WHERE user_id = '%s'", $userId);
mysql_query($sql);
?>
I believe the script is pretty much self-explainatory.
Next is the file which checks whether user is already logged in or vice versa (file name: check.php):
<?php
/**
*
*
*/
$userId = $_GET['userId'];
$sql = sprintf("SELECT COUNT(*) FROM sso_login WHERE user_id = '%s'", $userId);
$query = mysql_query($sql);
if (mysql_result($query, 0, 0) > 0)
{
echo 'User '.$userId.' has logged in';
}
else
{
echo 'User '.$userId.' is not logged in.';
}
?>
This script will check whether the user id being passed is available in the table or not. If not it means that the user have not logged in.
These three scripts will be store on the other party’s server. Next are scripts which will be store on BC’s server where the login and logout process being done. All these processes are using cURL.
First is the script which will update the other site when user is logged in (file name: login.php):
<
?php
/**
*
*
*/
if($_POST)
{
$username = $_POST['username'];
$password = $_POST['password'];
if (($username == "bcoffee") && ($password == "demo"))
{
$userId = 10;
$target_site = sprintf("http://the-other-site.com/register.php?id=%s", $userId);
$timeout = 5;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_site);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_exec($ch);
curl_close($ch);
header('location: index.php');
}
}
?>
You might notice that the username and password are hard coded, this was for P.O.C purpose, so it does not matter. Back to the tutorial.
As you can see, it will reach register.php with the user’s id. Refer back to content of register.php which will then insert the user id into the table. This way, the other server will know that user is already logged in.
Next is the script which will notify the other server when the user is signing out (file name: logout.php):
<?php
/**
*
*
*/
$userId = 10;
$target_site = sprintf("http://the-other-site.com/drop.php?id=%s", $userId);
$timeout = 5;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_site);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_exec($ch);
curl_close($ch);
header('location: login.html');
?>
Similar to login.php but this time, it will remove the user id when user is signing out. Hence when the system checks (using check.php) and found that the user id is not available, it will know that the user is not currently logged in.
I'm using Tank-Auth for my application. And the only problem I have is activating and resetting passwords for accounts.
For login, register, logout; I have no problem with this codes;
$route['login'] = "/auth/login";
$route['logout'] = "/auth/logout";
$route['register'] = "/auth/register";
But for activating accounts and resetting passwords, those codes are not working;
$route['activate/:num/:any'] = "/auth/activate/$1/$2";
$route['reset_password/:num/:any'] = "/auth/reset_password/$1/$2";
PS: The first segment after 'activate' is 'user id' and second segment is key like this: example.com/activate/2/4784322e48916efec1153c53d25453c7
The solution is changing url segments in the (auth) controller from this:
$user_id = $this->uri->segment(3);
$new_pass_key = $this->uri->segment(4);
to this:
$user_id = $this->uri->segment(2);
$new_pass_key = $this->uri->segment(3);
After this change, the routing for activate&reset_password is working with those rules
$route['activate/:num/:any'] = "/auth/activate/$1/$2";
$route['reset_password/:num/:any'] = "/auth/reset_password/$1/$2";