Using Dynamic Web technology PHP to create a personal website full introduction

Source: Internet
Author: User
Tags array file size header html form html header newsfile php and strlen

Here and everyone to communicate with the Dynamic Web technology PHP to set up personal website experience. The wrong place, please correct me!

  Editing a debugging environment

Win98 local edit debugging, cool? First of the next omnihttpd proffesinal V2.06, installed on the PHP4BETA3 can be used. If you know more about HTML, recommend to use EditPlus, pretty good Oh! Otherwise have to use Dreamweaver first. Do you want to even have a database? Luckily, MySQL also has a for WIN32 version.

 Two, first, a counter.

Their counter is always better than the foreign, Shing well! The current introduction of the counter source code is always that kind of refresh a long, unavoidably some self-deception, hehe. But let's first analyze how it works, and here is an example of a simple counter:

<php $countfile = "Count.txt";
if (file_exists ($countfile))
{$fp =fopen ($countfile, "r+");
$count =fgets ($FP, 5); $count +=1; Rewind ($FP);
Fputs ($FP, $count, 5);
Fclose ($FP);
$FP =fopen ($countfile, "w");
$count = "1";
Fputs ($FP, $count, 5);
Fclose ($FP);

This counter has a nickname, called a brush on the long ^_^. So how do you make the counter more realistic? PHP4 gives us an easy way to use session-level variables. One into the page, first verify that the session count exists and equal to the value you want, not equal to the first, and then call the counting process, after the end of the conversation, you can say to count: Byebye you na!

But what if it's not PHP4? I wonder if you remember cookies? We can assume that a person's request for your Web page within 15 minutes (or other time) does not belong to a new number, so you can do a counting process exist in Inc, where each page references the visitor's access time to the cookie in the first entry. Each subsequent page is accessed to check the cookie's last access time value. Compare, if the difference is greater than 15 minutes to count, otherwise refresh time. (You don't even know how to use cookies, do you?) Oh, recommend a book "PHP Core Programming", Tsinghua version, check the function is enough, each function has a use example:-D)

However, just the counter is not beautiful to say, I guess you are certainly not satisfied, change the shape of the bar. Do a set of 0.jpg,1.jpg ... 9.jpg. Didn't you just read a string? First check the string length, enter your desired length (such as 5) minus the string length (remove the space after) a 0, and then from the beginning of a truncated number (in fact, or character), and ". jpg" combined after the output, the graphics counter is not out?

Well, a person's counter has been more perfect.

  Three first page press release, make your update easier (ON)

--------each time in the home page to add a message, add two words, you have to upload the entire page, it is not worth! So like the blue wind such a lazy man thought of a once and for all, real dynamic real-time, remote management editing, automatic maintenance functions. It can also be used to make bulletin boards for virtual communities. (If change, change it, you can put your news center of the news on the homepage automatically generated feed, hehe. So many functions, we forgive, hehe.

--------to insert a few topics aside, when we do the site, is the HTML in the middle of the embellishment of PHP, or PHP to generate HTML? A professional site can focus on a single page, using this page to invoke different modules to achieve each of the child functions (so that the client is not only one page?) I haven't tried it.) In any case, functional modular software reuse is an important method of efficient development (also can save your host space Oh ^_^).

--------Well, let's just come up and get the function modular. Beginners Please read the code carefully, especially the use of basic functions. Master can skip this paragraph, hehe (so seriously?) I was embarrassed to see 9-().

/*head.php in order to avoid being other people directly type download your source code, it is best to use PHP suffix, so that your server will first intercept it for processing, hehe
echo "echo "Web Teaching Network"//if the title is constant, you can do a function.
echo "</title>?>
This is a functional block that writes an HTML header.
/*makestr.php defines a function of a hyperlink in a public processing text, assuming that the link form in the text is the ${| Web page teaching Network}*/
function Makestr ($text)
{$str = $text;
$a =strstr ($s, ' $} ');//Remove substring before ' ${' in $s
if ($a) {
$b =strstr ($a, '} ');
if ($b) {
$la =strlen ($a); $ls =strlen ($s);//String length
$s =substr ($a, 0, $ls-$la);//Fetch substring, second argument to start, third argument to number of characters
$a =substr ($a, 2);//Remove ' ${'
$LB =strlen ($b); $la =strlen ($a);
$a =substr ($a, 0, $la-$lb);//Remove Hyperlink section
$b =substr ($b, 1);//Remove '} '
$ta =strstr ($a, "|"); /Find out if there is any text that needs to be linked
if ($ta) {
$la =strlen ($a); $lt = (strlen ($TA);
$linktext =substr ($a, $la-$lt + 1);
$a =substr ($a, 0, $la-$lt);
$linktext = $a;
$s = $s. " <a herf= "". $a. "" $linktext. " </a> $b;//Prepare to return a string
return ($s);

--------If you're a novice, you should look for an introductory HTML book. Otherwise it is hard to become a PHP master. These functions, which you begin to look very simple in handling strings, are capable of doing a lot of beautiful things with your ingenious application. Next, we can see that we prepared the results of most of the day, hehe.

  three first page press release, make your update easier (middle)

Last time we made a file header (as for the end of the file, please do it yourself, assuming for tail.php), a function of the module, now, we come to a basic function of the implementation, that is, dynamic release

include ("makestr.php";
include ("head.php");
$newspath = "/announce/";//directory of news files stored in text files
$newsfile =array ();//Prepare news array
$hd =dir ($newspath);//directory Handle
WH Ile ($filename = $hd->read ()) {//Get all Files
$s =strtolower ($filename);
if (strstr ($s, ". txt")) {
//detect the latest modification date
$lastchanged =fileatime ($newspath. $filename);
$newsfile [$filename]= $lastchanged;
Arsort ($newsfile);//files sorted by Time
//output file
for (reset ($newsfile); $key =key ($newsfile); Next ($newsf ile))
{$fa =file ($newspath. $key);
$n =count ($FA);
Echo <p>. Date ("D.m.y-h:i:s". $newsfile [$key]). " <br>\n ";
for ($i =0; $i < $n; $i = $i + 1) {
$s =chop ($fa [$i]);//Remove Space
$s =htmlspecialchars ($s);
Print $s. " </p>\n ";  
$hd->close ();//release handle
include ("tail.php");

This way, you can publish your news text to the annouce subdirectory of your root directory. But the real convenience is not this, for example, when the news is outdated, the program can automatically delete it, how good. Do not use FTP, directly online write down to the new post, more convenient. Okay, and listen to let's.

  three first page press release, make your update easier (next)

-----Last talk, we have achieved the basic function of the homepage press release. In this talk, we give it a building block, so that its function more powerful. (a good software, in fact, is the basic function plus a group of ancillary functions, these ancillary functions are mainly drawing a convenient just, hehe)

-----First, let's be clear about what we want to get. We do not want a few 10 days ago things we forgot to delete, and the results were released as news on the homepage (very humiliating Oh ^&^). If we embed a piece of code in a basic implementation, such as using Filectime ($filename), then, under UNIX, we get the time from January 1, 1970, in seconds to the last file changed (the date time the file was generated under Windows), And from January 1, 1970 onwards in seconds to the current number of seconds can be obtained by $mynow=mktime (), subtraction, judgment, IF > The time we expect, hehe, Sorry,unlink ($newspath. $filename) ... What the?! Where do you want to put this code? You say, hehe.

Perhaps, you are not often on a machine on the Internet, when you get a new news but not next to your love machine, how to do? The ideal thing is to upload files on the homepage. You may have a page (Admin?) Need to use a password to enter (if you still cherish their own website, this is necessary, it is best not to let others know the filename, hehe). Here is a section of code that is visible everywhere:

if (Isset ($upfile))

The uploaded file information is as follows:
File Store path: <?echo $upfile;? >
Original filename: <?echo $upfile _name;? >
File Size (bytes): <?echo $upfile _size;? >
File type: <?echo $upfile _type;? >

<form enctype= "Multipart/form-data" method= "post" action= "<?echo $PHP _self;? > ">
<input type= "hidden" name= "max_file_size" value= "50000" >
Please select or enter the upload file name: <input name= "upfile" type= "File" >
<input type= "Submit" value= "upload" name= "submit" >

-----Here we have seen a combination of PHP and HTML form, you can learn from. File uploads, while convenient, always give people a sense of insecurity. Once the leaks are compromised, your server's system security is compromised because the uploaded file may be a destructive execution file (that is, a virus or hacker program) it is prudent to write an online edited file, here is an example:

〈? Php
function Mywritefile ($filename, $contents) {
if ($fp =fopen ($filename, "w")) {
Fwrite ($fp, Strpslashes ($contents));
Fclose ($FP);
return 1;
else {return 0;};
Determine the variables returned by the form
if ($submitButton = = "complete") {
$rs =mywritefile ("$page", "$test");
echo "<a href =" $page "> $page </a><br>";
ElseIf ($action = = "page")//The case where the variable action value is "page"
{$contents = "$page";
$fp = @fopen ($content, "w+") or Die (' could not open file! ');
$contents =htmlspecialchars ($contents);
Fclose ($FP);
echo "<form action=" admin.php?page= $page "method=" POST "wrap=soft>";
echo "$contents";
echo "</textarea>";
echo "<input type=" Submit "Name=" Submitbutton "value=" complete ">";
echo "</form>";
{echo "<form action=" basename ($PHP _self).? Action=page "method=" POST ">", or the way to refer to variables within the program.
echo "<input type=text size=20 name=page>";
echo "<input type=" submit "name=" Send "value=" selected ">";
echo "</form>";

-----to this point, the home page news release of the implementation has been able to come to an ending. We can see that any one feature (comparison) of the powerful Chen program is composed of some simple functions. The third part of the function of the whole perfect process using the superposition of enhanced methods, and software engineering Top-down, layered analysis of the development method is not the same. This is a suitable for learning methods, write their own writing is very useful, hehe.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.