Is it recommended to store user information like first and last name in session variables in PHP?
Storing sensitive user information like first and last name in session variables is not recommended as session data is stored on the server and can be accessed by other scripts running on the same server. It is better to store such information in a secure database and retrieve it when needed.
// Instead of storing user information in session variables, store it securely in a database
// Example of storing user information in a database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query to insert user information into the database
$sql = "INSERT INTO users (first_name, last_name) VALUES ('John', 'Doe')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();