HTML/PHP Detect if form value has been changed

Asked

Viewed 781 times

11

I have a form where I can change things like username, password, morada, etc. On the site I also use a session (login) for each user, which is defined by username. The problem is that by changing the value of the username he is no longer equal to username of the current session, causing the login to stop working. Is there anything I can use to detect the change in username, to redirect to the login page again and display an alert saying "Username changed, log in again"?

  • By changing the username you can also change the value of the current session so you no longer lose the session.

  • 1

    Or use Javascript that detects the change and already redirects in real time

  • Provide some code snippet for the answer

4 answers

1

Probably in your code there is a button to send the change in username, what you can do is: after the page that changes the username in the database is finished, vc makes a redirect

if (mysqli_query($con,$sql))
    print "<script>
    alert('username alterado bla bla')
location.href='página de login.php'
</script>";

Location.href='login.php page' > This part that forwards

-1

I suggest using token:

Once the user logs in, you generate the token and save it to the BD and session.

$token = md5(uniqid(rand(), true));

When logging out you remove the BD token and so the user must log in again.

Any change to the username will not affect the token.

I hope I’ve helped.

-1

Since the username can be changed it is recommended that you identify the session by the database user ID, which would not create conflict if the same login is open in two different sessions, as the name says, the ID is an identifier mechanism. The user, email, document number, etc., for being unique can also be used, but it is not recommended if these values can be changed because of situations like these.

-1

You can give a session_start(); making it generate a single session for each browser even if used by the same username in different browsers, each will have a different Sesssion, what you can do is how I do in my applications, you generate the Session each time the user enters the login and when passing through the password checker, takes the Session and Since within a log logins and if unnecessary a log, changes the field next to the user table, ie when logging in, you can choose

1) takes the Session and puts it in a login log table with SESSION, DATE and USERNAME.

2) takes the Session and changes the user’s Session within the user table.

and then when it makes user change, create along with it a Session that indicates to the browser that this happened through the stored Session in the database, then check if they are equal, if not, it redirects to login if you want more details, I can show you a practical code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.