How does the PathInfo variable work in PHP for handling virtual paths?
The PathInfo variable in PHP is used to extract information about the virtual path of a URL. It can be particularly useful for handling dynamic URLs and routing requests to the appropriate resources. To use the PathInfo variable, you can simply access it in your PHP code and parse the information as needed.
// Example of using PathInfo variable to handle virtual paths
$pathInfo = $_SERVER['PATH_INFO'];
if ($pathInfo == '/about') {
// Display the about page
echo 'This is the about page';
} elseif ($pathInfo == '/contact') {
// Display the contact page
echo 'This is the contact page';
} else {
// Handle other paths or display a 404 error
header("HTTP/1.0 404 Not Found");
echo '404 - Page not found';
}
Keywords
Related Questions
- What are the limitations of outputting PHP variables in an HTML file?
- What are potential security risks when accessing APIs in PHP and storing tokens in plain text?
- How can error messages, such as "supplied argument is not a valid MySQL result resource," be redirected to be sent as an email instead of displayed on the website?