How to make ProfileViews count in laravel? - laravel-8

I need help. How do I create profile visitors in Laravel 8? I have not written the code yet and I do not even have an idea how to complete this task. I would be very grateful if you could help me.

You can leave the visitor without any profile. He will be able to access public web pages without authentication and therefore only logged in users will have profile.
UPDATE
Proposed skeleton in two steps using cookies
1- When a user make request on profile page generate an unique identifier and store it in user cookie with a long enough validity if it not yet exists
2- Using this unique token register a new view count (if not yet done) before sending response to user.
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Requests;
use App\Http\Controllers\Controller;
public function getProfilePage(Request $request){
if(!Cookie::has('uniqueId')){
$uniqueId = Str::uuid(); // generate unique id for current user
Cookie::queue('uniqueId', $uniqueId, [live time in minutes]);
// save it using Cookie::queue
// one year for example as live time
}
// check if view count already exists for $uniqueId
// and save it in a persistent way if not
// other stuff ....
return ....
}

Related

How can i manage a global variable among different webpages in wix?

I am developing a site for online test series. I receive details o student on one page and wish to display the info on net page as dashboard. I need a global variables that can hold the detail of student like his roll no, name, email, class, semester, subject and the test he wishes to solve.
I have tries using a separate .js file to hold variables. However, the values of variables get lost as i move to next page.
can anyone help?
The code and links are attached.
https://ishuuw.wixsite.com/ymatutor/test-series
//import wixData from 'wix-data';
import {globalVars} from 'public/vars.js';
$w.onReady(function () {
});
export function button2_click(onClick,button2){
globalVars.StudentRoll=$w('#input1').value
globalVars.StudentName=$w('#input5').value
globalVars.StudentSurname=$w('#input6').value
globalVars.StudentMail=$w('#input7').value
globalVars.StudentClass=$w('#dropdown1').value
globalVars.StudentYear=$w('#dropdown2').value
globalVars.StudentSemester=$w('#dropdown3').value
globalVars.StudentPaper=$w('#dropdown5').value
globalVars.StudentTest=$w('#dropdown6').value
//Just to test, this works well
$w('#text63').text=globalVars.StudentRoll
$w('#text64').text=globalVars.StudentName
$w('#text65').text=globalVars.StudentSurname
$w('#text66').text=globalVars.StudentMail
$w('#text67').text=globalVars.StudentClass
$w('#text68').text=globalVars.StudentYear
$w('#text69').text=globalVars.StudentSemester
$w('#text70').text=globalVars.StudentPaper
$w('#text71').text=globalVars.StudentTest
}
`
Sounds like you should be using wix-storage. That will allow you to pass information between pages.
Use local storage if you want the data to persist for the user even after the user ends the session. Meaning, next session, the user's data will still be available.
Use session storage if you want the data to be removed when the user's session ends.

How to update login time in DB in YII2?

My user login with database is set up following this guide: http://www.bsourcecode.com/yiiframework2/yii-2-user-login-from-database/
Now my question is: how to implement last login feature so that every time user logs in, the appropriate database field is updated with current time?
I've set up rules in user model and also tried Timestamp Behavior, but no luck.
Thanks in advance!
Ended up using the login function where assigning date to user model was piece of cake.
$user->logintime = new Expression('NOW()');
$user->save();
The problem earlier was that I tried to implement it in the action of User model rather than in login form.
Simple way to make it happen is in your login method just add touch
function login(){
// check login
if(//login true)
{
$user = User::findIdentity(Yii::$app->user->id);
$user->touch('lastlogin_at'); // Datatable column name that you want to update the time.
}
}
See more about touch() over here

MVC user's full name in Url, how to handle duplicates

I want to setup the following url in my MVC4 website, using the user's full name in the url:
http://www.myapp.com/profile/steve-jones
I have setup the following route in Global.asax:
routeCollection.MapRoute(
"profile", "profile/{userName}",
new { controller = "myController", action = "profile", userName = string.Empty
});
And I can take the parameter 'steve-jones' and match it to a user with matching name. My only problem though is, what if there is more than one 'Steve Jones', how can I handle this?
Does anyone know of a workaround/solution to this so that I can use a user's full name as part of the url and still be able to retrieve the correct user in the controller method?
Am I forced into including the user's id with the url (something that I do not want to appear)?
The usual way of handling this is by appending a number when creating the profiles. So if "steve-jones" is already a name in the database, then make the user's display name "steve-jones2". You basically have to insist that all profile urls are unique, which includes updating any existing database and account creation code.
Alternatively (and/or additionally), if two same names are found then have the script reroute to a disambiguation page where the user is presented with links and snippet of profile info of the many existing Steve Joneseses so they can go to the full correct profile.
Another way of handling it is by giving all user profiles an additional numeric code on the end. At my university all logins are based on name, so they give everyone pseudo-random 3-digit extensions so that they are safe as long as they don't get 1000 people with the exact same names :)
Some people might be happier being steve-jones-342 if there is no steve-jones or steve-jones1, if you're concerned.

Funnel non-activated users to a single page and restricting access

So, given that I have user table with a field of "is_confirmed", I want funnel anyone with an "is_confirmed" that is equal to 0 to a single url using the redirect...
Is there a way to do this check so that it only needs to be implemented once? I guess a good example would be the default user functionality of how any page that requires a logged-in user forces the user to the login page...
Haven't tried this specifically myself, but based on Yii's documentation one would probably redirect the user inside the login action based on the value. If the user is confirmed, call the CWebUser::login(..) method to store the identity in persistent storage and use the returnUrl redirect, otherwise the user is not confirmed so skip that part and just render the in limbo page.
Inside your SiteController::actionLogin() you'll probably do something like this (additionally the UserIdentity component should have a method for returning the value of the confirmation field)-
$identity = new UserIdentity();
if($identity->authenticate()) {
if($identity->isConfirmed() == 1) {
Yii::app()->user->login($identity);
$this->redirect(Yii::app()->user->returnUrl);
} else {
$this->render('not_yet_confirmed_view');
}
} else {
throw new CHttpException(500,'Internal Server Error. Could not authenticate.');
}
Edit- Removed the accidental extra CWebUser::login() and clarified that the UserIdentity component will require just a bit of additional smarts.

Get the current user Liferay using a simple Java code

I'm working with : Liferay 6.0.6 with JBoss 5.1 and Struts2.
My question is, how to get the current user in Liferay once logged in, using a Java code.
In your doView/processAction method do following
User user = (User) request.getAttribute(WebKeys.USER);
or use the ThemeDisplay object. It contains another information like companyId, groupId, ...
ThemeDisplay td =(ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
User user = td.getUser();
Classes ThemeDisplay, User nad WebKeys are part of portal-service.jar.
If you need just some id to identify current user you can also use
String userId = request.getRemoteUser();
This solution is not Liferay specific and should be portable among jsr-286 portals.
Liferay provides Util class
com.liferay.portal.util.PortalUtil
This class contains all utility methods to get the frequently used attributes.
Try using PortalUtil.getUser(PortletRequest portletRequest) method to avoid creating new objects and references.
This is an other possible way to do it :
private LiferayFacesContext liferayFacesContext = LiferayFacesContext.getInstance();
User currentUser=liferayFacesContext.getUser()