How to identify the first time user login and that time display some alert message in javascript
can any body help me please ?
guys
Have a column in the user table LAST_LOGIN DateTime. Every time the user logs in update that record.
By default make the column NULL. If LAST_LOGIN === null then it's the first time the user has logged in.
You can always use cookies:
if (document.cookie != 'visited'){
alert('Hey there, new user!');
document.cookie = 'visited';
}
However, if the user clears history or cookies in between, this will be reset.
A better way would be to have your server track which users have visited before.
Make a first_login column in your DB.
Check if it is empty when a user tries to login, if it is, populate the field with the current date & time, else do nothing.
<?php
// Assuming you know the user is logged in & validated.
if($flogin === ""){
// Query: "UPDATE SET first_login = NOW() WHERE user_id = '$logged_in_user_id'"
}else {
// Get first_login and display to the user
}
?>
Not really sure if you know what you're doing since you're asking how to display something to the user, so, for more info please clarify more.
Related
I'm trying to run a function when body loads, that will check if the user is logged in, and if not redirect them to the login page. Here is the login function :
function login() {
var mail = document.getElementById('mail').value; //get values
var psw = document.getElementById('psw').value; //get values
localStorage.setItem('logged_in', true); //specify that user is logged in
localStorage.setItem('mail', mail); //store mail
window.location.replace('pages/home.html'); //redirect to home page
}
And the function to check if the user is logged in :
function check_logged_in() {
const logged_in = localStorage.getItem('logged_in');
if (logged_in == null) { //check if user is logged in
alert('You are not logged in, you are about to be redirected. '); //alert user
window.location.replace("../index.html"); //redirect to login page
}
}
The problem is that even if the login function run before, I am redirected. I think that the localStorage resets on each redirection. If that is the problem, do you know the way to prevent this, or if it isn't the problem, do you know what it might be?
Based on the documentation a localStorage item can either be null (when it's empty) or a string. It does not store any other data types.
The expression
localStorage.setItem("logged_in", true);
Does not save a boolean value to the localStorage item. instead, it saves the string value "true"
Hello everyone i try to use localStorage when user try to login my website if he/she try more than 2 times reCaptcha appear only way is click checkbox in reCaptcha but if user refresh the page reCaptcha is gone because of my state attempNumber. I want to keep attempNumber in localStorage and use firstly localStorage if user try more than 2 , when refresh page , reCaptche should be there .
I tried like that, but it is not working.If you help me i will be so appreciate for that . Thank you everyone .
this.state = {
email: '',
password: '',
load: false,
attempCount: 0,
};
handleLogin(e) {
const {
email, password, recaptcha,
} = this.state;
const cachedHits = localStorage.getItem(this.state.attempCount);
if (cachedHits) {
this.setState({ attempCount: JSON.parse(cachedHits) });
return;
}
this.setState({ attempCount: this.state.attempCount + 1 });
e.preventDefault();
e.stopPropagation();
this.props.doSignIn({ email, password, recaptcha });
return false;
}
well, code you have added doesn't show how and where you are setting value in localStorage.
I assume you will update localstorage in login button click event something like this
localStorage.setItem('clickCount', this.state.attempCount );
assuming your this.state.attempCount is incremented by 1 on click
Now to get its value use
let count = localStorage.getItem('clickCount');
if(count > 2){
// show Captcha
}
Without seeing your doSignIn code it is hard to tell what is happening with your localStorage, but your call to localStorage in the code you pasted is passing your attemptCount as the key. You should use a string as the key such as 'attemptCount' rather than a key that will change with your state changes.
That might not be all your problem, but is what I noticed so far.
I guess you're trying to get the value under key 0 if you take a look at the documentation here. Try to save it under a key you now as localStorage.setItem('attempts', this.state.attempCount) and then localStorage.getItem('attempts').
When you do a getItem I believe it will return a string, so do a parse parseInt(localStorage.getItem('attempts'), 10) before using it for any operation.
There are two issues with the code:
value is never beign set in localstorage.
First time when handleLogin is called, below line
const cachedHits = localStorage.getItem(0);
it tries to fetch value for zero which is never stored in local storage and it returns null(i.e cachedHits= null),
then it skips the if part and sets attempCount in state to 1 and show captcha.
next time when handleLogin is called,
const cachedHits = localStorage.getItem(1); which is again not present in cache and cachedHits= null and so on...
so, you need to set value in localStorage against some key like and in getItem use that key
I'm trying to do Remember Me with an interesting algorithm. I was thinking, when i click the Remember Me and Login, the value of the text fields (username&password) will be saved as a default value. My login button click event here:
var username = Ext.getCmp('setNo').getValue();
var password= Ext.getCmp('setPass').getValue();
if(refs.checkStudent.value === true){
Ext.getCmp('setNo').setValue(username);
Ext.getCmp('setPass').setValue(password);
}
else{
Ext.getCmp('setNo').setValue("");
Ext.getCmp('setParola').setValue("");
}
On console, it is working. But I'm working with a local store, no server. So when i refresh the page, it's gone. Is there a way to not lose them?
on your view onAfterRender event:
Ext.getCmp('setNo').setValue(localStorage.username);
Ext.getCmp('setPass').setValue(localStorage.password);
on your button click event:
if (refs.checkStudent.value === true) {
localStorage.username = Ext.getCmp('setNo').getValue();
localStorage.password = Ext.getCmp('setPass').getValue();
} else {
localStorage.removeItem('username');
localStorage.removeItem('password');
}
Use ExtJs utility to set and get values in cookies. At time of login set
username and password in cookies and after refresh the page read username
and password value from the cookie.
Ext.util.Cookies.set('username', username); // To set value in cookie.
Ext.util.Cookies.get('username'); // to get value form cookie.
I have a JS function to delete the user's own post.
Function delete(post_id)
{
//Here comes AJAX call to delete.php file
// In delete.php file i have a query to delete the post from DB using that
post_id parameter.
}
My Question is:
Through inspect element, the user can change the function post_id value, So how can i prevent it from getting changed?
If that cannot be blocked means, how can i retain my old value itself, that is whatever the user change, should not get affect the original function post_id value.
For example:
user posted 2 posts with post_id 1 and 2 respectively.
if the user wants to delete the 2nd post means, he can use 2nd post's delete button to delete that post.
Function will be Like:
Function delete(2) //post_id is 2
{
//AJAX call to delete.php
}
How to prevent user from changing the post_id in function to 1 ?
If that cannot be prevented means, how can i prevent from passing this changed post_id to delete.php
Delete.php:
"DELETE FROM `posts` WHERE `postid`='$_POST[post_id]' and user_id='$_SESSION[id]'";
In your delete.php, you can put a SESSION check if this user has the right to access that data before proceeding with the delete.
Even if the user changes the id, as long as this id is assigned to him/her, he/she has the rights to delete that row on your database.
It will still be on him/her alone the fault for removing a row in your database.
If you still want to retain the deleted post, you can add another column for your table, let's say status. It will only have two types of value, 1 or 0. The value 1 means that it is active and viewable, and if it is 0, means it is hidden (the user will actually think that the removed post is deleted permanently).
When the user decides to delete his/her own post, you will just run an UPDATE query and make that row to 0. It will still be in your database, but is hidden to him/her and all other users of your system. You can create an administrator page where you can filter deleted (hidden) and active post.
Even if a user changes the GET parameter in the URL, as long as you have a condition of WHERE status = 0, it will not be view-able in his/her end.
Or you can restrict a user from deleting his/her own old post. By putting a range in your DELETE query (assuming that your table has a TIMESTAMP or DATETIME column).
... WHERE id = ? AND user_id = ? AND date_posted < ? AND date_posted > ?
Note:
There is no way to prevent the users of your system to change the elements in their end. Even if you disable the right click using javascript, or having a lot of interactive validation in the front-end, and any procedure-alike, it will still all end in your back-end validation.
I'am really frustrated with the result I'm getting in my code:
userOldPass //from user input, user's old password
oldPass //from database, user's old password
newPass //from user input, user's new password
confirmNewPass //from user input, user's new password confirmation
function checkpass(){
if(userOldPass==oldPass) && (newPass==comfirmNewPass){
alert('success');
}else{
alert('error');
}
}
The problem is it always says 'error' even if the inputs are already all correct. I checked each variables' values via the alert function to see if there is some inequalities but everything is okay and still it alerts 'error'.
What could be the problem?
P.S. all the variables are in SHA1 hash. I used the CryptoJS.SHA1 function I found in the net.
I use the alert function to check each value from the variables and the results are right.
if(userOldPass==oldPass) && (newPass==comfirmNewPass) // all should stay in ()
In this way:
if ((userOldPass==oldPass) && (newPass==comfirmNewPass))