2024年03月27日 http请求 get请求 get请求参数 php获取url参数 共享博客
HTTP请求类型 GET请求

请求参数以键值对的方式,附加到url地址上,称为查询字符串,用?号与当前脚本分隔
url格式: index.php?name=鹏仔&age=30
受url长度限制, GET方式传递的数据也是有限制的
服务器端脚本使用预定义变量数组 $_GET 进行接收
index.php?name=鹏仔&age=18&sex=2
【PS】 ?后面都是参数,多个参数用&连接分开,每个参数格式例子 name=鹏仔
<?php print_r($_GET); ?>
输出结果
Array ( [name] => 鹏仔 [age] => 18 [sex] => 2 )
<?php echo $_GET['name'] . '<br>'; echo $_GET['sex']; ?>
输出结果
鹏仔
2
例
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>GET</title>
    </head>
    <body>
        <form action="" method="get">
            <input type="email" name="email" value="" placeholder="请输入邮箱">
            <input type="password" name="password" value="" placeholder="请输入密码">
            <button>登录</button>
        </form>
    </body>
</html>
<?php
    // 判断是否存在 $_GET['email'] ,存在则输出
    if( isset($_GET['email']) ){
        echo '邮箱:' . $_GET['email'];
    }
    echo '<br>';
    // 判断是否存在 $_GET['password'] ,存在则输出
    if( isset($_GET['password']) ){
        echo '密码:' . $_GET['password'];
    }
?>输出结果
邮箱:344225443@qq.com
密码:123456
? 前面是条件,跟if的括号 () 效果一样
? 后面 : 前面,跟if {} 里面效果一样
:后面跟 else {} 里面的效果一样
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>GET</title>
    </head>
    <body>
        <form action="" method="get">
            <input type="email" name="email" value="" placeholder="请输入邮箱">
            <input type="password" name="password" value="" placeholder="请输入密码">
            <button>登录</button>
        </form>
    </body>
</html>
<?php
    echo isset($_GET['email']) ? '邮箱:' . $_GET['email'] : '';
    echo '<br>';    
    echo isset($_GET['password']) ? '密码:' . $_GET['password'] : '';
?><!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>GET</title>
    </head>
    <body>
        <form action="" method="get">
            <!-- 将用户输入的内容动态添加到value字段中, 创建具有粘性的表单 -->
            <input type="email" name="email" value="
                <?php
                    echo isset($_GET['email']) ?  $_GET['email'] : '';
                ?>
            " placeholder="请输入邮箱">
            <input type="password" name="password" value="
                <?php
                    echo isset($_GET['password']) ? $_GET['password'] : '';
                ?>
            " placeholder="请输入密码">
            <button>登录</button>
        </form>
    </body>
</html>
<?php
    echo isset($_GET['email']) ? '邮箱:' . $_GET['email'] : '';
    echo '<br>';    
    echo isset($_GET['password']) ? '密码:' . $_GET['password'] : '';
?>本文链接:http://so.lmcjl.com/news/473/