一、实验目的
(1)掌握PHP与表单;
(2)掌握PHP文件上传;
(3)掌握PHP数据库。
二、实验内容
(1) 实验1:PHP与表单:
(2) 实验2:文件上传:
(3) 实验3:用户登录。
云平台网址:(可以做一个实验主网页)
实验1网址:http://1.yangduo.applinzi.com/exp4_1.php
实验2网址:http://1.yangduo.applinzi.com/exp4_2.php
实验3网址:http://1.yangduo.applinzi.com/exp4_3.html
三、实验步骤及设计说明
(1) 实验1:
左边的文字部分排版,将<span>升级为块标签,设置右对齐。通过正则表达式,设置手机号码,手机验证码输入的格式,输入的位数。通过$_POST来获取传过来的数据。
(1) 实验2:
通过$_FILES接收传过来的文件,将文件用数组保存起来,将图片格式也用数组保存起来,按照实验要求遍历文件数组,判断是否是图片,若为图片则打印出来,若不是图片,直接给出下载链接,选做部分,多加一个判断,判断是否是所给类型,若在所给类型中,是图片且大小小于5mb,则打印图片,若是其他所给类型则给出链接,其他的直接略过。
(2) 实验3:
通过date('Y-m-d H:i:s')获取当前时间,主要是连接数据库,通过new mysqli();里面输入连接信息来连接数据库,通过query来执行,select_db来选择数据表。选做部分,主要通过setcookie来实现账号密码的保存,当传过来的账号密码为空时,判断是否有cookie,若有则将账号密码拿过来,没有则提示出错,自动跳转登录界面。
四、实验结果与结论:
(1) 实验1
输入信息:
结果:
(2) 实验2
选做:
(3) 实验3
输入PHP链接,自动跳转
五、实验中遇到的问题、解决方法及体会
(1) 在做实验二文件上传部分,无法实现文件上传,一开始以为是输入路径是,部分符号没有转义,结果发现是路径出了问题,忘记文件上传的文件夹和代码所在路径一致,导致文件无法上传,经过修改解决,通过该实验,学会了文件上传。
(4) 做实验三选做部分的时候,连接数据库没有问题,选择的时候出现了错误,以为是选择选择表名,其实是选择表的上一级那个库,导致没办法正确执行SQL语句,经过查资料解决了问题。做选做的时候,保存了cookie但是拿不出保存的账户密码,总是跳转登录界面,后来通过先判断传过来的东西进行判断,若存在cookie则直接拿出账号密码,实现php界面,若不存在直接跳转登录界面。
附录:程序源程序(在此贴上网页源代码)
(1) 实验1:
exp4_1:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
body{
margin: 0 auto;
width:500px;
line-height: 2;
}
body{
font-size: 14px;
}
.div{
margin: auto;
padding: 5px;
}
.s{
text-align: right;
display: block;
width: 150px;
float:left;
}
#phone,#psw,#name,#code{
width: 200px;
}
</style>
</head>
<body>
<div class="div">
<form action="mess.php" method="post" >
<div class="r1">
<label for="phone">
<span class="s">我的手机号:</span>
</label>
<input type="text" id="phone" name="phone1" pattern="^1[3‐9]\d{9}" required="required" autofocus="autofocus"/>
</div>
<div class="r2">
<label for="psw">
<span class="s">创建密码:</span>
</label>
<input type="password" id="psw" name="psw1"/>
</div>
<div class="r3">
<label for="name">
<span class="s">昵称:</span>
</label>
<input type="text" id="name" name="name1"/>
</div>
<div class="r4">
<span class="s">性别:</span>
<input type="radio" id="boy" value="男" name="sex" checked="checked"/>
<label for="boy" id="bb">男</label>
<input type="radio" id="girl" value="女" name="sex" />
<label for="girl" id="gg">女</label>
</div>
<div class="r5">
<span class="s">所在地:</span>
<select id="city" name="city1">
<option value="广州">广州</option>
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="深圳">深圳</option>
<option value="珠海">珠海</option>
</select>
</div>
<div class="r6">
<label for="code">
<span class="s">手机验证码:</span>
</label>
<input type="text" id="code" name="code1" pattern="^\d{6}"/>
</div>
<div class="right">
<span class="s"> </span>
<input type="submit" value="提交"/>
</div>
</form>
</div>
</body>
</html>
mess.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>mess</title>
<style type="text/css">
p span{
text-align: right;
display: block;
width: 150px;
float:left;
}
</style>
</head>
<body>
<p>
<span>你的手机号:</span>
<?php
echo $_POST['phone1'];
?>
</p>
<p>
<span>创建密码:</span>
<?php
echo $_POST['psw1'];
?>
</p>
<p>
<span>昵称:</span>
<?php
echo $_POST['name1'];
?>
</p>
<p>
<span>性别:</span>
<?php
echo $_POST['sex'];
?>
</p>
<p>
<span>所在地:</span>
<?php
echo $_POST['city1'];
?>
</p>
<p>
<span>所在区号:</span>
<?php
$cities=array("广州"=>"020","北京"=>"010","上海"=>"021","深圳"=>"0755","珠海"=>"0756");
$num=$_POST['city1'];
$keys=array_keys($cities);
$values=array_values($cities);
$num1;
for($i=0;$i<count($cities);$i++)
{
if($keys[$i]==$num)
$num1=$values[$i];
}
echo $num1;
?>
</p>
<p>
<span>手机验证码:</span>
<?php
echo $_POST['code1'];
?>
</p>
</body>
</html>
(2) 实验2:
exp4_2:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<form method="post" action="mess2.php"
enctype="multipart/form-data">
<label for="file1">上传文件:</label>
<input type="file" name="file1" /><br/>
<label for="file2">上传文件:</label>
<input type="file" name="file2" /><br/>
<label for="file3">上传文件:</label>
<input type="file" name="file3" /><br/>
<label for="file4">上传文件:</label>
<input type="file" name="file4" /><br/>
<label for="file5">上传文件:</label>
<input type="file" name="file5" /><br/>
<input type="submit" values="上传" />
</form>
</body>
</html>
mess2.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<?php
$f=array($_FILES['file1'],$_FILES['file2'],$_FILES['file3'],
$_FILES['file4'],$_FILES['file5']);
$i=0;
$pic=['image/gif','image/jpeg','image/png','image/jpg'];
foreach($f as $file)
{
$i++;
if($file['error']==0)
{
echo '文件',$i,'<br/>';
$t=$file['type'];
$filePath = 'upload/' . $file['name'];
move_uploaded_file($file['tmp_name'], $filePath);
if(in_array($t,$pic))
{
echo "<p><img src=\"{$filePath}\"/></p>";
echo '<hr/>';
}
else{
echo '文件下载:',"<a href=\"{$filePath}\">{$file['name']}</a>",'<br/>';
echo '<hr/>';
}
}
}
?>
</body>
</html>
选做:
<?php
$f=array($_FILES['file1'],$_FILES['file2'],$_FILES['file3'],
$_FILES['file4'],$_FILES['file5']);
$i=0;
$pic=['image/gif','image/jpeg','image/png','image/jpg'];
//选做
$type=['image/gif','image/jpg','image/jpeg','image/png','application/msword',
'application/pdf','application/vnd.ms-excel','application/vnd.ms-powerpoint',
'image/bmp',' text/html','text/plain'];
foreach($f as $file)
{
$i++;
if($file['error']==0)
{
$size=$file['size']/(1024*1024);
$t=$file['type'];
if(in_array($t,$type)&&$size<=5)
{
echo '文件',$i,'<br/>';
$filePath = 'upload/' . $file['name'];
move_uploaded_file($file['tmp_name'], $filePath);
if(in_array($t,$pic))
{
echo "<p><img src=\"{$filePath}\"/></p>";
echo '<hr/>';
}
else{
echo '文件下载:',"<a href=\"{$filePath}\">{$file['name']}</a>",'<br/>';
echo '<hr/>';
}
}
}
}
?>
(3) 实验3:
exp4_3.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<style type="text/css">
.s{
text-align: right;
display: block;
width: 100px;
float:left;
}
</style>
</head>
<body>
<h3>请输入用户名</h3>
<form action="mess3_1.php" method="post">
<label for="user">
<span class="s">用户名:</span>
</label>
<input type="text" id="user" name="user1"/>
<br />
<label for="psw">
<span class="s">密码:</span>
</label>
<input type="password" id="psw" name="psw1" />
<br />
<span class="s"> </span>
<input type="checkbox" id="remember" value=11 name="rem"/>
<label for="remember">
<span >记住我一周</span>
</label>
<br />
<span class="s"> </span>
<input type="submit" value="登录" />
</form>
</body>
</html>
mess3_1.php:
<?php
error_reporting(0);//关闭错误报告
$user=$_POST['user1'];
$psw=$_POST['psw1'];
//当账号密码为空时,若无cookie则返回登录界面,若有时拿出cookie
if(is_null($user) && is_null($psw)){
if(empty($_COOKIE['user']) && empty($_COOKIE['psw']))
header("Location:exp4_3.html");
$user=$_COOKIE['user'];
$psw=$_COOKIE['psw'];
}
$db=new mysqli("127.0.0.1","root","654321");
if ($db->connect_error)
die('链接错误: '. $db->connect_error);
$db->query("SET NAMES 'utf8'");
$db->select_db('mysql');
$sql="select * from user_one where id='%s' and psw='%s'";
$strSQL=sprintf($sql,$user,$psw);
$result=$db->query($strSQL);
$num=$result->num_rows;
if($num==1)
{
$row = $result->fetch_assoc();
echo "<h2>欢迎您 {$row['name']}</h2>";
echo '您上次的登录时间是 ',$row['time'],'<br/>';
$time=date('Y-m-d H:i:s');
$sql1="update user_one set time='".$time."' where id='".$user."'";
$db->query($sql1);
if($_POST['rem'])
{
setcookie("user",$user,time()+7*86400);
setcookie("psw",$psw,time()+7*86400);
}elseif(is_null($_POST['rem'])){
setcookie("user");
setcookie("psw");
}
}else{
echo '输入的账号密码有误!</br>';
echo "<a href='exp4_3.html'>重新登录</a>";
}
?>