导航菜单

PHP/Web开发基础
课程进度 39% · 第9/22章9/22章 · 标签 1/2
1

表单与请求

PHP通过$_GET、$_POST、$_REQUEST获取请求数据。

php
1
<!-- form.html -->
2
<form method="POST" action="handler.php">
3
<input name="username" />
4
<input type="password" name="pwd" />
5
<button>提交</button>
6
</form>
7
 
8
<?php // handler.php
9
$name = $_POST['username'] ?? '';
10
$pwd = $_POST['pwd'] ?? '';
11
echo "欢迎, $name";
12
?>
2

Session与Cookie

php
1
<?php
2
session_start(); // 启动会话
3
$_SESSION['user_id'] = 1; // 设置session
4
 
5
setcookie("lang", "zh-CN", time()+3600); // 设置cookie
6
echo $_COOKIE['lang'] ?? '默认'; // 读取cookie
7
 
8
// 删除
9
session_destroy();
10
setcookie("lang", "", time()-3600);
11
?>
$_GET$_POSTsessionsetcookieform