session data updating and retriving in yii - php

firstly I have two tables
1) family - stores data about family like familyid,city,homephone,address,email
etc
2) person - stores about individuals like
persontype,gender,mobile,email,firstname,lastname etc
these tables are filled while signup.
After signup,
Whenever user logins all the data from person and family
retrieved and stored in session and used anywhere in the website by session.
$person=new Person();
$account=Onlineaccount::model()->find(array('select'=>'PersonId,EmailAddress,LastLoginDate,FailedLoginCount', 'condition'=>'EmailAddress=:EmailAddress','params'=>array(':EmailAddress'=>$model->username),));
$person=new Person();
$person=Person::model()->find(array('select'=>'PersonID,FamilyId,FirstName,LastName,MiddleInitial,
MobilePhone,PrimayContact,PersonTypeCode,DateOfBirth,GenderCode,EmailAddress', 'condition'=>'PersonID=:PersonID','params'=>array(':PersonID'=>$account->PersonId),));
$familyDetails=Family::model()->find(array('select'=>'FamilyID,HomeAddress1,HomeAddress2,HomeAddress3,City,State,Zipcode,HomePhone,HomeFlag,CurrChapterCode,FinanceMasterRecID,Membersince',
'condition'=>'FamilyID=:FamilyID','params'=>array(':FamilyID'=>$person->FamilyId),));
// $session=new CHttpSession;
// $session->open();
$_SESSION['newaccount'] = serialize($account);
$familyDetails->firstName= $person->FirstName;
$familyDetails->middleName= $person->MiddleInitial;
$familyDetails->lastName= $person->LastName;
$familyDetails->gender= $person->GenderCode;
$familyDetails->dob= $person->DateOfBirth;
$familyDetails->email= $person->EmailAddress;
$familyDetails->userName= $account->EmailAddress;
$familyDetails->mobilePhone= $person->MobilePhone;
$familyDetails->registrantType= $person->PersonTypeCode;
$familyDetails->PersonID=$person->PersonID;
$familyDetails=$this->getMembershipDetails($person->FamilyId,$familyDetails);
$_SESSION['familyDetails'] = serialize($familyDetails);
Now the question is ,When user logins and in his profile he wanna change the field like lastname through update form...the lastname is updating but the updated data is not showing after refreshing also it is displaying last data because of sessions...when user logout and login the newly updated data is showing..
how to show the updated data after refresh through session in yii ..
thanks for reading and waiting for suggestions..please help me..

After updating the profile reassign the session values again. Then only you can access updated data every where in the application.

Related

Inserting current user id into another table PHP

I am fairly new to PHP and am not sure how to do this.
I have two tables:
owner (ownerid(PK), username, password)
venue (venueid(PK), owenerid(FK), venuename, location, number)
owner stores the details of the current logged in user. Once the user is logged in they enter details into a form that gets inserted into venue table.
How do I take the ownerid of the current logged in user and insert it into ownerid (in venue table) so that at a later stage I can select all venues that a particular user has added, and only that logged in user can view them.
I am pretty new to PHP so would appreciate as much explanation / code as possible :)
Thanks!
If you have a logged in user you will be able to identify them either through a cookie or a session. If you store their ownerid in the session variable, then you will be able to access it whenever you require
$ownerid = $_SESSION['user']
You can then use that variable in association with the selected venues, and relate them together so only this user can view them when logged in, matching their ownerid

Laravel add Auth session variables

After user logging to the system(Auth::attempt success)
How to add additional data to the Auth session for user ?
I have user all data but there is no profile image data of user.Profile image of user is save into another table like user_profile_image.
I have to set profile image data in Auth::user()->file_name till when user logout.
I tried this way:
Auth::user()->setAttribute('key','value');
and
Auth::user()->file_name=$filename;
but it doesn't work.
If you want to just save the user picture:
add a migration that adds the picture column in the users table
Use the field to save the picture
$u = User::find($userId);
$u->picture = $pathToPicture;
$u->save();
then access it with
$u->picture;
or
Auth::user()->picture;
if you want to add fields to registration form: http://www.easylaravelbook.com/blog/2015/09/25/adding-custom-fields-to-a-laravel-5-registration-form/

profiles with sessions php

recently I've added a search feature to find users through my database and I encountered a question upon selecting the searched user:
-Example: John Doe searches for a user; John Doe then clicks the link to go to the users profile (view_profile.php). -Should I be echoing out all the info I want to display about the searched user via using mysql_fetch_array($username) and storing all desired info in session variables?
Should I be passing session variables to create profiles for other users to view. Or should every profile have it's own file? Thanks.
Set the Link that comes up when a user is searched to:
*put user name here*
When the User clickes the link, PHP should get the users row from the database with:
$_GET['userId'];
SQL Query:
SELECT * FROM users WHERE id = *put user id here*
Don't use $_SESSION variables for this.
Hope this helped you out.

Connecting a users shopping basket to their user account? PHP

I'm confused on how to do this. Say the user creates an account with my ecommerce website, and then starts adding products into their basket. If I store the users username and password in a database table, and use sessions/cookies to manage the products in their shopping basket, what would I need to do in order to connect the users shopping basket to their account, so that when they log in they will be able to see the items they had previously stored?
Do I first allow the user to login, query whether they logged in successfully, and then make a session/cookie variable for their username? Or do I have to store the users cart items in a database and connect it with the user accounts table?
I'm confused with how to store a shopping baskets items into a table. Is that even the right way to do it?
There's no code yet, I want to create the databases correctly before I start coding and just need some advice. Thank you
If you have two tables, one for the users and one for the items, you can do something like the following.
Manage current basket items by adding to and removing the item_ids into a serialized array, which you can store in your users table AND the session at the same time, keeping them in sync. For example, if a user visits your store for the first time (not logged in, and empty shopping basket), you can create the session like so.
session_start();
We start the session.
if (isset($_SESSION['current_basket']) {
$current_basket = unserialize($_SESSION['current_basket']);
} else {
$current_basket = array();
}
Because this is the first time our visitor has visited our page, the session variable current_basket will not be set, meaning the above if statement will not run and instead just create an empty PHP array in a variable called current_basket.
Now, when the visitor adds an item to the basket, we just need the item's ID in your database and add it into the current_basket array.
$current_basket[] = $item_id;
Then we immediately update the session variable with the new array, serializing it in the process.
$_SESSION['current_basket'] = serialize($current_basket);
You now have a proper, usable array with all the product IDs for that person's shopping basket.
Now, let's pretend the user was logged in. We'd check for that and only add one more step.
$sql = "UPDATE users SET current_basket=" . serialize($current_basket) . " WHERE id=$user_id"
// Execute that query.
There, now that same array is in the database which you can then pull and set as a session variable when the user logs in.
You can run these series of steps everytime a product is added or deleted, keeping it updated both in the session and the database.
This is obvisouly a very watered down concept for the sake of explaining. Obviously, you'd need to manage the array better. For example, not adding a product to the basket if it's already there, removing items from the array...etc.
The key is to serialize the array, and store in the session always, and in the users table if the user is logged in.
Then when the user comes back and logs in, you can set the array in the session for them using the array in the database from when they were last on your site.
I would have the cart table cart_id / user_id / date_time_created then an item table indexed on the cart_id.
Also add a way to dump the cart_id and items out of the tables after so many hours or days otherwise your database will get huge.
And let your end users know that the items will be dumped after x amount of time.

Getting values from a table based on Session data

I have an admin section to one of my websites I am building.
The variables it logs in by is email address and password.
Obviously each email address is unique to each admin, although I do have an 'admin_id' column which increments automatically per row.
The other columns stored within my admin table are - First Name (String), Last Name(String), Level(int).
I have tried -
$_SESSION['admin_id']=$_GET['admin_id'];
But when I try and simply echo the id, nothing is displayed.
Yet this line of code:
if(!isset($_SESSION['admin_email']))
die("<meta http-equiv='refresh' content='0; url=login.php'> ");
Manages to get the 'Admin_email' column from the table.
I am confused as to how to add other columns into my session data.
Many thanks in advance.
I have figured out why the 'admin_id' was not successful in been retrieved from the database.
The login.php where the admin first must authenticate themselves from is where the session data is saved - for it to be used in the admin session once logged in.
In order to solve this, I added the following line within the login.php (whilst the email address were been retrieved from the database):
$dblevel = $row['level'];
and then to save it to a session:
$_SESSION['admin_level']=$dblevel;
I logged out and then back in to reinitiate the session and it worked.

Categories