Examples of learning PHP message Program

Source: Internet
Author: User
Tags base64 exit count header connect variables rand oracle database




Most of the sites will take into account the interaction between users. At this time, use the function of the message board, can let the user stay to this one visit, or is some and the interactive information of the website.
In the design, it can be very simple to leave only the user's short message, can also be designed to classify the nature of a very complex Web BBS system. Of course, how to build a website of their own message board, according to the nature of the site and Web site developers ingenuity.

The example presented here is a simple listing of all the messages. For users to see more than one message at a time. The back-end message of the system is the Oracle 7.x version of the database system. The database name in the example is WWW, the connected user account is user38, and the password is iam3849. To use this example directly, you must first execute the following SQL instruction and create a guestbook data table.


CREATE TABLE Guestbook (
Serial VARCHAR2 (255) NOT NULL,
Ref VARCHAR2 (255) NULL,
ID char (8) NOT NULL,
Alias VARCHAR2 () not NULL,
IP varchar2 (1024) NULL,
Msgdatedate NOT NULL,
Email VARCHAR2 (1024) NULL,
MSG varchar2 NOT NULL,
Flagchar (1) Default 1,
Primary key (serial)
);


The above SQL field descriptions and details are shown in the following table

Ordinal field name record type record length field description limit Key
0 serial number serial VARCHAR2 255 NN PK
1 Reference serial number ref VARCHAR2 255 temporary reservation. For reply message function
2 Account ID char 8 user account NN
3 Anonymous alias varchar2 32 display name NN
4 URL IP varchar2 1024 internet IP
5 Time Msgdate Date NN
6 Email Email varchar2 1024
7 message content msg VARCHAR2 NN
8 Display Flags flag Char 1 0: not shown
1: Show (default)


In this section of the message board related programs, if you have added the user authentication function, you can leave the user's authentication account number in the Guestbook data table's account bar to facilitate webmaster to find the wrong sender in the future. Leave the field here first, and let the readers in need practice.

To use the program in this section, first install Oracle 7.x, and make sure that the sql*net on the Web Server side can successfully connect to the Oracle database. Then you have to add the--with-oracle=/home/oracle/product/7.3.2 option when compiling PHP, of course, it doesn't matter if you change the path, as long as the path is really Oracle's path. Please refer to the relevant books for details on Oracle installation and usage.

The following program is the user's message added to the guestbook message datasheet. To configure the user authentication function, you can check at the beginning of the program, the sender can confirm the identity of the message, and read the message does not have to check the identity. This configuration can prevent improper speech, but will not let the message function only a few people use.


<?php
//---------------------------
Add Message program addmsg.php
Author:wilson Peng
Copyright (C) 2000
//---------------------------
//
You can add the identity check function here
//
if (($alias!= "") and ($msg!= "")) {
Putenv ("oracle_sid=www");
Putenv ("Nls_lang=american_taiwan(China).zht16big5");
Putenv ("oracle_home=/home/oracle/product/7.3.2");
Putenv ("Ld_library_path=/home/oracle/product/7.3.2/lib");
Putenv ("Ora_nls=/home/oracle/product/7.3.2/ocommon/nls/admin/data");
Putenv ("Ora_nls32=/home/oracle/product/7.3.2/ocommon/nls/admin/data");

$handle =ora_logon ("User38@www", "iam3849") or die;
$cursor =ora_open ($handle);
Ora_commitoff ($handle);

$serial =md5 (rand ()) (uniqid);
$ref = "";
$id = $PHP _auth_user;
$ip = $REMOTE _addr;
$msg =base64_encode ($msg);
$flag = "1";
$query = "INSERT into guestbook (serial, ref, ID, alias, IP, msgdate, email, MSG, flag) VALUES (' $serial ', ' $ref ', ' $id ', ' $al IAS ', ' $ip ', sysdate, ' $email ', ' $msg ', ' $flag ');

Ora_parse ($cursor, $query) or die;
Ora_exec ($cursor);

Ora_close ($cursor);
Ora_logoff ($handle);

Header ("Location:./index.php");
Exit

} else {
?>
<title> Fill in message </title>
<body bgcolor=ffffff>
<form method=post action= ". echo $PHP _self?> ">
<table border=0 cellpadding=2 width=395>
<tr>
&LT;TD nowrap><font color=004080> Code nickname </font></td>
&LT;TD width=20%><input type=text Name=alias size=8></td>
&LT;TD nowrap><font color=004080> e-mail </font></td>
&LT;TD width=50%><input type=text Name=email size=18></td>
</tr>
<tr>
&LT;TD Nowrapvalign=top><font color=004080> Content </font></td>
&LT;TD width=80% colspan=3><textarea rows=5 name=msg cols=33></textarea></td>
</tr>
<tr>
&LT;TD width=100% colspan=4 align=center>
<input type=submit value= "send a Message" >
<input type=reset value= "Erase message" >
</td>
</tr>
</table>
</form>
</body>
<?php
}
?>


The above program in the implementation, first check the variable alias and msg whether there is information, if no information is sent to fill out the message form to the user side, for users to fill out the message.

If the user fills in the message, presses the "Send out Message" button, then executes the first half part of the program.

The program is probably divided into five parts.

Configure the environment variables required by Oracle
To connect to the Oracle database
Organize the data into Oracle
End connection to Oracle
End the program, display the latest message information
In configuring the Oracle environment, use the PHP function putenv () to configure the environment variables for the operating system layer. To use Chinese, remember to join the following line

Putenv ("Nls_lang=american_taiwan(China).zht16big5");

The function of the Oracle function library is then used: Ora_logon () and so on. See Oracle database function library for details. With this function library, Oracle databases can be easily manipulated.

Again, it's about organizing the data to be placed in an Oracle database.

$serial =md5 (rand ()) (uniqid);
$ref = "";
$id = $PHP _auth_user;
$ip = $REMOTE _addr;
$msg =base64_encode ($msg);
$flag = "1";
$query = "INSERT into guestbook (serial, ref, ID, alias, IP, msgdate, email, MSG, flag) VALUES (' $serial ', ' $ref ', ' $id ', ' $al IAS ', ' $ip ', sysdate, ' $email ', ' $msg ', ' $flag ');

$serial variable is a unique string, the program first randomly produces a unique string, and then uses MD5 encoding to confuse the string to form a meaningless string similar to the thinning process. Because of the long string, and become very messy, can prevent users, especially hackers or passengers use serial numbers to crack the system.

The $REF variable is currently invalid. $id variables for user authentication, if the program at the beginning of the application of the user authentication, then $PHP _auth_user will become the user's account, passed into the $id variable.

As for the string written by the user, it is simply encoded with BASE64 to prevent the complexity of the database or processing. You can let the characters of the strange character disappear together, of course, this is the Saw method, but for WEB applications, the implementation of fast, easy to modify is the most important, there is no need to waste energy to deal with these Chinese punching code problem. It is worth noting that the use of BASE64 encoding, will allow the string expansion of about 1/3, if the database storage space is limited, may not be suitable for this method, then said back, now the hard drive cheap, casual is more than more than 10 GB, should not consider the limited database space problem.

Finally, the variables are sorted into $query strings for use by the database to execute the SQL instructions.

Ora_parse ($cursor, $query) or die;
Ora_exec ($cursor);
Ora_close ($cursor);
Ora_logoff ($handle);

Before you can execute an Oracle SQL directive, go through the parse steps. If you precede with an @ (for example, @ora_prase ();), you can keep the user from seeing the error message. After you execute the query command, you can turn off the connection to Oracle.

Header ("Location:./index.php");
Exit

These two lines allow the browser to redirect to index.php. Let the user see his new message, the completion of the message step.

Then take a look at the message content display program.


<meta content= "text/html; charset=gb2312 "http-equiv=content-type>
<title> Message Board </title>
<body bgcolor=ffffff>
<?php
//---------------------------
Message Display program index.php
Author:wilson Peng
Copyright (C) 2000
//---------------------------

$WebmasterIPArray = Array (
"10.0.1.30",//management personnel a machine IP
"10.0.2.28"//management personnel B's machine IP
);

$WebmasterIP =false;
for ($i =0; $i <count ($WebmasterIPArray); $i + +) {
if ($REMOTE _addr = = $WebmasterIPArray [$i]) $WebmasterIP =true;
}

Putenv ("oracle_sid=www");
Putenv ("Nls_lang=american_taiwan(China).zht16big5");
Putenv ("oracle_home=/home/oracle/product/7.3.2");
Putenv ("Ld_library_path=/home/oracle/product/7.3.2/lib");
Putenv ("Ora_nls=/home/oracle/product/7.3.2/ocommon/nls/admin/data");
Putenv ("Ora_nls32=/home/oracle/product/7.3.2/ocommon/nls/admin/data");

$handle =ora_logon ("User38@www", "iam3849") or die;
$cursor =ora_open ($handle);
Ora_commitoff ($handle);

$query = "Select serial, ref, ID, alias, IP, To_char (msgdate, ' yyyy/mm/dd hh:mi:ss '), email, msg from guestbook where flag= ' 1 ' ORDER by msgdate Desc ';
Ora_parse ($cursor, $query) or die;
Ora_exec ($cursor);
$i = 0;
while (Ora_fetch ($cursor)) {
$guestbook [$i][0] = ora_getcolumn ($cursor, 0);
$guestbook [$i][1] = Ora_getcolumn ($cursor, 1);
$guestbook [$i][2] = Ora_getcolumn ($cursor, 2);
$guestbook [$i][3] = Ora_getcolumn ($cursor, 3);
$guestbook [$i][4] = Ora_getcolumn ($cursor, 4);
$guestbook [$i][5] = Ora_getcolumn ($cursor, 5);
$guestbook [$i][6] = Ora_getcolumn ($cursor, 6);
$guestbook [$i][7] = Ora_getcolumn ($cursor, 7);
$i + +;
}
Ora_close ($cursor);
Ora_logoff ($handle);

echo "<a href=addmsg.php> new message ... </a><p>\n";

if ($QUERY _string!= "") {
$page = $QUERY _string;
} else {
$page = 0;
}

$i =count ($guestbook);
$msgnum = 20; 20 Records per page
$start = $page * $MSGNUM;
$end = $start + $msgnum;
if ($end > $i) $end = $i;
$totalpage = $i/$msgnum;

$pagestr = "";
if ($page >0) $pagestr = $pagestr. " <a href=index.php? ". ($page-1). " >< prev </a>-";
$pagestr = $pagestr. " [subsection];
for ($i =0; $i < $totalpage; $i + +) {
if ($i!= $page) {
$pagestr = $pagestr. " <a href=index.php $i > ". ($i + 1). " </a> ";
} else {
$pagestr = $pagestr. ($i + 1). "";
}
}
$pagestr = $pagestr. "Page]";
if ($page < ($totalpage-1)) $pagestr = $pagestr. " -<a href=index.php? ". ($page + 1). " > next page ></a> ";

$pagestr = "<div align=center> $pagestr </div>";
echo "<p>". $pagestr. "
for ($i = $start; $i < $end; $i + +) {
echo "<p> echo "<p>\n<font color=e06060>". $guestbook [$i][5]. " </font> ";
if ($guestbook [$i][6]!= "") echo "<a Href=mailto:". $guestbook [$i][6]. " > ";
echo "<strong>" $guestbook [$i][3]. " </strong> ";
if ($guestbook [$i][6]!= "") echo "</a>";
echo "<br>\n";
if ($WebmasterIP) echo "<a href=erase.php?". $guestbook [$i][0]. " > Delete this article!!   </a> (". $guestbook [$i][2].") ";
echo "<font size=-1 color=c0c0c0>from:". $guestbook [$i][4]. " </font><p>\n ";
$msg =base64_decode ($guestbook [$i][7]);
$msg =nl2br ($msg);
Echo $msg;
echo "<p>\n";
}

echo "<p> Echo $pagestr;

?>
</body>

In the display of the message, considering the content of the message if a lot, coupled with the network slow, it may allow users to slow down the line when the entire database, so, as soon as possible to connect the database, to obtain the necessary information, immediately close the database, and then slowly to the user, should be the best

The procedure is divided into four parts

Class
Take the data from the database
Calculate the number of pages to display
Send out information



$WebmasterIPArray = Array (
"10.0.1.30",//management personnel a machine IP
"10.0.2.28"//management personnel B's machine IP
);

$WebmasterIP =false;
for ($i =0; $i <count ($WebmasterIPArray); $i + +) {
if ($REMOTE _addr = = $WebmasterIPArray [$i]) $WebmasterIP =true;
}
Then initialize the Oracle program slightly

The display program and the message program initialization part of the same, but the display program added a function, configure webmaster computer. The IP address used by the webmaster is added to the $WebmasterIPArray array variable, which displays the string to delete the message when the message is displayed, so as to facilitate improper handling of the message.

$handle =ora_logon ("User38@www", "iam3849") or die;
$cursor =ora_open ($handle);
Ora_commitoff ($handle);

$query = "Select serial, ref, ID, alias, IP, To_char (msgdate, ' yyyy/mm/dd hh:mi:ss '), email, msg from guestbook where flag= ' 1 ' ORDER by msgdate Desc ';
Ora_parse ($cursor, $query) or die;
Ora_exec ($cursor);
$i = 0;
while (Ora_fetch ($cursor)) {
$guestbook [$i][0] = ora_getcolumn ($cursor, 0);
$guestbook [$i][1] = Ora_getcolumn ($cursor, 1);
$guestbook [$i][2] = Ora_getcolumn ($cursor, 2);
$guestbook [$i][3] = Ora_getcolumn ($cursor, 3);
$guestbook [$i][4] = Ora_getcolumn ($cursor, 4);
$guestbook [$i][5] = Ora_getcolumn ($cursor, 5);
$guestbook [$i][6] = Ora_getcolumn ($cursor, 6);
$guestbook [$i][7] = Ora_getcolumn ($cursor, 7);
$i + +;
}
Ora_close ($cursor);
Ora_logoff ($handle);

After initialization, you can connect to the Oracle database and put the message out of the $guestbook array. After obtaining the information, we quickly shut down the database and then process the data of $guestbook array.

if ($QUERY _string!= "") {
$page = $QUERY _string;
} else {
$page = 0;
}

This procedure is judged by the number of pages to display, and the default is to display the first page. To display pages on the third page, you need to use the http://xxxxxx/index.php?2 format, which is the incoming $QUERY _string, and so on. The following lines of procedures are used to process the number of pages and numbers of data displayed.

$msgnum = 20; 20 Records per page

To change the number of display bars per page, you can change the $msgnum variable. The default value for the program is 20.

for ($i = $start; $i < $end; $i + +) {
echo "<p> echo "<p>\n<font color=e06060>". $guestbook [$i][5]. " </font> ";
if ($guestbook [$i][6]!= "") echo "<a Href=mailto:". $guestbook [$i][6]. " > ";
echo "<strong>" $guestbook [$i][3]. " </strong> ";
if ($guestbook [$i][6]!= "") echo "</a>";
echo "<br>\n";
if ($WebmasterIP) echo "<a href=erase.php?". $guestbook [$i][0]. " > Delete this article!!   </a> (". $guestbook [$i][2].") ";
echo "<font size=-1 color=c0c0c0>from:". $guestbook [$i][4]. " </font><p>\n ";
$msg =base64_decode ($guestbook [$i][7]);
$msg =nl2br ($msg);
Echo $msg;
echo "<p>\n";
}

This section of the program is really show message information to the user to see the program. Using the For loop, the data $guestbook array is taken out according to the configured number of pages and displayed to the user. It is worth mentioning that if you look at the message machine IP as a $WebmasterIPArray variable array of elements, it will be in the message of the nickname after the "delete this article!!" string, for managers to delete inappropriate messages.

The following is the procedure for deleting a message.


<?php
//---------------------------
Message Removal program erase.php
Author:wilson Peng
Copyright (C) 2000
//---------------------------
Putenv ("oracle_sid=www");
Putenv ("Nls_lang=american_taiwan(China).zht16big5");
Putenv ("oracle_home=/home/oracle/product/7.3.2");
Putenv ("Ld_library_path=/home/oracle/product/7.3.2/lib");
Putenv ("Ora_nls=/home/oracle/product/7.3.2/ocommon/nls/admin/data");
Putenv ("Ora_nls32=/home/oracle/product/7.3.2/ocommon/nls/admin/data");

$handle =ora_logon ("User38@www", "iam3849") or die;
$cursor =ora_open ($handle);
Ora_commitoff ($handle);

$query = "UPDATE Guestbook set flag= ' 0 ' where serial= '". $QUERY _string. "'";
Ora_parse ($cursor, $query) or die;
Ora_exec ($cursor);

Ora_close ($cursor);
Ora_logoff ($handle);

Header ("Location:./index.php");
?>


In fact, this program is very simple, as long as open the Oracle database, will delete the serial number of the Flag field set to 0 on it, do not have the data is really removed from the database.

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: info-contact@alibabacloud.com 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.