PHP login and get user information

前端 未结 3 2016
离开以前
离开以前 2020-12-17 08:36

How can I get the user information when the user successfully login?

Here is my login.php code



        
3条回答
  •  不知归路
    2020-12-17 09:03

    You already have username and password coming from the POST array, you can store them in session like this:

    session_start(); // this will go on top of the page
    .........
    
    if($count == 1) {
        session_register(username);
        session_register(password);
    
        $_SESSION['username'] = $_POST['username']; // store username
        $_SESSION['password'] = $_POST['password']; // store password
    
        header('location: index.php');
      }
      else {
        $error = "Invalid Username or Password Please Try Again";
      }
    

    Later you can get them like:

    echo $_SESSION['username'];
    echo $_SESSION['password'];
    

    Note that you should destroy them in session logout file.

    Note 2 session_register has been DEPRECATED as of PHP 5.3.0. Simply use $_SESSION array for storing your values.

提交回复
热议问题