PHP Username Retrieval: A Complete Guide

To retrieve and display the username, you can use either the $_SESSION variable or the $_COOKIE variable to store the username.

Example code:

// 设置用户名到SESSION变量
session_start();
$_SESSION['username'] = 'John'; 

// 获取用户名
$username = $_SESSION['username'];

// 显示用户名
echo "Welcome, $username!";

Alternatively, you can use $_GET or $_POST to retrieve user inputted usernames.

if(isset($_POST['username'])){
    $username = $_POST['username'];
    echo "Welcome, $username!";
}

Please note that storing the username in the $_SESSION variable can keep the user’s state continuously after logging in, while using the $_GET or $_POST variable requires the user to input the username each time.

bannerAds