Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ПрактикаWEB_2

.pdf
Скачиваний:
8
Добавлен:
09.02.2016
Размер:
356.18 Кб
Скачать

 

 

 

-

c

c

 

 

 

20 (

)

 

)

 

(

 

 

1.

 

 

 

 

 

2.

 

 

 

 

 

3.

 

 

 

 

 

4.

 

 

 

 

 

5.

 

 

 

 

 

:

 

 

 

 

 

1.

- pGAG

HgWdk

q'

',++ ' . (

2.

- p

 

q'

4

', + ',22 (

3.

'

 

web -

 

GAG Dkc ' - -

' ,

(

 

 

 

 

abW$

'

%-

-

 

'

(

,4

-

 

$

$f%

$T%5

 

 

 

-

 

:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

R

 

 

 

'

 

 

 

 

 

 

(

 

'

 

 

 

 

R+

 

 

 

 

'

 

 

 

 

 

(

 

'

 

 

 

 

 

 

 

 

 

 

 

 

 

W

 

 

 

 

'

 

 

 

 

 

 

 

(

'

 

 

 

 

 

 

 

 

 

 

 

 

W+

 

 

 

 

 

'

 

 

 

 

 

 

(

'

 

 

 

A

 

 

 

'

 

(

 

 

 

 

 

'

 

 

 

 

 

A+

 

 

 

 

'

 

 

 

 

 

(

 

'

 

 

 

aU]$

'

%-

 

 

(

 

 

 

4

 

 

 

 

 

2 -

 

5

 

 

 

 

 

3 -

 

5

 

 

 

 

 

Wfe$

%-

 

(

 

 

 

fdgUSfW$

' %-

 

 

$

%(

 

IL>$

 

 

%

?:C >(

 

 

 

bgfe$

' %-

 

(

 

 

 

UaeW$

%-

(

 

 

 

 

V[W$

%-

 

'

 

'

 

(

 

 

 

 

 

 

 

 

 

 

 

4

 

 

 

 

fopen() or die() -

 

 

 

 

?:C >'

 

 

(

 

 

 

 

 

 

 

 

 

+

 

4

 

 

 

 

 

4

A DC

GAG-

(

4

 

 

 

 

 

A DC-

 

(

 

<FORM ENCTYPE="multipart/form - data" ACTION="action.php" METHOD=POST>

<INPUT NAME="myfile" TYPE="file">

 

 

 

 

<INPUT TYPE="submit" value="

">

 

 

</FORM>

 

 

 

 

 

html -

 

browse,

 

.

 

'

-

(

 

 

 

 

 

 

 

 

SUf[a(bb(

 

 

 

4

 

 

<?php

 

 

 

 

 

if(isset($_FILES["myfile"])) //

 

 

 

 

{

 

 

 

 

 

$catalog = "./image/"; //

 

 

 

 

 

if (is_dir($catalog)) //

 

 

 

 

 

{

 

 

 

 

 

$myfile = $_FILES["myfile"]["tmp_name"]; //

 

$myfile_name = $_FILES["myfile"]["name"]; // x'

 

if(!copy($myfile, $catalog))

echo '

 

 

'.$myfile_name //

}

 

 

 

 

 

WeW_]V[d$()[_SW)%5)

 

'

 

 

}

 

 

 

 

 

?>

 

 

 

 

 

 

 

-

'

(

 

 

 

 

 

(

 

 

 

 

(

 

 

 

 

 

(

 

 

 

UWU]Taj(

 

'

 

 

UWU]TajS

'

RGF

4

 

 

 

 

<FORM ACTION="file.php" METHOD=POST>

 

<input name="mycolor" type="checkbox" value="blue">

 

<input name="mycolor" type="checkbox" value="black">

 

<input name="mycolor" type="checkbox" value="white">

 

<input name="Submit" type=submit value="

">

 

 

</FORM>

 

 

 

 

 

<?php

 

 

 

 

 

if (!empty($_POST['mycolor'])) echo $_POST['mycolor']; //

1

WeWWUa

5

 

 

 

 

?>

 

 

 

 

 

 

 

,

 

-

 

 

 

 

Uagter.txt.

1.

-

 

UagfWd(bb

 

<?php

 

 

$f=fopen("counter.txt", "a+t") or die("

");

flock(

$f, 2);

 

$s = fgets($f);

 

$s+=1;

// $s=$s+1;

 

ftruncate ($f, 0);

 

fputs ($f, $s);

 

flock ($f, 3);

 

fclose($f);

 

echo $s;

 

?>

 

 

2.

forma.html

+1

3.

 

4

<?php

 

 

echo "

- "; require_once("counter.php");

 

?>

 

 

 

 

Require_once(

'

) -

, '

.

 

 

PHP

HTML.

 

4.

 

 

: forma.php

 

+(

 

 

9

 

,(

 

 

9

 

(

 

(

 

 

4(

(

 

 

 

/(

 

(

 

 

 

 

I

6.1 - 4 4

- PHP

WEB

 

 

 

 

 

 

 

 

21 ( c

)

 

N>;

(

 

 

 

 

 

N>;

 

 

1.

 

 

 

 

 

2.

 

 

 

 

 

3.

 

 

 

 

 

4.

 

 

 

 

 

5.

 

 

 

 

 

 

:

 

 

 

 

1.

- pGAG

HgWdk

q'

',++ ' . (

2.

- p

 

q'

4

', + ',22 (

3.

'

 

web -

 

GAG Dkc ' - -

' , (

 

 

 

 

 

 

 

 

4

 

GAG

 

 

'

 

'

 

 

 

(

GAG

 

 

 

 

 

(

 

 

 

'

 

(

 

(

 

- LeWd$

 

 

 

 

eShW$%'

'- LeWdaae$

'

 

'

a[$%'

aagf$% (%'

'-

$

 

 

-

'

'

 

'

%(

 

 

 

'

 

 

 

aTS([U(bb(

 

 

 

 

 

'

(

'

 

 

 

 

(x

-

 

4

[VWj(bb'dW[efWd(bb'a[(bb'aagf(bb'eWf[ e(bb iWUa_W(bb( 4

 

 

geWde

 

DkHC(

 

(

geWde'

(

SQL : CREATE TABLE IF NOT EXISTS 'users' ( 'id' int(11) NOT NULL AUTO_INCREMENT, 'username' varchar(50) NOT NULL,

'password' varchar(50) NOT NULL,

 

 

 

'email' varchar(50) NOT NULL,

 

 

 

'join_date' datetime NOT NULL,

 

 

 

PRIMARY KEY ('id'),

 

 

 

UNIQUE KEY 'username' ('username')

 

 

 

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

 

 

 

[V'

 

 

 

(

 

 

 

 

geWdS_W(x

"password", "email",

"join_date".

 

=;(USee(bb

 

 

 

 

4

 

HC

'

 

 

(

 

 

 

4

 

 

 

class DB {

 

 

 

 

protected $db_name = 'yourdatabasename';

 

 

 

protected $db_user = 'databaseusername';

 

 

 

protected $db_pass = 'databasepassword';

 

 

 

protected $db_host = 'localhost';

 

 

 

)

(

'

 

 

)

 

 

 

 

public function connect(){

 

 

 

$connection = mysql_connect($this ->db_host, $this ->db_user, $this ->db_pass);

 

mysql_select_db($this ->db_name);

 

 

 

return true;

 

 

 

 

}

 

 

 

 

)

_kec

'

 

 

)

(

e[ WIaitrue,

 

 

)

 

 

 

 

public function processRowSet($rowSet, $singleRow=false)

 

 

{

 

 

 

 

$resultArray = array();

 

 

 

while($row = mysql_fetch_assoc($rowSet))

 

 

 

{

 

 

 

 

array_push($resultArray, $row);

 

 

 

}

 

 

 

 

if($singleRow === true)

 

 

 

return $resultArray[0];

 

 

 

return $resultArray;

 

 

 

}

 

 

 

 

//

 

 

 

 

)

fSTW

 

i WdW

 

public function select($table, $where){

$sql = "SELECT * FROM $table WHERE $where"; $result = mysql_query($sql); if(mysql_num_rows($result) == 1)

return $this ->processRowSet($result, true); return $this ->processRowSet($result);

}

//

public function update($data, $table, $where){ foreach ($data as $column => $value){

$sql = "UPDATE $table SET $column = $value WHERE $where";

 

 

mysql_query($sql) or die(mysql_error());

 

 

 

 

}

 

 

 

 

 

 

 

return true;

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

)

 

 

 

 

 

 

 

public function insert($data, $table){

 

 

 

 

$columns = "";

 

 

 

 

 

 

$values = "";

 

 

 

 

 

 

foreach ($data as $column => $value){

 

 

 

 

$columns .= ($columns == "")? "" : ";

 

 

 

 

$columns .= $column;

 

 

 

 

 

$values .= ($values == "")? "" : ";

 

 

 

 

$values .= $value;

 

 

 

 

 

 

}

 

 

 

 

 

 

 

$sql = "insert into $table ($columns) values ($values)";

 

 

 

mysql_query($sql) or die(mysql_error());

 

 

 

 

)

B=

 

(

 

 

 

 

return mysql_insert_id();

 

 

 

 

 

}}?>

 

 

 

 

 

 

 

 

 

 

4

: $db_name, $db_user, $db_pass,

$db_host.

 

 

 

 

( VTRaef

aUS aef(

 

 

"protected" -

'

 

 

(

 

 

 

 

 

f[e->db_name, $this -8VTRgeWd'

 

 

 

 

 

 

Ua WUf$%(

 

 

 

(

 

 

 

 

-

 

$

 

%(

 

 

 

 

 

 

 

 

 

 

 

4

 

 

 

$db = new DB();

 

 

 

 

 

 

$db ->connect();

 

 

 

 

 

 

 

 

 

processRowSet().

-

'

mysql

 

 

 

 

,

 

.

 

 

 

_kecRWfURSeeaU$%

(

 

 

 

 

 

(

 

 

 

 

x

 

e[ WIai'

 

 

(

fdgW'

 

 

 

 

(

'

 

 

$

'

 

 

 

[V%(

 

 

 

 

 

 

DkHC4eWWUf'[eWdf'gbVSfW(

 

 

 

HC

'

 

-

 

(

 

 

HC

 

 

 

(

eWWUf$%'

 

 

 

(

gbVSfW$%'

fdgW

 

(

[eWdf$%'

 

[V

(

 

 

 

 

 

 

 

 

 

 

gbVSfW$%4

 

$db = new DB();

 

 

 

 

 

 

$data = array(

 

 

 

 

 

 

"username" => "'johndoe'

 

 

 

 

 

"email" => "'johndoe@email.com'"

 

 

 

 

);

 

 

 

 

 

 

 

)

[V7

 

 

 

 

 

//username - johndoe

e - mail - johndoe@email.com

 

 

 

$db ->update($data, 'users ', 'id = 3');

 

 

(

 

(

 

 

(USee

([U(

 

(fSUUWee

(

$LeWd(USee(bb%

 

 

(

 

(

'

 

(

 

-

'

 

(

 

 

 

(

 

4

'

geWde

(

 

 

require_once 'DB.class.php'; class User {

public $id;

public $username; public $hashedPassword; public $email;

public $joinDate;

)

//Takes an associative array with the DB row as an argument. function __construct($data){

$this ->id = (isset($data['id']))? $data['id'] : "";

$this ->username = (isset($data['username']))? $data['username'] : "";

$this ->hashedPassword = (isset($data['password']))? $data['password'] : ""; $this ->email = (isset($data['email']))? $data['email'] : "";

$this ->joinDate = (isset($data['join_date']))? $data['join_date'] : "";

}

public function save($isNewUser = false){ //create a new database object.

$db = new DB();

//if the user is already registered and we're //just updating their info. if(!$isNewUser){

//set the data array $data = array(

"username" => "'$this ->username' "password" => "'$this ->hashedPassword' "email" => "'$this ->email'"

);

//update the row in the database

$db ->update($data, 'users ', 'id = '.$this ->id); }else {

//if the user is being registered for the first time. $data = array(

"username" => "'$this ->username' "password" => "'$this ->hashedPassword'

"email" => "'$this ->email'

"join_date" => "'".date("Y - m - d H: i: s", time())".'" );

$this ->id = $db ->insert($data, 'users'); $this ->joinDate = time();

}

return true; }}?>

 

'

'

 

$

LeWd

'

 

%(

 

 

 

 

bdafWUfWV $

+-

%

bgT[U(

'

-

 

 

LeWd(

 

'

 

 

(

 

 

f[e-8hSd[STWS_W(

 

'

 

 

(

'

 

 

(x -

 

(

 

 

[4

 

$value = (3 == 4)? "A": "B";

 

 

 

 

 

 

 

 

- $value = "A",

- $value =

"B".

 

hSgW7 ;(

 

 

 

x

 

 

 

 

 

LeWd(

 

'

 

(

 

'

 

VSfS(

 

 

'

[eEWiLeWd

 

fdgW$

SeW%(

[eEWiLeWd7 fdgW'

 

[eWdf$% =;(x

 

gbVSfW$%(

 

geWd

(

 

 

 

 

LeWdaae(USee(bb

 

 

 

 

 

 

'

 

4 a[$%' aagf$%'

UWU]LeWdS_W>j[efe$% Wf$%(z

 

'

 

(

 

 

 

 

 

//UserTools.class.php require_once 'User.class.php'; require_once 'DB.class.php'; class UserTools {

//Log the user in. First checks to see if the

//username and password match a row in the database. //If it is successful, set the session variables

//and store the user object within.

public function login($username, $password)

{

$hashedPassword = md5($password);

$result = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$hashedPassword'");

if(mysql_num_rows($result) == 1)

{

$_SESSION["user"] = serialize(new User(mysql_fetch_assoc($result))); $_SESSION["login_time"] = time();

$_SESSION["logged_in"] = 1; return true;

}else{ return false;

}}

//Log the user out. Destroy the session variables. public function logout() { unset($_SESSION['user']); unset($_SESSION['login_time']); unset($_SESSION['logged_in']); session_destroy();

}

//Check to see if a username exists.

//This is called during registration to make sure all user names are unique. public function checkUsernameExists($username) {

$result = mysql_query("select id from users where username='$username'"); if(mysql_num_rows($result) == 0)

{

return false; }else{ return true;

}}

//get a user

//returns a User object. Takes the users id as an input public function get($id)

{

$db = new DB();

$result = $db->select('users', "id = $id"); return new User($result);

}}

?>

a[$%

 

 

 

 

a[$%

 

(

geWdS_W bSeeiadV

(

 

'

LeWd

(

'

 

 

GAGeWd[S[lW$%(

 

'

 

 

geWd[S[lW$%(

(

(

 

 

 

 

 

'

 

R> BFEPa WVR[Q

+(

 

 

 

(

(

aagf$%

 

 

 

 

 

(

GAGgeWf$%

'

eWee[aRVWefdak$%

(

 

 

 

 

UWU]LeWdS_W>j[efe$%

 

 

 

 

'

 

(

 

Wf$%

 

 

 

 

 

[V

 

 

=;'

eWWUf$%(

 

 

 

 

LeWd'

 

(

 

 

 

9

'

'

 

 

'

 

 

(

4$

fb4)iii(iWTe[fW(Ua_)bda[W(bb9geWdB=7%

 

//note: you will have to open up a database connection first. //see Part 1 for further information on doing so.

//You'll also have to make sure that you've included the class files. $tools = new UserTools();

$user = $tools->get($_REQUEST['userID']); echo "Username: ".$user->username."";

echo "Joined On: ".$user->joinDate."";

4 aTS([U(bb

aTS([U(bb

 

( 9

'

(

'

(

 

 

require_once 'classes/UserTools.class.php'; require_once 'classes/DB.class.php'; //connect to the database

$db = new DB(); $db->connect();

//initialize UserTools object $userTools = new UserTools(); //start the session session_start();

//refresh session variables if logged in if(isset($_SESSION['logged_in'])) { $user = unserialize($_SESSION['user']);

$_SESSION['user'] = serialize($userTools->get($user->id));

}

?>

 

(

'

 

 

'

eWee[aRefSdf$%(

 

(

 

 

\

'

 

(

 

 

(

-

 

 

(

'

 

(z

 

 

 

$dW[efWd(bb%

 

 

(

GAG

 

GAG

aTS([U(bb(

 

 

 

 

(

 

checkUsernameExists($username))

 

 

{

 

 

 

$error .= "That username is already taken.<br/> \n\r";

 

$success = false;

 

 

 

}

 

 

 

//

 

 

 

if($password != $password_confirm){

 

 

$error .= "Passwords do not match.<br/> \n\r";

 

$success = false;

 

 

 

}

 

 

 

if($success)

 

 

 

{

 

 

 

//

 

'

 

eWee[aRefSdf$%(

(

'

 

'

R> BFEPgeWdQ'

 

'

 

(

'A DC-

(

( A DC

4