Example of creating a message board with registration and logon in PHP + MYSQL

Source: Internet
Author: User
Tags md5 php file php code setcookie

System environment: WAMP

IDE: NETBEANS

SQL tool: NAVICAT

Display effect

As shown in the following figure:

1. Import MYSQL data

Import the following SQL statement into the database, assuming the database name is called bbs, here ensure that the library and table fields are encoded in UTF-8 mode

-------------------------------------------

Create table 'message '(

'Id' tinyint (1) not null auto_increment,

'User' varchar (25) not null,

'Title' varchar (50) not null,

'Content' tinytext not null,

'Lastdate' date not null,

Primary key ('id ')

) ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 1;

--------------------------------------------

2. Some PHP code

Http://www.111cn.net/phper/php/add.php

------------------------------------------------

<?

// Load the conn. Php file

Include ("conn. php ");

// Load the head. Php file

Include ("head. php ");

// Submit the form to the database

If ($ _ POST ['submit ']) {

$ SQL = "insert into message (id, user, title, content, lastdate )".

"Values ('', '$ _ POST [user]', '$ _ POST [title]', '$ _ POST [content]', now ())";

Mysql_query ($ SQL );

Echo"

";

}

?>

User:
   

Title:

Content:


------------------------------------------------

Head. php

-------------------------------------------------

Add message | browse message | login

-------------------------------------------------

Http://www.111cn.net/phper/php/list.php

-------------------------------------------------

<?

// Load the conn. Php file

Include ("conn. php ");

// Load the head. Php file

Include ("head. php ");

// Set the paging value to 5

$ Pagesize = 5;

// Obtain the complete address path after the domain name

$ Url = $ _ SERVER ["REQUEST_URI"];

// Obtain the url array of the fixed key value [path] [query]

$ Url = parse_url ($ url );

// Print_r ($ url );

// Obtain the url's [path] path value

$ Url = $ url [path];

// Obtain the test table information

$ Numq = mysql_query ("select * from message ");

// Obtain the number of rows in the test table

$ Num = mysql_num_rows ($ numq );

// Determine whether the page parameter value can be obtained

If ($ _ GET [page]) {

// Pass the value to pageval

$ Pageval = $ _ GET [page];

// Calculate the page value for SQL statements.

$ Page = ($ pageval-1) * $ pagesize;

// Equivalent to $ page = $ page. ',' means to connect the page parameter value

$ Page. = ",";

}

// Determines whether the total number of database entries is greater than the page number.

If ($ num> $ pagesize ){

// Determine the value of the previous and next pages. If the value is smaller than 0, it is processed as 0.

If ($ pageval <= 1)

$ Pageval = 1;

// Display the page

Echo "$ num total ".

// (...). Used to connect the variable name

"Next page of the previous page ";

}

?>

<?

$ SQL = "select * from message order by id desc limit $ page $ pagesize ";

$ Query = mysql_query ($ SQL );

While ($ row = mysql_fetch_array ($ query )){

?>

Title:User:


Content:

Time:


<?

}

?>


-------------------------------------------------

Conn. php

--------------------------------------------------

<?

// Log on to the MYSQL database

$ Conn = @ mysql_connect ("localhost", "root", "") or die ("database link error ");

// Enter the BBS Library

Mysql_select_db ("bbs", $ conn );

// Transmit data streams using UTF-8 Chinese encoding

Mysql_query ("set names 'utf8 '");

// Modify the space and carriage return encoding to HTML identifiable encoding.

Function htmtocode ($ content ){

$ Content = str_replace ("n ","
", Str_replace (" "," ", $ content ));

Return $ content;

}

?>

--------------------------------------------------

Http://www.111cn.net/phper/php/login.php

---------------------------------------------------

<?

// Load the conn. Php file

Include ("conn. php ");

// Determine whether to exit and mark the cookie as out. The logon page is returned.

If ($ _ GET [out]) {

Setcookie ("cookie", "out ");

Echo"

";

}

// Submit a form to check whether the logon ID is admin, whether the password matches the MD5 value of 'php', and mark the cookie as OK

If ($ _ POST [id] = 'admin '){

$ Pw = md5 ($ _ POST [pw]);

If ($ pw = 'e1bfd762321e409cee4ac0b6e841963c '){

Setcookie ("cookie", "OK ");

// Refresh the logon page to enable the cookie flag value.

Echo"

";

}

}

// Load the head. Php file

Include ("head. php ");

// When the cookie flag value is not OK, the logon page is displayed. Otherwise, the exit page is displayed.

If ($ _ COOKIE ['cooker']! = 'OK '){

?>

User name:

Password:


<?

} Else {

?>

Exit

<?

}

?>

Related Article

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.