PHP实用小程序(六)
<HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <title>检测操作系统和浏览器类型</title> </HEAD> <body alink="#FF0000" link="#000099" vlink="#CC6600" topmargin="8" leftmargin="20" bgColor="#FFFFFF"> <? /* 浏览器类型、版本 */ if(ereg( 'MSIE ([0-9].[0-9]{1,2})',$HTTP_USER_AGENT,$log_version)) { $browser_Ver=$log_version[1]; $browserType='IE'; } elseif(ereg( 'Opera ([0-9].[0-9]{1,2})',$HTTP_USER_AGENT,$log_version)) { $browser_Ver=$log_version[1]; $browserType='OPERA'; } elseif(ereg( 'Mozilla/([0-9].[0-9]{1,2})',$HTTP_USER_AGENT,$log_version)) { $browser_Ver=$log_version[1]; $browserType='MOZILLA'; } else { $browser_Ver=0; $browserType='OTHER'; } echo "<br><br>浏览器类型:<font color=red>$browserType $browser_Ver</font>"; /* 操作系统类型 */ if(strpos($HTTP_USER_AGENT,'Windows 9')) { $osType="Windows 9X"; } elseif(strpos($HTTP_USER_AGENT,'Windows NT')) { $osType="Windows NT"; } elseif(strpos($HTTP_USER_AGENT,'Windows 2000')) { $osType="Windows 2000"; } elseif(strpos($HTTP_USER_AGENT,'Mac')) { $osType="Mac"; } elseif(strpos($HTTP_USER_AGENT,'Unix')) { $osType="Unix"; } elseif(strpos($HTTP_USER_AGENT,'Linux')) { $osType="Linux"; }else $osType="Others"; echo "<br><br>操作系统类型:<font color=red>$osType</font>"; echo "<br><br>IP地址:<font color=red>$REMOTE_ADDR</font>"; ?> </bady> </html>
<? /*本例是用PHP4连接一个mysql数据库操作的演示, 实现连接打开一个库,并读取数据的基本功能。 */ ?> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <style type="text/css"> <!-- input { font-size:9pt;} A:link {text-decoration: underline; font-size:9pt;color:000059} A:visited {text-decoration: underline; font-size:9pt;color:000059} A:active {text-decoration: none; font-size:9pt} A:hover {text-decoration:underline;color:red} body,table {font-size: 9pt} tr,td{font-size:9pt} --> </style> <title>注册会员列表 - 读取mysql的测试</title> </HEAD> <body alink="#FF0000" link="#000099" vlink="#CC6600" topmargin="8" leftmargin="0" bgColor="#FFFFFF"> <br><br><center><font color=green size=3><b>注 册 会 员 列 表</b></font></center> <br> <table cellspacing=0 bordercolordark=#FFFFFF width="95%" bordercolorlight=#000000 border=1 align="center" cellpadding="2"> <tr bgcolor="#6b8ba8" style="color:FFFFFF"> <td width="5%" align="center" valign="bottom" height="19">ID</td> <td width="10%" align="center" valign="bottom">姓名</td> <td width="5%" align="center" valign="bottom">性别</td> <td width="5%" align="center" valign="bottom">年龄</td> <td width="20%" align="center" valign="bottom">联系电话</td> <td width="20%" align="center" valign="bottom">电子邮件</td> <td width="20%" align="center" valign="bottom">家庭住址</td> </tr> <? //连接到本地mysql数据库 $myconn=mysql_connect("localhost","root",""); //选择test为操作库 mysql_select_db("test",$myconn); $strSql="select * from reguser"; //用mysql_query函数从user表里读取数据 $result=mysql_query($strSql,$myconn); while($row=mysql_fetch_array($result))//通过循环读取数据内容 { ?> <tr> <td align="center" height="19"><?echo $row["id"]?></td> <td align="center"><?echo $row["userid"]?></td> <td align="center"><?echo $row["sex"]?></td> <td align="center"><?echo $row["age"]?></td> <td align="center"><?echo $row["tel"]?></td> <td align="center"><?echo $row["email"]?></td> <td align="center"><?echo $row["address"]?></td> </tr> <? } //关闭对数据库的连接 mysql_close($myconn); ?> </table> </BODY> </HTML>
<? //本例是用PHP4实现向一个mysql数据表添加记录, ?> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <style type="text/css"> <!-- input { font-size:9pt;} A:link {text-decoration: underline; font-size:9pt;color:000059} A:visited {text-decoration: underline; font-size:9pt;color:000059} A:active {text-decoration: none; font-size:9pt} A:hover {text-decoration:underline;color:red} body,table {font-size: 9pt} tr,td{font-size:9pt} --> </style> <title>会员注册 - 添加记录演示</title> </HEAD> <body alink="#FF0000" link="#000099" vlink="#CC6600" topmargin="8" leftmargin="0" bgColor="#FFFFFF"> <? if(strlen($userid)<1) { ?> <br><br><center><font color=green size=3><b>注 册 会 员 表 单</b></font></center> <br> <form action="<? echo $PHP_SELF; ?>" method="get" name="frmAdduser"> <table cellspacing=0 bordercolordark=#FFFFFF width="60%" bordercolorlight=#000000 border=1 align="center" cellpadding="2"> <tr bgcolor="#6b8ba8" style="color:FFFFFF"> <td width="100%" align="center" valign="bottom" height="19" colspan="2">请仔细填写以下内容</td> </tr> <tr> <td width="30%" align="right" height="19">姓名:</td> <td width="70%"><input type="text" name="userid" size="10" maxlength="18"></td> </tr> <tr> <td width="30%" align="right" height="19">性别:</td> <td width="70%"><input type="radio" name="sex" value="男" checked>男 <input type="radio" name="sex" value="女">女</td> </tr> <tr> <td width="30%" align="right" height="19">年龄:</td> <td width="70%"><input type="text" name="age" size="5" maxlength="2"></td> </tr> <tr> <td width="30%" align="right" height="19">联系电话:</td> <td width="70%"><input type="text" name="tel" size="20"></td> </tr> <tr> <td width="30%" align="right" height="19">电子邮件:</td> <td width="70%"><input type="text" name="email" size="20"></td> </tr> <tr> <td width="30%" align="right" height="19">家庭住址:</td> <td width="70%"><input type="text" name="address" size="30"></td> </tr> <tr> <td width="100%" align="center" colspan="2"><input type="submit" value="马上注册"> <input type="reset" value="全部重写"></td> </tr> </table> </form> <? } else { //连接到本地mysql数据库 $myconn=mysql_connect("localhost","root",""); mysql_select_db("test",$myconn); //将用户填写信息添加到数据库 $strSql="insert into reguser(userid,sex,age,tel,email,address) values('$userid','$sex',$age,'$tel','$email','$address')"; $result=mysql_query($strSql,$myconn) or die(mysql_error()); //关闭对数据库的连接 mysql_close($myconn); echo "<br><br><br><center>您已经成功注册成我们的会员!<br><br><br><br><a href=\"7-1.php\">查看会员资料</a></center>"; } ?> </BODY> </HTML>
<? //本例是用PHP4实现修改一个mysql数据表, ?> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <style type="text/css"> <!-- input { font-size:9pt;} A:link {text-decoration: underline; font-size:9pt;color:000059} A:visited {text-decoration: underline; font-size:9pt;color:000059} A:active {text-decoration: none; font-size:9pt} A:hover {text-decoration:underline;color:red} body,table {font-size: 9pt} tr,td{font-size:9pt} --> </style> <title>会员注册 - 修改记录测试</title> </HEAD> <body alink="#FF0000" link="#000099" vlink="#CC6600" topmargin="8" leftmargin="0" bgColor="#FFFFFF"> <br><br><center><font color=green size=3><b>注 册 会 员 信 息 编 辑 表 单</b></font></center> <br> <? //连接到本地mysql数据库 $myconn=mysql_connect("localhost","root",""); //选择test为操作库 mysql_select_db("test",$myconn); if(strlen($id)<1) { ?> <table cellspacing=0 bordercolordark=#FFFFFF width="95%" bordercolorlight=#000000 border=1 align="center" cellpadding="2"> <tr bgcolor="#6b8ba8" style="color:FFFFFF"> <td width="5%" align="center" valign="bottom" height="19">ID</td> <td width="10%" align="center" valign="bottom">姓名</td> <td width="5%" align="center" valign="bottom">性别</td> <td width="5%" align="center" valign="bottom">年龄</td> <td width="20%" align="center" valign="bottom">联系电话</td> <td width="20%" align="center" valign="bottom">电子邮件</td> <td width="20%" align="center" valign="bottom">家庭住址</td> </tr> <? $strSql="select * from reguser"; //用mysql_query函数从reguser表里读取数据 $result=mysql_query($strSql,$myconn); while($row=mysql_fetch_array($result))//通过循环读取数据内容 { ?> <tr> <td align="center" height="19"><?echo $row["id"]?></td> <td align="center"><a href="83.php?id=<?echo $row["id"]?>"><?echo $row["userid"]?></a></td> <td align="center"><?echo $row["sex"]?></td> <td align="center"><?echo $row["age"]?></td> <td align="center"><?echo $row["tel"]?></td> <td align="center"><?echo $row["email"]?></td> <td align="center"><?echo $row["address"]?></td> </tr> <? } ?> </table> <? } else if(strlen($userid)<1) { //将用户的信息填写到输入框 $strSql="select * from reguser where id=$id"; $result=mysql_query($strSql,$myconn) or die(mysql_error()); $row=mysql_fetch_array($result); ?> <form action="<? echo $PHP_SELF; ?>" method="get" name="frmAdduser"> <table cellspacing=0 bordercolordark=#FFFFFF width="60%" bordercolorlight=#000000 border=1 align="center" cellpadding="2"> <tr bgcolor="#6b8ba8" style="color:FFFFFF"> <td width="100%" align="center" valign="bottom" height="19" colspan="2">用户信息修改</td> </tr> <tr> <td width="30%" align="right" height="19">姓名:</td> <td width="70%"><input type="text" name="userid" size="10" maxlength="18" value="<?echo $row["userid"]?>"></td> </tr> <tr> <td width="30%" align="right" height="19">性别:</td> <td width="70%"> <?//隐藏用户ID?> <input type="hidden" name="id" value="<?echo $row["id"]?>"> <input type="radio" name="sex" value="男"<?if($row["sex"]=="男") print(" checked");?>>男 <input type="radio" name="sex" value="女"<?if($row["sex"]=="女") print(" checked");?>>女</td> </tr> <tr> <td width="30%" align="right" height="19">年龄:</td> <td width="70%"><input type="text" name="age" size="5" maxlength="2" value="<?echo $row["age"]?>"></td> </tr> <tr> <td width="30%" align="right" height="19">联系电话:</td> <td width="70%"><input type="text" name="tel" size="20" value="<?echo $row["tel"]?>"></td> </tr> <tr> <td width="30%" align="right" height="19">电子邮件:</td> <td width="70%"><input type="text" name="email" size="20" value="<?echo $row["email"]?>"></td> </tr> <tr> <td width="30%" align="right" height="19">家庭住址:</td> <td width="70%"><input type="text" name="address" size="30" value="<?echo $row["address"]?>"></td> </tr> <tr> <td width="100%" align="center" colspan="2"><input type="submit" value="修 改"> <input type="reset" value="取 消"></td> </tr> </table> </form> <? } //如果用户填写了资料则对数据进行更新 if(strlen($userid)>1) { $strSql="update reguser set userid='$userid',sex='$sex',age=$age,tel='$tel',email='$email',address='$address' where id=$id"; //更新该ID所对应的资料 $result=mysql_query($strSql,$myconn) or die(mysql_error()); echo "<br><br><br><center>您已经成功修改了用户信息!<br><br><br><br><a href=\"82.php\">查看会员资料</a></center>"; } //关闭对数据库的连接 mysql_close($myconn); ?> </BODY> </HTML>
<? //本例是用PHP4实现删除一个mysql数据记录, ?> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <style type="text/css"> <!-- input { font-size:9pt;} A:link {text-decoration: underline; font-size:9pt;color:000059} A:visited {text-decoration: underline; font-size:9pt;color:000059} A:active {text-decoration: none; font-size:9pt} A:hover {text-decoration:underline;color:red} body,table {font-size: 9pt} tr,td{font-size:9pt} --> </style> <title>会员注册 - 删除记录演示</title> </HEAD> <body alink="#FF0000" link="#000099" vlink="#CC6600" topmargin="8" leftmargin="0" bgColor="#FFFFFF"> <br><br><center><font color=green size=3><b>注 册 会 员 信 息 删 除 表 单</b></font></center> <br> <? //连接到本地mysql数据库 $myconn=mysql_connect("localhost","root",""); //选择test为操作库 mysql_select_db("test",$myconn); if(strlen($id)<1) { ?> <table cellspacing=0 bordercolordark=#FFFFFF width="95%" bordercolorlight=#000000 border=1 align="center" cellpadding="2"> <tr bgcolor="#6b8ba8" style="color:FFFFFF"> <td width="5%" align="center" valign="bottom" height="19">ID</td> <td width="10%" align="center" valign="bottom">姓名</td> <td width="5%" align="center" valign="bottom">性别</td> <td width="5%" align="center" valign="bottom">年龄</td> <td width="20%" align="center" valign="bottom">联系电话</td> <td width="20%" align="center" valign="bottom">电子邮件</td> <td width="20%" align="center" valign="bottom">家庭住址</td> </tr> <? $strSql="select * from reguser"; //用mysql_query函数从reguser表里读取数据 $result=mysql_query($strSql,$myconn); while($row=mysql_fetch_array($result))//通过循环读取数据内容 { ?> <tr> <td align="center" height="19"><?echo $row["id"]?></td> <td align="center"><a href="<?echo $PHP_SELF?>?id=<?echo $row["id"]?>"><?echo $row["userid"]?></a></td> <td align="center"><?echo $row["sex"]?></td> <td align="center"><?echo $row["age"]?></td> <td align="center"><?echo $row["tel"]?></td> <td align="center"><?echo $row["email"]?></td> <td align="center"><?echo $row["address"]?></td> </tr> <? } ?> </table> <? } else { //进行删除操作$id为删除的记录号 $strSql="delete from reguser where id=$id"; $result=mysql_query($strSql,$myconn) or die(mysql_error()); echo "<br><br><br><center>您已经成功删除了该ID的信息!<br><br><br><br><a href=\"$PHP_SELF\">查看会员资料</a></center>"; } //关闭对数据库的连接 mysql_close($myconn); ?> </BODY> </HTML>
<? /*本例是用PHP4通过ODBC访问数据库操作的演示*/ ?> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <style type="text/css"> <!-- input { font-size:9pt;} A:link {text-decoration: underline; font-size:9pt;color:000059} A:visited {text-decoration: underline; font-size:9pt;color:000059} A:active {text-decoration: none; font-size:9pt} A:hover {text-decoration:underline;color:red} body,table {font-size: 9pt} tr,td{font-size:9pt} --> </style> <title>注册会员列表 - 通过ODBC访问数据库演示</title> </HEAD> <body alink="#FF0000" link="#000099" vlink="#CC6600" topmargin="8" leftmargin="0" bgColor="#FFFFFF"> <br><br><center><font color=green size=3><b>注 册 会 员 列 表</b></font></center> <br> <table cellspacing=0 bordercolordark=#FFFFFF width="95%" bordercolorlight=#000000 border=1 align="center" cellpadding="2"> <tr bgcolor="#6b8ba8" style="color:FFFFFF"> <td width="5%" align="center" valign="bottom" height="19">ID</td> <td width="10%" align="center" valign="bottom">姓名</td> <td width="5%" align="center" valign="bottom">性别</td> <td width="5%" align="center" valign="bottom">年龄</td> <td width="20%" align="center" valign="bottom">联系电话</td> <td width="20%" align="center" valign="bottom">电子邮件</td> <td width="20%" align="center" valign="bottom">家庭住址</td> </tr> <? //连接到ODBC数据库 $myconn=odbc_connect("fengfeng","sa","123"); $strSql="select * from reguser"; /*执行查询*/ $result=odbc_do($myconn,$strSql); //Boolean odbc_fetch_row(integer result,integer row) while(odbc_fetch_row($result))//通过循环读取数据内容 { ?> <tr> <td align="center" height="19"><?echo odbc_result($result,1)?></td> <td align="center"><?echo odbc_result($result,2)?></td> <td align="center"><?echo odbc_result($result,3)?></td> <td align="center"><?echo odbc_result($result,4)?></td> <td align="center"><?echo odbc_result($result,5)?></td> <td align="center"><?echo odbc_result($result,6)?></td> <td align="center"><?echo odbc_result($result,7)?></td> </tr> <? } //关闭对数据库的连接 odbc_close("$myconn"); ?> </table> </BODY> </HTML>
<? //本例是用PHP4实现向一个ODBC数据表添加记录, ?> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <style type="text/css"> <!-- input { font-size:9pt;} A:link {text-decoration: underline; font-size:9pt;color:000059} A:visited {text-decoration: underline; font-size:9pt;color:000059} A:active {text-decoration: none; font-size:9pt} A:hover {text-decoration:underline;color:red} body,table {font-size: 9pt} tr,td{font-size:9pt} --> </style> <title>会员注册 - 添加记录演示</title> </HEAD> <body alink="#FF0000" link="#000099" vlink="#CC6600" topmargin="8" leftmargin="0" bgColor="#FFFFFF"> <? if(strlen($userid)<1) { ?> <br><br><center><font color=green size=3><b>注 册 会 员 表 单</b></font></center> <br> <form action="<? echo $PHP_SELF; ?>" method="get" name="frmAdduser"> <table cellspacing=0 bordercolordark=#FFFFFF width="60%" bordercolorlight=#000000 border=1 align="center" cellpadding="2"> <tr bgcolor="#6b8ba8" style="color:FFFFFF"> <td width="100%" align="center" valign="bottom" height="19" colspan="2">请仔细填写以下内容</td> </tr> <tr> <td width="30%" align="right" height="19">姓名:</td> <td width="70%"><input type="text" name="userid" size="10" maxlength="18"></td> </tr> <tr> <td width="30%" align="right" height="19">性别:</td> <td width="70%"><input type="radio" name="sex" value="男" checked>男 <input type="radio" name="sex" value="女">女</td> </tr> <tr> <td width="30%" align="right" height="19">年龄:</td> <td width="70%"><input type="text" name="age" size="5" maxlength="2"></td> </tr> <tr> <td width="30%" align="right" height="19">联系电话:</td> <td width="70%"><input type="text" name="tel" size="20"></td> </tr> <tr> <td width="30%" align="right" height="19">电子邮件:</td> <td width="70%"><input type="text" name="email" size="20"></td> </tr> <tr> <td width="30%" align="right" height="19">家庭住址:</td> <td width="70%"><input type="text" name="address" size="30"></td> </tr> <tr> <td width="100%" align="center" colspan="2"><input type="submit" value="马上注册"> <input type="reset" value="全部重写"></td> </tr> </table> </form> <? } else { //连接到ODBC数据库 $myconn=odbc_connect("fengfeng","sa","123"); //将用户填写信息添加到数据库 $strSql="insert into reguser(userid,sex,age,tel,email,address) values('$userid','$sex',$age,'$tel','$email','$address')"; $result=odbc_do($myconn,$strSql); //关闭对数据库的连接 odbc_close("$myconn"); echo "<br><br><br><center>您已经成功注册成我们的会员!<br><br><br><br><a href=\"7-5.php\">查看会员资料</a></center>"; } ?> </BODY> </HTML>
<html> <head> <title>用户登录</title> </head> <body alink="#FF0000" link="#000099" vlink="#CC6600" topmargin="8" leftmargin="0" bgColor="#FFFFFF"> <br> <? if($userid!="") { mysql_connect("localhost", "root", ""); mysql_select_db("test") or die ("不能连接到数据库!"); //判断用户名和密码是否在正确 $strSql="select * from users where userid='$userid' and password='$password'"; $result=mysql_query($strSql);// or die("读取数据库出现错误!"); $num=mysql_numrows($result); mysql_close($myconn); if($num>0) { echo "<center>登录成功!</center><br>"; } else echo "<center><font color=red>登录失败!请检查用户名和密码!</font></center><br>"; } else { echo "<center>请输入用户名和密码登录!</center>" ?> <form action="<?echo $PHP_SELF?>" method="post"> <table cellspacing=0 bordercolordark=#FFFFFF width="60%" bordercolorlight=#000000 border=1 align="center" cellpadding="2"> <tr bgcolor="#6b8ba8" style="color:FFFFFF"> <td width="100%" align="center" valign="bottom" height="19" colspan="3">用户登录</td> </tr> <tr> <td width="30%" align="center">用户名:</td> <td width="70%" align="center"><input type="text" name="userid" size="20" maxsize="20"></td> </tr> <tr> <td width="30%" align="center">密 码:</td> <td width="70%" align="center"><input type="password" name="password" size="20" maxsize="20"></td> </tr> <tr> <td width="100%" align="center" colspan="2"><input type="submit" value="登录"></td> </tr> </table> </form> <? } ?> </body> </html>
<html> <head> <title>注册新用户</title> </head> <body alink="#FF0000" link="#000099" vlink="#CC6600" topmargin="8" leftmargin="0" bgColor="#FFFFFF"> <br> <? if($userid!="") { $ID = uniqid("userID"); $db = mysql_connect("localhost","root",""); mysql_select_db ("test"); $result = mysql_query ("insert into users (id, username, userid, password, email) VALUES ('$ID', '$username', '$userid', '$password','$email')"); if(!$result) { echo "<center>出现错误:</center>", mysql_error(); exit; } if($result) { mysql_close($db); echo "<center>用户 <b>$username</b> 注册成功!</center>"; } } else { echo "<center>资料填写不完整,请仔细填写!</center>"; ?> <form method="post" action="<?echo $PHP_SELF?>"> <table cellspacing=0 bordercolordark=#FFFFFF width="60%" bordercolorlight=#000000 border=1 align="center" cellpadding="2"> <tr bgcolor="#6b8ba8" style="color:FFFFFF"> <td width="100%" align="center" valign="bottom" height="19">注册新用户</td> </tr> <tr> <td width="100%" align="center"> 姓名:<INPUT TYPE=text MAXLENGTH=20 NAME="username" SIZE=20><Br> 呢称:<INPUT TYPE=text MAXLENGTH=20 NAME="userid" SIZE=20><Br> 密码:<Input Type=text Maxlength=20 Name="password" Size=20><Br> 邮件:<Input Type=text Maxlength=50 Name="email" Size=20><Br> <INPUT TYPE=submit VALUE="注 册"> <INPUT type=reset VALUE="重 填"> </td> </tr> </table> </form> <? } ?> </body> </html>