1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu PHP & MySQL Discussion part 4 docx

14 519 0
Tài liệu đã được kiểm tra trùng lặp

Đ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ông tin cơ bản

Tiêu đề Thủ thuật PHP sử dụng PHP để chuyển đến 1 URL khác
Thể loại Hướng dẫn
Định dạng
Số trang 14
Dung lượng 280,23 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Thủ thuật php Sử dụng PHP để chuyển đến 1 URL khác PHP Code: Trong HTML bạn có thể sử dụng đoạn mã sau: Code: Please wait.... Bạn sử dụng đoạn PHP sau trong đoạn mà bạn muốn hiển th

Trang 1

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

Trang 2

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">

Trang 3

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))

{

Trang 4

$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;

*}

Trang 5

//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;

Trang 6

* *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>

Trang 7

B2 Tạo

PHP Co

<?php

/*

CREA

LOG

MES

ITST

);

*/

// Thê

functi

$log

$me

mys

UES

}

// Gử

if ( iss

add

}

?>

Chú ý: B

thử bài t

Chúc bạ

Viết 1 tr

B1 Bạn

PHP Co

<htm

<hea

<title

</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 trang

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

"INSERT IN

"', '".$login

p ST['msg'] ))

$_POST['lo

ch rời đoạn

ng !

d = PHP

g HTML có

/title>

g như sau

AT ( NULL defa 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

Trang 8

<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;

Trang 9

case 4

echo

break

}

exit;

}

// put

$upfil

// is_u

if (is_

{

if (!m

{

echo

exit;

}

} else

echo

exit;

}

echo

// sho

echo

echo

echo

?>

Chú ý ở

lưu vào,

Chúc bạ

Hiển thị

Bạn sử d

PHP Co

<?

$brow

4:

'No file upl

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 lik 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, $upf 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)) destination

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

Trang 10

?>

Để đẹp h

PHP Co

print(

Chúc bạ

Tạo cho

Quote:

Tạo cho

Trước hế

PHP Co

<form

<in

<br>

<in

</form

Còn đây

PHP Co

<?

$web

$sear

$sear

$sear

heade

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

Trang 11

Chú ý th

Chúc bạ

Hiển thị

Bạn tạo

dung nh

PHP Co

<?php

$folde

$exts

$files

if ('' =

$hand

$exts

while

for

i

}

}

closed

mt_sr

$rand

heade

?>

Để hiển

PHP Co

[img*

hay đổi www

ạn thành côn

ị hình ảnh

1 thư mục i

ư sau:

ode:

hp

er = 'image

= 'jpg jpeg

s = array();

== $folder)

dle = opend

= explode(

e (false !==

reach($exts

if (preg_ma

$files[] =

++$i;

}

}

dir($handle

rand((doubl

d = mt_rand

er('Location

thị những h

ode:

*]http://ww

w.yoursite

ng !

Random ở images chứ

es/'; //tên th

g png gif';

$i = -1;

) $folder = dir($folder) (' ', $exts);

($file = re

as $ext) { atch('/\.'.$ex

= $file;

e);

le)microtim d(0 $i);

n: '.$folder.

hình ảnh đó

ww.yoursite.

com thành

ở chữ ký

ứa các hình

hư mục mà b

'./';

);

eaddir($han xt.'$/i', $file

me()*100000

.$files[$ran

ó bạn chỉ cầ

com/random

địa chỉ web

ảnh cần hi

bạn lưu chứ

ndle))) {

e, $test)) {

00);

nd]);

ần chèn dòn

m.php[*/im

bsite của bạ

iển thị và 1 f

ứa hình ảnh

ng sau vào c

mg]

ạn

file random

h

chữ ký của

m.php có nộ

bạn:

ội

Trang 12

Chú ý: B

Chúc bạ

Tạo cho

Bắt đầu

PHP Co

<?php

print(

<form

<inpu

<inpu

<inpu

<text

<inpu

bmit()

<inpu

</for

");

// Đây

?>

Viết 1 fil

PHP Co

<?php

$_GE

'';

if ($_

if (em

) || em

// Kiể

{

echo

} else

extrac

$ip =

$ema

// Tha

$mail

$mail

Bỏ * trong đ

ạn thành côn

o mình 1 for

với đoạn m

ode:

hp

("

m action=\"

ut type=\"te

ut type=\"te

ut type=\"te

tarea name=

ut type=\"su

()\"/>&nbsp

ut type=\"re

rm>

y là phần h

le form.php

ode:

hp

ET['action']

_GET['actio

mpty($_POS

mpty($_POS

ểm tra các t

'<p>Xin vu

e {

ct($_POST)

$_SERVER

ail = "You@

ay đổi thành

lmsg = "Na

lmsg = "Em

đoạn mã trê

ng !

rm mail liê

mã sau:

"form.php?a ext\" name=

ext\" name=

ext\" name=

=\"text\">M ubmit\" valu p;

eset\" value iển thị để k

p có nội dun

] = (isset($_

on'] == 'sen ST['name']) ST['text'])) thông tin tro

ui lòng điền

T);

R['REMOT

@domain.co

h email của ame: $name mail: $send

ên

ên hệ trên w

action=sen

=\"name\" v

=\"sender\"

=\"subject\"

Message</t ue=\"Send!

e=\"Reset\"

khách hàng

ng như sau:

_GET['actio

nd') { || empty($_

ong field

n đầy đủ thô

TE_ADDR']

om";

a bạn e\n";

der\n";

web

d\" method value=\"Na

" value=\"yo

" value=\"S textarea><b

!\" onClick=

">

điền thông

on'])) ? $_G _POST['sen

ông tin!</p>

;

d=\"post\">

ame\"><br>

ou@domain Subject\"><

br>

=\"this.disa

tin

GET['action

nder']) || em

>';

>

n.com\"><b

<br>

abled=true;

n'] : mpty($_POS

br>

; this.form.s

ST['subject

su

t']

Trang 13

$mail

$mail

if (ma

{

print(

}

}

}

?>

Chúc bạ

Tạo 1 đồ

PHP Co

<?PH

$time

$adj =

$time

echo

?>

Chúc bạ

Lưu lại

Bạn tạo

Sau đó b

PHP Co

<?php

$Date

$user

$host

$user

$file =

$fp =

fputs

uyệt:

fclose

?>

lmsg = "Su

lmsg = "IP

lmsg = "M

ail($email,

("<p>Thôn

ạn thành côn

ồng hồ trên

de:

HP

e_offset ="0

= ($time_of

e = date(" h

"$time";

ạn thành côn

thông tin U

1 file logge

bạn viết 1 fi

de:

p

e = date("F

r_ip = $REM

t = gethostb

r_browser =

= "admin/lo

= fopen($file

($fp, "<br>

$user_brow

e($fp);

ubject: $sub P: $ip\n\n";

Message: $te

$subject, $m

ng tin của bạ

ng !

site của bạ

0";

ffset * 120) :i:s",time()

ng !

User vào w

er.txt và cho ile PHP có

jS Y, h:iA"

MOTE_AD byaddr($use

= $HTTP_U ogger.txt";

e, "a+");

>Date: $Dat

wser<br> -bject\n";

ext";

mailmsg, "F

ạn đã được

ạn

);

+ $adj);

website

o nó vào thư nội dung nh

");

DDR;

er_ip);

USER_AGE

te <br> IP:

-From: $sen

c chuyển đi!

ư mục adm

hư sau:

ENT;

$user_ip <

-nder\n"));

!</p>");

min

<br> Host(IS -<br>SP) $host <"); <br> Trình dd

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

TỪ KHÓA LIÊN QUAN