PHP newbies on the road (6)_PHP tutorial

WBOY
Release: 2016-07-21 16:00:44
Original
876 people have browsed it

Building a simple interactive website (2)

5.5 Counter

Let us add a counter to the homepage. This example has been told many times, but it is still useful to demonstrate how to read and write files and create your own functions. counter.inc contains the following code:

/*
|| A simple counter
*/
function get_hitcount($counter_file)
{
/* Reset the counter to zero
In this way, if the counter has not been used, the initial value will be 1
Of course you can also set the initial value to 20000 to trick people
*/
$count =0;
// If the file storing the counter already exists, read its contents
if ( file_exists($counter_file) )
{
$fp=fopen($counter_file,"r") ;
// We only took the top 20, I hope your site will not be too popular
$count=0+fgets($fp,20);
// Since the function fgets() returns String, we can automatically convert it to an integer by adding 0
fclose($fp);
// File operation completed
}
// Increase the count value once
$count++;
//Write the new count value to the file
$fp=fopen($counter_file,"w");
fputs($fp,$count);
fclose($ fp);
# Return the count value
return ($count);
}
?>

Then we change the front.php3 file to display this counter:
include("include/counter.inc");
// I put the counter value in the file counter.txt, read it out and output it
printf ("

< B>%06d

n",
get_hitcount("counter.txt"));
include("include/footer.inc");
?>
Check out our new front.php3

5.6 Feedback Form

Let us add another feedback form for your visitors to fill out and e-mail to you. For example, we use a very simple method to implement it. We only need two pages: one to provide the viewer with an input form; the other to obtain the form data, process it, and mail it to you.

Obtaining form data in PHP is very simple. When a form is sent, each element contained in the form is assigned a corresponding value, and can be used like a reference to a general variable.




In process_form.php3, the variable $mytext is assigned the entered value - very simple! Similarly, you can get variable values ​​from form elements such as list boxes, multi-select boxes, radio boxes, buttons, etc. The only thing you have to do is give each element in the form a name so that you can reference it later.

Based on this method, we can generate a simple form containing three elements: name, e-mail address and message. When the visitor sends the form, the PHP page (sendfdbk.php3) that processes the form reads the data, checks whether the name is empty, and finally emails the data to you.

表单:form.php3
include("include/common.inc");
$title = "Feedback";
include("include/header.inc");
?>













include("include/footer.inc");
?>

处理表单:sendfdbk.php3
include("include/common.inc");
$title = "Feedback";
include("include/header.inc");
if ( $name == "" )  
{
// 现在我很讨厌匿名的留言!
echo "Duh ? How come you are anonymous?";
}  
elseif ($name == "Your name")  
{
// 这个浏览者真是不想透露姓名啊!
echo "Hello ? Your name is supposed to be replaced with
your actual name!";
}  
else  
{
// 输出一段礼貌的感谢语
echo "
Hello, $name.


Thank you for your feedback. It is greatly appreciated.


Thanking you


$MyName

$MyEmailLink
";
// 最后mail出去
mail($MyEmail, "Feedback.","
Name : $name
E-mail : $email
Comment : $comment  
");
}
include("include/footer.inc");
?>

  注意:如果在你的测试过程中,该程序末能正常工作,请查看你的PHP配置文件(PHP3为php3.ini,PHP4为php.in)有没有设置好。因为本程序需要您的PHP配置文件作如下的设置:

  首先,用NotePad打开你的php3.ini或是php.ini文件,查看一下[mail function]有没有设置好,默认的情况如下所示:
SMTP = localhost  
sendmail_from = me@localhost.com
给SMTP设置SMTP服务器,最好是你当地的SMTP服务器,我这里以21cn的SMTP服务器作为例子,然后,在sendmail_from处填上你的E-MAIL地址,例如可以改成这样:
SMTP = smtp.21cn.com
sendmail_from = pert@21cn.com  
修改后不要忘了重启Apache,IIS或PWS服务哦.  


5.7 简单的站内搜索引擎

  PHP可以调用外部程序。在Unix环境下我们可以利用程序grep实现一个简单的搜索引擎。我们可以做的稍微复杂一些:使用一个页面既输出一个表单供用户输入搜索字串又输出查询结果。

include("include/common.inc");
$title = "Search";
include("include/header.inc");
?>


" METHOD="POST">
"
SIZE="20" MAXLENGTH="30">



if ( ! empty($searchstr) )
{
// empty( ) is used to check whether the query string is empty
// If it is not empty, call grep query
echo "
n";
// Call grep to make all files case-insensitive Pattern query
$cmdstr = "grep -i $searchstr *";
$fp = popen( $cmdstr, "r" ); // Execute command and output pipe
$myresult = array() ; // Store query results
while( $buffer = fgetss ($fp, 4096))
{
// grep returns this format: File name: Number of lines in the matching string
// Therefore, we use the function split() to separate and process the data
list($fname, $fline) = split(":",$buffer, 2);
// We only output the result of the first match
if ( !defined($myresult[$fname]))
$myresult[$fname] = $fline;
}
// Now we store the result in the array, and we can process it below Output
if ( count($myresult) )
{
echo "
    n";
    while(list($fname,$fline) = each($myresult))
    echo "

  1. $fname : $fline
  2. n";
    echo "
n ";

else 
{
// If there are no search results
echo "Sorry. Search on $searchstr
returned no results.< ;BR>n";
}
pclose($fp);
}
?>
include("include/footer.inc");
?>


Note:

PHP_SELF is a built-in variable in PHP. Contains the current file name.
fgets() reads the file line by line, with a maximum length of 4096 (specified) characters.
fgetss() is similar to fgets(), except that it parses the output HTML tags.
split() has a parameter of 2, because we only need to split the output into two parts. Also need to omit ":".
each() is an array operation function, used to traverse the entire array more conveniently.
The functions of popen() and pclose() are very similar to fopen() and fclose(), except that pipeline processing is added.
Please note that the above code is not a good way to implement a search engine. This is just an example to help us learn PHP better. Ideally you should build a database of keywords and then search them. 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317014.htmlTechArticleBuilding a simple interactive website (2) 5.5 Counter Let us add a counter to the homepage. This example has been told many times, but it is still useful to demonstrate how to read and write files to...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template