如何在PHP中使用PathInfo獲取URL參數(shù)

PHP
小樊
91
2024-08-14 17:54:35
欄目: 編程語言

在PHP中,可以使用$_SERVER['PATH_INFO']來獲取URL中的參數(shù)。PathInfo是一個(gè)URL中用于識(shí)別動(dòng)態(tài)頁面的一部分,通常包含在網(wǎng)址的末尾。例如,如果URL是http://example.com/index.php/user/profile/123,那么PathInfo部分就是/user/profile/123。

以下是一個(gè)簡(jiǎn)單的示例代碼,演示如何使用PathInfo獲取URL參數(shù):

<?php
// 獲取PathInfo
$pathInfo = $_SERVER['PATH_INFO'];

// 將PathInfo以'/'分隔成數(shù)組
$parts = explode('/', $pathInfo);

// 獲取URL參數(shù)
$user = $parts[1]; // 獲取'user'
$profile = $parts[2]; // 獲取'profile'
$id = $parts[3]; // 獲取'123'

// 輸出URL參數(shù)
echo "User: $user <br>";
echo "Profile: $profile <br>";
echo "ID: $id <br>";
?>

當(dāng)訪問http://example.com/index.php/user/profile/123時(shí),上面的代碼將輸出以下結(jié)果:

User: user
Profile: profile
ID: 123

0