Home Backend Development PHP Tutorial Driving novices on the road PHP novices on the road (6)

Driving novices on the road PHP novices on the road (6)

Jul 29, 2016 am 08:34 AM

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)
{
/* Return the counter to zero
This way, if the counter has not been used, the initial The value will be 1
Of course you can also set the initial value to 20000 to fool people
*/
$count=0;
// If the file to store 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 a string, we can automatically convert it to an integer by adding 0
fclose($fp);
// The file operation is 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 count value
return ($count);
}
?>
Then we change the front.php3 file to display this counter:
include("include/counter.inc");
// I put the count value Place it in the file counter.txt, read and output
printf ("

%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 for 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.
 Getting 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, check 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.
 According to 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: form.php3
include("include/common.inc");
$title = "Feedback";
include("include/header.inc");
?>









;
$title = "Feedback";
include("include/header.inc");
if ( $name == "" )
{
// Now I hate anonymous comments!
echo "Duh ? How come you are anonymous?";
}
elseif ($name == "Your name")
{
// This viewer really doesn’t want to be named!
echo "Hello ? Your name is supposed to be replaced with
your actual name!";
}
else
{
// Output a polite thank you
echo "
Hello, $name.


Thank you for your feedback.
// Finally send the email
mail($MyEmail, "Feedback.","
Name : $name
E-mail : $email
Comment : $comment
");
}
include("include/footer.inc ");
?>
  Note: If the program does not work properly during your test, please check whether your PHP configuration file (php3.ini for PHP3, php.in for PHP4) is set properly. Because this program requires the following settings in your PHP configuration file:
First, use NotePad to open your php3.ini or php.ini file and check whether [mail function] has been set up. The default situation is as follows:
SMTP = localhost
sendmail_from = me@localhost.com
Set SMTP server for SMTP, preferably your local SMTP server. I will use the SMTP server of 21cn as an example. Then, fill in your E-MAIL in sendmail_from The address, for example, can be changed to this:
SMTP = smtp.21cn.com
sendmail_from = pert@21cn.com
Don’t forget to restart the Apache, IIS or PWS service after modification.
5.7 Simple on-site search engine
  PHP can be called external program. In a Unix environment we can use the program grep to implement a simple search engine. We can make it a little more complicated: use a page to output a form for users to enter search strings and output query results.
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 queries all files in case-insensitive mode
$cmdstr = "grep -i $searchstr *";
$fp = popen( $cmdstr, "r" ); // Execute the command and output the pipe
$myresult = array(); // Store query results
while( $buffer = fgetss ($fp, 4096))
{
{
// grep returns this format: File name: the number of lines in the matching string
// So we use the function split () Separate and process 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 results in the array, we can process and output them
if ( count($myresult) )
{
echo "< ;OL>n";
while(list($fname,$fline) = each($myresult))
echo "

  • $fname : $fline
  • n";
    echo "n";
    }
    else
    {
    // If there are no query results
    echo "Sorry. Search on $searchstrreturned no results.
    n";
    }
    pclose($fp);
    }
    ?>
    include("include/footer.inc");
    ?>
    Comments :
    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.
    popen(), pclose() have very similar functions to fopen(), 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.​

    The above has introduced the PHP novice on the road (6), including the content of the novice driving on the road. I hope it will be helpful to friends who are interested in PHP tutorials.

    🎜
    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

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

    The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

    Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

    JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

    Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

    Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

    Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

    Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

    How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

    Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

    Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

    The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

    What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

    An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

    See all articles