Home > php教程 > php手册 > php中检查 url 链接是否已经有参数,添加 ? 或 &代码

php中检查 url 链接是否已经有参数,添加 ? 或 &代码

WBOY
Release: 2016-05-25 16:55:25
Original
1017 people have browsed it
在php中我们要如何检查 url 链接是否已经有参数,添加 ? 或 &呢,下面我来分享一个函数。
 代码如下 复制代码
$old_url = $_SERVER["REQUEST_URI"];
  
//检查链接中是否存在 ?
$check = strpos($old_url, '?');
  
//如果存在 ?
if($check !== false)
{
    //如果 ? 后面没有参数,如 hindex.php?
    if(substr($old_url, $check+1) == '')
    {
        //可以直接加上附加参数
        $new_url = $old_url;
    }
    else    //如果有参数,如:index.php?ID=12
    {
        $new_url = $old_url.'&';
    }
}
else    //如果不存在 ?
{
    $new_url = $old_url.'?';
}
  
echo $new_url;
?>



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template