How can session variables be used to persist MySQL connection information across multiple PHP files and what limitations do they have?
To persist MySQL connection information across multiple PHP files, you can store the connection details in session variables. This way, the connection information can be accessed and used throughout the user's session without the need to reconnect on each page. However, session variables have limitations such as being dependent on the user's session and potentially causing security risks if not handled properly.
<?php
session_start();
$_SESSION['db_host'] = 'localhost';
$_SESSION['db_user'] = 'username';
$_SESSION['db_pass'] = 'password';
$_SESSION['db_name'] = 'database_name';
// Establish MySQL connection using session variables
$connection = mysqli_connect($_SESSION['db_host'], $_SESSION['db_user'], $_SESSION['db_pass'], $_SESSION['db_name']);
// Use the $connection variable for MySQL queries
?>