PHP-PHP常用功能模块

PHP常用功能模块

一、编写一个统计统计投票数的程序,要求投票的计数保存在文本文件中。

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>统计投票数</title>
  <style>
    table {
   
			margin: 0 auto;
		}
		h3,p {
   
			text-align: center;
		}
		p {
   
			font-size: 20px;
			color: red;
		}
	</style>
</head>

<body>
  <form action="" enctype="multipart/form-data" method="post">
    <table>
      <tr>
        <td bgcolor="#CCCCCC">
          <div>当前最流行的Web开发语言</div>
        </td>
      </tr>
      <tr>
        <td><input type="radio" name="vote" value="PHP">PHP</td>
      </tr>
      <tr>
        <td><input type="radio" name="vote" value="ASP">ASP</td>
      </tr>
      <tr>
        <td><input type="radio" name="vote" value="JSP">JSP</td>
      </tr>
      <tr>
        <td><input type="submit" name="sub" value="请投票">请投票</td>
      </tr>
    </table>
  </form>
  <?php 
		$votefile = "vote.txt";     //用于计数的文本文件
		if (!file_exists($votefile)) {
      //判断文本是否存在
			/*$handle = fopen($votefile, "w+"); //若不存在,则创建该文件 fwrite($handle, "0|0|0"); //将文件内容初始化 fclose($handle);*/
			file_put_contents($votefile, "0|0|0");
		}
		if (isset($_POST['sub'])) {
       //判断用户是否提交
			if (isset($_POST['vote'])) {
     //判断用户是否投票
				$vote = $_POST['vote']; //接收投票值

				/*$handle = fopen($votefile, "r+"); $votestr = fread($handle, filesize($votefile)); fclose($handle);*/
				$votestr = file_get_contents($votefile);

				$votearray = explode("|", $votestr);  //explode() ---> 字符串转化为数组
				echo "<h3>投票完毕!</h3>";
				if ($vote == 'PHP') {
   
					$votearray[0]++;
				}
				echo "<p>目前PHP的票数为:<span>".$votearray[0]."</span></p>";
				if ($vote == 'ASP') {
   
					$votearray[1]++;
				}
				echo "<p>目前ASP的票数为:<span>".$votearray[1]."</span></p>";
				if ($vote == 'JSP') {
   
					$votearray[2]++;
				}
				echo "<p>目前JSP的票数为:<span>".$votearray[2]."</span></p>";
				//计算总票数
				$sum = $votearray[0]+$votearray[1]+$votearray[2];
				echo "<p>总票数为:<span>".$sum."</span></p>";
				$votestr2 = implode("|", $votearray); //implode() ---> 数组转换为字符串
				/*$handle=fopen($votefile, "w+"); fwrite($handle, $votestr2); fclose($handle);*/
				file_put_contents($votefile, $votestr2);
			}
			else {
   
				echo "<script>alert('未选择投票选项!');</script>";
			}
		}
 	?>
</body>

</html>

vote.txt:

9|10|7

二、实现HTML表单的文件上传,并实现文件夹创建、不限定文件类型、程序控制上传文件大小、文件名加上传日时和随机号等功能。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>文件上传</title>
</head>
<body>
	<div align="center">
		<p style="font-size: 26px; font-family: '华文楷体'; color: blue;">PHP--文件上传--PHP</p>
		<form action="" enctype="multipart/form-data" method="post">
			<input type="file" name="myFile">
			<input type="submit" name="up" value="上传文件">
		</form>
	</div>

	<div align="center">
		<!-- PHP 代码 -->
	<?php 
	/*if (isset($_POST['up'])) { if ($_FILES['myFile']['type'] == "image/gif") { if ($_FILES['myFile']['error'] > 0) { echo "错误:".$_FILES['myFile']['error']; } else { $tmp_filename = $_FILES['myFile']['tmp_name']; $filename = $_FILES['myFile']['name']; $dir = 'sy5upload/'; if (is_uploaded_file($tmp_filename)) { if (move_uploaded_file($tmp_filename, "$dir.$filename")) { echo "文件上传成功!"; echo "文件大小为:".($_FILES['myFile']['size']/1024)."KB"; } else { echo "上传文件失败!"; } } } } else { echo "文件格式非GIF图片!"; } }*/

	if (isset($_POST['up'])) {
   
			if ($_FILES['myFile']['error'] > 0) {
   
				echo "错误:".$_FILES['myFile']['error'];
			}
			else {
   
				if ($_FILES['myFile']['size'] > 0 && $_FILES['myFile']['size'] < 1024*1024*2) {
   
					$dir = 'image/';   //设置保存位置
					if (!is_dir($dir)) {
      //如果没有该目录则创建
						mkdir($dir);
					}
					$tmp_filename = $_FILES['myFile']['tmp_name'];   //临时文件名
					$filename = $_FILES['myFile']['name'];    //上传的文件名
					$rand = rand(100, 999);     //生成一个三位随机数
					$filename = date('YmdHis').$rand.$filename;    //重新组合文件名
					if (is_uploaded_file($tmp_filename)) {
        //判断是否通过HTTP POST上传
						if (move_uploaded_file($tmp_filename, "$dir$filename")) {
      //上传并移动文件
							echo "文件上传成功!";
							echo "文件大小为:".($_FILES['myFile']['size']/1024)."KB";   
						}
						else {
   
							echo "文件上传失败!";
						}
					}
				}
				else {
   
					echo "文件大小超过了2MB";
				}
			}
	}
 ?>
	</div>
	
</body>
</html>

三、创建随机生成验证码的图片

SY5_3.php:

<?php 
	header('Content-type:image/gif');   //输出头信息

	$image_w = 100;   //验证码图形的宽
	$image_h = 25;   //验证码图形的高

	$number = range(0, 9);   //定义一个成员为数字的数组
	$character = range("Z", "A");   //定义一个成员为大写字母的数组
	$result = array_merge($number, $character);   //合并两个数组
	$string = "";  //初始化
	$len = count($result);   //新数组的长

	for ($i=0; $i < 4; $i++) {
    
		$new_number[$i] = $result[rand(0, $len - 1)];   //在$resulr数组中随机取出4个字符
		$string = $string.$new_number[$i];  //生成验证码字符串
	}

	$check_image = imagecreatetruecolor($image_w, $image_h);  //创建图片对象
	$white = imagecolorallocate($check_image, 255, 255, 255);
	$black = imagecolorallocate($check_image, 0, 0, 0);
	imagefill($check_image, 0, 0, $white);    //设置背景颜色为白色

	for ($i=0; $i < 100; $i++) {
       //加入100个干扰的黑点
		imagesetpixel($check_image, rand(0, $image_w), rand(0, $image_h), $black);
	}

	for ($i=0; $i < count($new_number); $i++) {
      //在背景图片中循环输出4位验证码
		$x = mt_rand(1, 8) + $image_w*$i/4 ; //设定字符所在位置X坐标
		$y = mt_rand(1, $image_h/4);   //设定字符所在位置Y坐标
		//随机设定字符颜色
		$color = imagecolorallocate($check_image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
		//输入字符到图片中
		imagestring($check_image, 5, $x, $y, $new_number[$i], $color);
	}

	imagepng($check_image);   //输出图片
	imagedestroy($check_image);
 ?>

SY5_3.html:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<!-- 引用验证码 -->
	<img src="SY5_3.php" alt="">
</body>
</html>

访问SY5_3.php

访问SY5_3.html:

四、由用户输入自己的生日,系统帮助用户计算出年龄和出生时是星期几。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>计算年龄</title>
</head>
<body>
	<form action="" method="post">
		请输入您的出生日期:
		<input type="text" name="year" size="4"><input type="text" name="month" size="4"><input type="text" name="day" size="4"><input type="submit" name="submit" value="提交">
	</form>
</body>
</html>
<?php 
	date_default_timezone_set('PRC');   //设置时区
	if (isset($_POST['submit'])) {
   
		$year = $_POST['year'];  //年份
		$month = $_POST['month'];   //月份
		$day = $_POST['day'];   //天数
		if (@checkdate($month, $day, $year)) {
   
			echo "您的年龄为:".(date('Y', time()) - $year);   //计算年龄
			$array = getdate(strtotime("$year - $month - $day"));   //使用getdate函数得到指定日期的信息
			echo "<br>出生时是:".$array['weekday'];   //输出星期值
		}
		else {
   
			echo "<script>alert('无效的日期!');</script>";
		}
	}
 ?>

创作不易,喜欢的话加个关注点个赞,谢谢谢谢

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务