PHP login user name

I canceled all the tutorials that never worked for one reason or another, and decided to deploy my own registration / login function myself, and, to my surprise, it really works!

But I do not understand how the logic associated with the fact that someone entered the work! For example, as soon as they are logged in, I just have $_POSTtheir data on any other page that they visit, and as soon as they are on a new page $_REQUESTthat send data from the URL and display a message like "yes, you are still logged in system ??

I'm a little confused, so I hope that this question will not bother you either.

+3
source share
4 answers

We have pages like login.php after_login_page1.php after_login_page2.php

You can follow these simple steps.

  • Set $ ​​_SESSION ['id'] = $ userid // userid from db to login.php

  • always has session_start () on subsequent pages, for example after_login_page1.php, after_login_page2.php

  • Make sure (! Isset ($ _ SESSION ['id'])) {header ("Location: login.php"); }

  • on the logout.php page, specify $ _SESSION ['id'] = ''; and execute session_destroy ()
+1
source

, . (, PHP , ) session cookie. , , .

, , , . , , . , , .

, . - , , - . .

:

session_start(); //Make sure you call this at the top of EVERY page
if($passwordsMatch){
    $_SESSION['user'] = $_POST['username'];
}
//Now we have access to $_SESSION['user'] on every page.

:

session_start();
print "Welcome, ".$_SESSION['user'];
+1

- " " - PHP. session_start();, PHP cookie SESSION_ID , . $_SESSION, .

+1
+1

Source: https://habr.com/ru/post/1757522/


All Articles