实例学习PHP之投票程序篇(一)
学习前的准备:
要想学习PHP,当然少不了要安装PHP啦,所以如果你是初次学习,请先阅读网络学院的文章:
PHP4.03在linux下的安装
PHP4.04在win98下的安装
PHP4.04在英文win2000下的安装
如果你找不到安装程序请到下面下载:
PHP4.04Beta WIN32 安装程序
PHP4.03源程序
PHP3.0.16 WIN32 安装程序
PHP3.0.16源程序
本篇是"实例学习 PHP 之表单处理"的后继篇,请对php处理表单不清楚的朋友先看前篇————实例学习 PHP 之表单处理。
大家学习完前一篇表单处理之后,是不是信心十足的想做点小程序呢?OK,下面我们来开始做一点真正有用的东西吧!一个可以应用的PHP的投票程序。在这篇里大家将学习到cookie技术的使用,PHP的数组操作及档案的处理。如何?大家准备好了吗?Let's go!
在开始具体的程序设计之前,我们先学习一下几个下面需要用到的重要概念和函数:
首先是cookie,我们需要用他来防止同一台机器进行重复投票。
那么什么是cookie?如果你的英文够好,又想吃块原味的cookie,那么先到这里来尝尝鲜;要不然你就只有吃地藏给你热的啦………(不好吃别扁我哟,:-)) )
cookie原义在美语中是小甜饼的意思,当然我们现在不是要吃饼,在这cookie指的是一个有许多限制的ASCII文件。它是由服务器发给用户用于记录着用户在浏览过程中的一些信息。Cookies的文件大小被限制在4K以内。 cookie的用途是非常多的,比如你到过的一些网站有记录你来过次数,那就多半是用了cookie。 在这里我们用他来记录访问者是否已经投过票。
在PHP里我们可以非常方便的用setcookie函数来使用cookie,cookie实际上是HTTP协议中header的一部分。因此setcookie函数必须在没有任何其它信息输出到浏览器之前调用。说简单点就是要在标志前用这个函数啦………。下面是setcookie的用法示例,来自星空浪子的PHP4中文手册,大家等下还可以再参照一下我们在程序中的具体用法。
setcookie
送出 Cookie 资讯到浏览器。
语法: int setcookie(string name, string value, int expire, string path, string domain, int secure);
传回值: 整数
函式种类: 网路系统
内容说明
本函式会跟着标头 Header 送出一段小资讯字串到浏览器。使用本函式要在送出 HTML 资料前,实际上 cookie 也算标头的一部份。本函式的参数除了第一个 name 之外,都是可以省略的。参数 name 表示 cookie 的名称;value 表示这个 cookie 的值,这个参数为空字串则表示取消浏览器中该 cookie 的资料;expire 表示该 cookie 的有效时间;path 为该 cookie 的相关路径;domain 表示 cookie 的网站;secure 则需在 https 的安全传输时才有效。
expire时间的格式如下:
Wdy, DD-Mon-YYYY HH:MM:SS GMT
GMT表示格林尼治标准时间
使用范例
dante@mpath.com (27-May-1999) 所提供的 setcookie() 及 header() 范例。
php
$status = 0;
if (isset($myTstCky) && ($myTstCky == "ChocChip")) $status = 1;
if (!isset($CCHK)) {
setcookie("myTstCky", "ChocChip");
header("Location: $PHP_SELF?CCHK=1");
exit;
}
?>
Cookie Check
bgcolor="#FFFFFF" text="#000000">
Cookie Check Status:
php
printf ('%sbr>;',
$status ? "00FF00" : "FF0000",
$status ? "PASSED!" : "FAILED!");
?>
怎么样?大家对cookie的用法是不是有所了解呢?地藏在这里告诉大家一个关于expire日期的小技巧,如果你想让cookie的expire日期为从当前算起的第三天。那么你可以使用time()函数,这个函数将返回一个以秒为单位的当前时间(注意哟!这个时间是包括了年月日的,是不是很奇怪?:)),那么如果你想把expire日期定为第三天,那么就是 time()+60*60*24*3。
PHP的数组使用非常简单,大家只要注意它默认的起始下标是象C语言一样从零开始的,当然你也可以自己设定它的下标,如下面这样:
$descArray=array(
1=>"英文:源代码、程序下载",
2=>"英文:php动态",
3=>"英文:新闻组、公告栏",
4=>"英文:教学类",
5=>"中文:源代码、程序下载",
6=>"中文:新闻组、公告栏",
7=>"中文:教学类" );
使用的时候 $descArray[1]= "英文:源代码、程序下载"。更绝的是你还可以………
$MyArray2 = array( "
地支" => array("子", "丑", "寅", "卯"),
"生肖" => array("鼠", "牛", "虎", "兔"),
"数字" => array(1, 2, 3, 4) );
用的时候$MyArray2["地支"][0]="子"; 怎么样?是不是很有人情味啊,:)
最后我们来看看PHP的档案处理,PHP中用于档案处理的函数有大概几十个,在我们这一节里,使用了其中的五个函数fopen(); fclose(); flock();fexists();fwrite(); 其中我想重点说一下flock();其它的大家就去自己查手册吧。
为什么要重点说flock()?因为这是一个对于网络编程非常重要的功能,我举个例子,两个人同时投票,而且选的都是选项A,假设他们同时打开数据文件,这时A的选票是2,然后两个进程都在原有的基础上加1,接着一个写入了数据,另外一个也跟着写完了写入,大家想这时会出现什么情况?A的选票是多少?正确结果应该是4,但实际上却会是3。为什么会这样?这就是因为网络的多人环境的特点啦,所以我们在投票前一定要先用flock()函数把文件锁住,投完后再打开文件让其它的进程进行操作,这样才能防止出现上面的那类错误。下面是flock函数的用法说明。
flock 锁住档案。
语法: boolean flock(int fp, int operation);
传回值: 布林值
函式种类: 档案存取
内容说明 本函式用来锁住档案,使别的行程无法存取。传入的参数 fp 为档案的指标。参数 operation 的值为下列的数字之一:
1 、表示设定锁住档案可以允许别的行程读取;
2 、表示只有该行程可以写入档案;
3 、表示读写均锁住;
4 、不锁住区块 (block)。
而本函式无论在 UNIX 或是 Windows 系列中的锁住效果都相近。执行成功则传回 true 值,否则传回 false 值。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
