1.安装集成PHP开发环境
下载地址:http://www.appservnetwork.com/index.php?newlang=chinese
软件名称:appserv-win32-2.5.10.exe
装好以后将php文档写在D:\AppServ\www目录下就可以打开了
eg. http://localhost/Untitled-5.php
Untitled-5为www目录下的文件名
2.今天学习了session和cookie的用法:其实不太明白他们是干什么的。
session 在服务器端,cookie 在客户端(浏览器)
1》session的使用-多页之间信息传递 :简单的说就是在另一个页面显示这个页面传过去的数据
Untitled-1.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SESSION使用表单部分</title>
</head>
<body>
<form action="Untitled-4.php" id="form1" name="form1" method="post" >
输入你的用户名:
<label>
<input type="text" name="user" id="user" />
</label>
<label>
<input name="button" id="button" type="submit" value="登录"/>
</label>
</form>
</body>
</html>
Untitled-4.php
<?
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>使用-注册</title>
</head>
<body>
<?php
if(!$_POST["user"]) //通过post
{
echo "输入用户名为空";
}
else
{
$user=$_POST["user"];
echo "你好".$user."<br>";
}
$_SESSION["username"]=$user;
echo "<a href='Untitled-5.php'>超链接测试按钮</a>";
?>
</body>
</html>
Untitled-5.php
<?
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>使用-第三页验证表单</title>
</head>
<body>
<?
echo "欢迎你,".$_SESSION["username"].",进入第三页";
?>
</body>
</html>
$_POST 变量用于收集来自 method="post" 的表单中的值。 action="welcome.php" (Untitled-4.php)文件现在可以通过 $_POST 变量来获取表单数据了。。$_POST 变量是一个数组
$_SESSION["username"]=$user;
SESSION 可以直接被赋值,不需要注册。赋值之后,另一个文件可以通过$_SESSION[]获取赋值的值。
2》cookie的使用 -》用户登录保存期限
Untitled6-.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户登录保存实例-COOKIE实例</title>
</head>
<body>
<form action="Untitled-7.php" method="post" name="form1" id="form1">
<p align="center">用户登录</p>
<table width="268" height="174" border="0" cellpadding="5" cellspacing="0" >
<tr>
<td width="81" align="center">用户登录</td>
<td width="141" align="center">
<label>
<input name="user" type="text" id="user" size="10"/>
</label>
</td>
</tr>
<tr>
<td width="81" align="center">密码</td>
<td align="center">
<label>
<input name="password" type="password" id="password" size="10"/>
</label>
</td>
</tr>
<tr>
<td align="center">保存期限</td>
<td align="center" width="141">
<label>
<select name="time" id="time">
<option value="1">不保存</option>
<option value="2">1小时</option>
<option value="3">1天 </option>
<option value="4">1月</option>
<option value="5">1年</option>
</select>
</label>
</td>
</tr>
<tr>
<td width="81" height="46"></td>
<td>
<input type="submit" name="button" id="button" value="提交"/>
<input type="reset" name="button2" id="button2" value="重置"/>
</td>
</tr>
</table>
<p> </p>
</form>
</body>
</html>
Untitled-7.php
<?
$username=$_POST["user"];
$time=$_POST["time"];
$password=$_POST["password"];
if(!$_POST["user"])
{
echo "没有输入用户名";
echo "<p>";
echo "<a href='Untitled-6.php'>重新登录</a>";
}
else
{
switch($time)
{
case 1:
$time=time();
break;
case 2:
$time=time()+60*60;
break;
case 3:
$time=time()+60*60*24;
break;
case 4:
$time=time()+60*60*24*30;
break;
case 5:
$time=time()+60*60*24*30*365;
break;
}
setcookie("username",$username,$time); //注册用户名
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 注册用户信息</title>
</head>
<body>
<?
echo "注册的用户名为:";
echo $_COOKIE["username"]."<br>";
echo "COOKIE的有效期为:";
switch($_POST["time"])
{
case 1:
echo "1";
break;
case 2:
echo "2";
break;
case 3:
echo "3";
break;
case 4:
echo "4";
break;
case 5:
echo "5";
break;
}
?>
</body>
</html>
语法
setcookie(name,value,expire,path,domain,secure)
这样就创建了一个名为 name的cookie全局变量, 之后如果需要访问,就采用$_COOKIE[]全局变量对其访问。
删除cookie :setcookie["username"];
必须将setcookie函数放在任何<html>或者<head>之前