Programming HandBook part 77 pptx

6 235 0
Programming HandBook part 77 pptx

Đang tải... (xem toàn văn)

Thông tin tài liệu

print("Bạn đang sử dụng: $browser") ?> hoặc PHP Code: print("<font color=\"red\"> Bạn đang sử dụng: $browser <font>") Tạo 1 cái Shoutbox đơn giản Bạn làm theo các bước sau đây. B1: Tạo 1 DB như sau: PHP Code: CREATE TABLE `tagboard` ( `id` int(7) NOT NULL auto_increment, `name` varchar(250) NOT NULL default '', `comments` text NOT NULL, `date` timestamp(14) NOT NULL, PRIMARY KEY (`id`), ) TYPE=MyISAM AUTO_INCREMENT=419 ; B2:Viết 1 file config.php có nội dung như sau: PHP Code: <? $dbuser=""; //Database Username $dbname=""; //Database Name $dbpass=""; // Database Password ?> B3:Tạo 1 file tag.php có nôi dung như sau: PHP Code: <iframe src="view.php" name="tag" width="179" height="130" frameborder =0 marginwidth="0" marginheight="0"></iframe><form method="POST" A CTION="todo.php"> <input type="text" NAME="name" Value=">"> <textarea NAME="post" cols="27" rows="3"></textarea> <br> <input TYPE="submit" value="Submit"> B4:Tiếp theo là file todo.php PHP Code: <?php if ($name == '' || $post == ''") { die ("Please fill all fields. Click <a HREF=tag.php>here</a> to return."); } include('config.php'); $post = preg_replace("/</","&lt;",$post); $post = preg_replace("/>/","&gt;",$post); $post = nl2br($post); $comments = "$post"; } $c=mysql_connect("localhost","$dbuser","$dbpass"); mysql_select_db($dbname); $todo="INSERT INTO tagboard (id,name,comments,date) VALUES('','$nam e','$comments',now())"; $solution = mysql_query($todo) or die (mysql_error()); if ($solution) { ?> <meta http-equiv="refresh" content="0;url=tag.php" > <? } ?> B5:Cuối cùng là file view.php PHP Code: <?php include('config.php'); //Kết nối DB $c=mysql_connect("localhost","$dbuser","$dbpass"); //Lựa chọn DB mysql_select_db($dbname); //Lựa chọn tên DB $todo='SELECT * FROM `tagboard` order by id desc LIMIT 50'; $solution=mysql_query($todo); while ($place = mysql_fetch_array($solution)) { $id=$place["id"]; $comments=$place["comments"]; //là nơi hiển thị trên trang web ?> »<b><? echo "$place[name]"; ?></b><? echo ": $comments<br>" ; ?> <? }?> Chúc bạn thành công ! Ngày 1: Những câu lệnh cơ bản xử lí chuỗi Code: <? //Hàm kiem tra mot chuoi có phai là mot hyperlink: function checkLink($link){ * *if (!stristr($link, "http://")) return "http://".$link; * *else return $link; *} //Hàm cat bo khong trong trong chuoi: function stripSpaces($text, $replace=NULL) { * *if (is_string($replace)) return str_replace(" ", substr($replace, 0, 1), $text); * *else return str_replace(" ", "", $text); *} //Hàm kiem tra mot chuoi có phai là các ky tu alphabet hoac ky tu so tu 0-9 function alphaNum($stripper) { * *return preg_replace("/[^a-zA-Z0-9]/", "", $stripper); *} //Hàm kiem tra chuoi có chua duu ' hoac dau ": function checkUnslashed($text){ * *if (preg_match("/[\s\w]+[\"']/", $text)) * * *return TRUE; * *else * * *return FALSE; *} //Hàm cat bo dau ' hoac dau " trong chuoi: function stripQuotes($text) { * *$text = str_replace("'", "", $text); * *$text = str_replace("\"", "", $text); * *return $text; *} //Hàm thêm vào dau \ tryuoc dau ' hoac dau ": function addslashes($text) { * *if (get_magic_quotes_gpc()) return $text; * *else return addslashes($text); *} //Hàm ma hóa ky tu sang utf-8, rat hay dó các ban: function utfEncode($text){ * *$text = $this->stripSlashQuotes($text); * *$search = array("/</", "/>/"); * *$replace = array('&#x003c;', '&#x003e;'); * *return preg_replace($search, $replace, $text); *} //Hàm *kiem tra bay loi các dang chuoi nhap vào (form nhap) *nhu email,number,file, function isValidInput($userEntry, $type=NULL) { * *if (empty($userEntry) || !is_string($userEntry)) return FALSE; * *switch ($type) { * *case "chars_space": *if (eregi("^[a-z_0-9 ]+$",$userEntry)) return TRUE; *else return FALSE; *break; * *case "number": *if (ereg("^[0-9]+$",$userEntry)) return TRUE; *else return FALSE; *break; * *case "url": *if (eregi("^(http:\/\/)[_a-z0-9-]+(\.[_a-z0-9-]+|\/)", $userEntry)) return TRUE; *else return FALSE; *break; * *case "email": *if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $userEntry)) return TRUE; *else return FALSE; *break; * *case "file": *if (eregi("^[a-z_0-9\.]+$",$userEntry)) return TRUE; *else return FALSE; *break; * *default: *if (eregi("^[a-z_0-9]+$",$userEntry)) return TRUE; *else return FALSE; *break;

Ngày đăng: 03/07/2014, 09:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan