Tài liệu PHP and MySQL Discussion part 4 pptx

14 586 0
Tài liệu PHP and MySQL Discussion part 4 pptx

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Thủ thuật php Sử dụng PHP để chuyển đến 1 URL khác PHP Code: <?php header("Location: http://yoursite.com/") ?> Trong HTML bạn có thể sử dụng đoạn mã sau: Code: <html> <head> <title>Please wait .</title> <script language="JavaScript"> self.location.href='http://yoursite.com/'; </script> </head> <body> </body> </html> Hiển thị thông tin trình duyệt đang sử dụng. Bạn sử dụng đoạn PHP sau trong đoạn mà bạn muốn hiển thị: PHP Code: <? $browser = "$HTTP_USER_AGENT"; 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 ma rginwidth="0" marginheight="0"></iframe><form method="POST" ACTION="tod o.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('','$name','$co mments',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; * *} *}// END FUNC validForm() ?> Tiêu đề "Mỗi ngày một source" không đúng với topic này! Tạo 1 mini chat đơn giản. B1. Tạo 1 file HTML có nội dung như sau: PHP Code: <html> <head> <title>Mini chat &#273;ơn giản</title> </head> <body> <form method="post"><br> Login:<input type="text" name="login" size="6"><br> Message:<input type="text" name="message" size="10"><br> <input type="submit" value="Send"><br> </form> </body> </html> B2. Tạo PHP C o <?php /* CRE A LOG MES ITST ); */ // Th ê functi $lo g $me mys UES } // G ử if ( iss add } ?> Chú ý: B thử bài t Chúc b ạ Viết 1 t r B1. Bạn PHP C o <htm <hea <titl e </hea 1 file php c ode: hp ATE TABLE GIN varcha SSAGE var TIME varch êm thông đi ion addMes gin = mysq essage = my sql_query( ( "'".time(). ửi thông điệp set( $_POS dMessage( $ Bạn nên tác trên. ạn thành côn rang Uploa viết 1 tran g ode: ml> d> e>Upload< ad> có nội dung E MINICHA r(20) NOT char(255) N har(10) NO iệp vào min ssage( $log ql_escape_s ysql_escap "INSER T IN "', '".$login p ST['msg'] )) $_POST['lo ch rời đoạn ng ! d = PHP. g HTML có /title> g như sau AT ( NULL def a NOT NULL OT NULL def ni chat gin, $messag string( strip_ e_string( st NTO MINIC n."','".$mess { ogin'], $_PO tạo DB va ó nội dung n ault '', L default '', efault '' ge ) { _tags( $log trip_tags( $ CHAT ( ITS sage."' )"); OST['msg'] lưu nó thàn như sau: gin ) ); $message, '< STIME, LOG ); nh 1 file con <a><b><i GIN, MESS nfig.php, bạ i><u>') ); SAGE ) VAL ạn tham khả L ảo <body> <h1>Upload</h1> <form enctype="multipart/form-data" action="upload.php" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> File: <input name="userfile" type="file"> <input type="submit" value="Upload"> </form> </body> </html> B2. Bạn viết 1 trang upload.php: PHP Code: <?php // $userfile is where file went on webserver $userfile = $HTTP_POST_FILES['userfile']['tmp_name']; // $userfile_name is original file name $userfile_name = $HTTP_POST_FILES['userfile']['name']; // $userfile_size is size in bytes $userfile_size = $HTTP_POST_FILES['userfile']['size']; // $userfile_type is mime type e.g. image/gif $userfile_type = $HTTP_POST_FILES['userfile']['type' ]; // $userfile_error is any error encountered $userfile_error = $HTTP_POST_FILES['userfile']['error']; // userfile_error was introduced at PHP 4.2.0 // use this code with newer versions if ($userfile_error > 0) { echo 'Problem: '; switch ($userfile_error) { case 1: echo 'File exceeded upload_max_filesize'; break; case 2: echo 'File exceeded max_file_size'; break; case 3: echo 'File only partially uploaded'; break; case 4 echo brea k } exit; } // pu t $upfi l // is_ u if (is_ { if (! m { echo exit; } } els e echo exit; } echo // sho echo echo echo ?> Chú ý ở lưu vào, Chúc b ạ Hiển th ị Bạn sử d PHP Co <? $bro w 4: 'No file up l k; t the file wh le = '/uploa uploaded_f _uploaded_f move_upload 'Problem: C e { 'Problem: P 'File uploa ow what was 'Preview of $contents; '<br /><hr trên: dòng bạn có th ể ạn thành côn ị thông tin dụng đoạn P ode: wser = "$H loaded'; ere we'd li k ads/'.$userfi file and mov _file($userfi ded_file($u Could not m Possible file ded success s uploaded f uploaded f r />'; $upfile = ' / ể thay đổi nó ng ! trình duyệ t PHP sau tro HTTP_USER ke it file_name; ve_uploade ile)) serfile, $u pf move file to e upload at sfully<br /> file content /uploads/'.$ ó tùy theo ý t đang sử d ong đoạn m R_AGENT" d_file pfile)) destinatio n ttack. Filena ><br />'; ts:<br /><h $userfile_na ý mình. dụng. mà bạn muố "; n directory' ame: '.$use hr />'; ame; là thư ốn hiển thị: '; erfile_name ư mục các fi e; le upload đđược print( ?> Để đẹp h PHP Co print( Chúc b ạ Tạo cho Quote: Tạo ch o Trước h ế PHP Co <for m <in <br> <i n </form Còn đâ y PHP Co <? $we b $sear $sear $sear head e h%22 ?> ("Bạn đang hơn bạn có ode: ("<font colo ạn thành côn mình 1 mụ o mình 1 m ết bạn tạo 1 de: m action="s nput type=" nput type=" m> y là nội dung de: bsite = "www ch = $_POS ch = str_rep ch = stripsl er("Location 2&btnG=Go g sử dụng: $ thể sửa thà or=\"red\"> ng ! ục tìm kiếm mục tìm kiếm 1 form để nh search.php" text" name = submit" val g file search w.yoursite. ST['search'] place(" ","+ lashes($sear n: http://ww oogle+Sear $browser") ành: > Bạn đang m ngay trên s m ngay trê hập từ khóa method="p ="search"> lue="Search h.php: com"; ]; +",$search); rch); ww.google.c rch"); g sử dụng: $ site của bạn ên site của b a tìm kiếm: post"> > h"> <input ; com/search $browser <f n bạn (sử dụ type="rese h?q=site%3A font>") ụng google) et" value="R A$website+ Reset"> +%22$searcc [...]... xt.'$/i', $file $test)) { e, $files[] = $file; ++$i; } } } dir($handle e); closed mt_sr rand((doubl le)microtim me()*100000 00); $rand = mt_rand $i); d d(0, er('Location '.$folder n: $files[$ran nd]); heade ?> h ó ần ng chữ Để hiển thị những hình ảnh đó bạn chỉ cầ chèn dòn sau vào c ký của bạn: ode: PHP Co [img* *]http://ww ww.yoursite.com/random m .php[ */im mg] Bỏ đ ên Chú ý: B * trong đoạn mã trê Chúc bạ... bạ thành côn ! ạn ng ị Hiển thị hình ảnh Random ở chữ ký i ứa iển file m .php có nộ ội Bạn tạo 1 thư mục images chứ các hình ảnh cần hi thị và 1 f random dung như sau: ode: PHP Co < ?php hp $folde = 'image //tên th mục mà b lưu chứ hình ảnh er es/'; hư bạn ứa h g $exts = 'jpg jpeg png gif'; s $files = array(); $i = -1; if ('' = $folder) $folder = './'; == ) $hand = opend dle dir($folder) ); $exts = explode(... ! Tạo 1 đồ hồ trên site của bạ ồng ạn PHP Code: HP . <? } ?> B5:Cuối cùng là file view .php PHP Code: < ?php include('config .php& apos;); //Kết nối DB $c =mysql_ connect("localhost","$dbuser","$dbpass");. tạo dung nh PHP C o < ?php $folde $exts $file s if ('' = $hand $exts whil e for i } } close d mt_sr $rand head e ?> Để hiển PHP C o [img*

Ngày đăng: 15/12/2013, 02:15

Từ khóa liên quan

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

  • Đang cập nhật ...

Tài liệu liên quan