How can the session_start() function be used to initiate a session in PHP?
To initiate a session in PHP, you can use the session_start() function at the beginning of your PHP script. This function creates a new session or resumes an existing one based on a session identifier passed via a GET or POST request, or a cookie. It is essential to call session_start() before accessing any session variables or using session-related functions.
<?php
// Start a new or resume an existing session
session_start();
// Access session variables or set new ones
$_SESSION['username'] = 'john_doe';
$_SESSION['user_id'] = 12345;
?>
Keywords
Related Questions
- What are the potential drawbacks of using mod_rewrite for URL redirection in PHP?
- How can PHP developers improve their code by using isset() and !empty() functions for form data validation?
- How can the readability and maintainability of PHP scripts be improved to avoid confusion and errors during development?