Nws feem ntau tshwm sim hais tias nws yog qhov tsim nyog los faib cov qhua rau qhov tsim nyog thiab tsis xav tau, thiab muab lub sijhawm pom qee cov nplooj ntawv ntawm lub xaib tsuas yog rau cov neeg uas muaj lub npe zais thiab tus password. Yuav ua li cas ua li no, piv txwv li, hauv tus neeg rau zaub mov-sab sau cov lus PHP?
Cov Lus Qhia
Kauj ruam 1
Cia peb npaj txoj hauv kev yooj yim tiv thaiv koj cov nplooj ntawv los ntawm cov neeg tuaj saib uas tsis tau tso cai. Tus tuav cov ntaub ntawv hais txog seb tus neeg qhua raug tso cai yog qhov kev sib kho. Kev sib kho yog qhov qhia txog cov ncuav qab zib hauv qhov browser, nrog qhov sib txawv tsuas yog qhov lawv tau tsim tsis yog hauv peb lub computer, tab sis nyob rau ntawm server. Thiab lawv siv rau tib lub hom phiaj xws li ncuav qab zib - khaws cov ntaub ntawv sib txawv ntawm peb thaum peb mus ntawm nplooj ntawv mus rau nplooj ntawm ib lub xaib. Thaum peb kaw qhov browser, lub server puas tsuaj rau ntu no, thiab lwm zaus peb nkag mus, nws tsim qhov tshiab. Peb siv lub tshuab haujlwm no sau cia seb tus siv puas tau nkag rau hauv qhov kev sib kho lossis tsis. Nyeem cov ntaub ntawv no, thaum tus qhua thov nplooj ntawv, php-ntawv yuav qhib rau kev nkag mus rau nplooj ntawv tiv thaiv password, lossis pub nkag mus rau lub npe username thiab password.
Kauj ruam 1: Tsim ib nplooj ntawv rau kev nkag mus rau tus ID nkag mus thiab tus password. Cov HTML code ntawm daim ntawv tso cai nyob hauv nws daim ntawv yooj yim tshaj plaws yuav zoo li no:
ID nkag mus:
Lo lus zais:
Ntawm no (thaum pib ntawm cov ntaub ntawv) peb yuav ntxiv php-code uas yuav kuaj xyuas qhov tseeb ntawm tus username thiab password nkag mus los ntawm tus qhua. Thaum pib peb yuav sau:
session_start ();
Qhov lus txib no pib lub sijhawm tshiab yog tias ib qho tsis tau tsim rau tus qhua no.
Tom qab ntawd cia peb kuaj xyuas yog tias qhov kev sib kho ntawd muaj qhov sib txawv hu ua 'userName' - nws yuav khaws lub npe yog tias tus qhua twb nkag lawm. Yog tias muaj qhov sib xws, hloov tus qhua rau nplooj ntawv tseem ceeb (index.php) thiab ua kom tiav cov ntawv php no:
yog ($ _ kho [userName ']) {
header ("Qhov chaw: index.php");
tawm;
}
Tus lej ntawm lub chaws yuav raug tua tsuas yog tias tus neeg siv tseem tsis tau nkag mus lub npe username thiab password kom raug. Cia peb qhia seb tus ID nkag mus thiab tus password yuav tsum raug xam tias yog lawm:
$ validName = 'Kuv yog kuv!';
$ validPass = 'zais password';
Tom qab ntawd peb xyuas kom pom tias lub txiaj ntsig xa los ntawm daim foos raug cov raug. Txij li peb tau teev POST cov ntaub ntawv hloov chaw nyob hauv daim ntawv, lawv yuav tsum tau nyeem ntawm $ _POST superglobal kuj sib txawv:
yog ($ _ POST ['userName'] == $ validName && $ _POST ['userPass'] == $ validPass) {
$ _SESSION ['userName'] = $ validName;
header ("Qhov chaw: index.php");
tawm;
}
Ntawm no cov cai nyob rau hauv curly kev zawm hniav {} yuav raug tua nrog cov nqi tsim nyog ntawm tus username thiab password. Ntawm kab $ _SESSION ['userName'] = $ validName; ma peb sau rau hauv qhov kev sib kho lub npe hu ua 'userName' uas muaj tus ID nkag mus ntawm tus tam sim no tso cai siv. Qhov no yuav yog tus cim uas nkag tau qhib rau nws txhua qhov chaw tsuav nws qhov kev sib kho niaj hnub no siv tau.
Thiab yog tias tsis muaj cov ntaub ntawv nkag mus rau hauv daim foos, ntxiv cov lus tsim nyog:
lwm tus ncha"
Nkag mus lossis tus password tsis raug!
;
Txhua tus lej uas yuav tsum muab khaws cia rau hauv ntaub ntawv npe hu ua login.php yuav zoo li no:
<? php
session_start ();
yog ($ _ kho [userName ']) {
header ("Qhov chaw: index.php");
tawm;
}
$ validName = 'Kuv yog kuv!';
$ validPass = 'zais password';
yog ($ _ POST ['userName'] == $ validName && $ _POST ['userPass'] == $ validPass) {
$ _SESSION ['userName'] = $ validName;
header ("Qhov chaw: index.php");
tawm;
}
lwm tus ncha"
Nkag mus lossis tus password tsis raug!
;
?>
ID nkag mus:
Lo lus zais:
Kauj ruam 2
Kauj Ruam 2: Tsim kev tso cai thaiv - ib daim ntawv txawv uas yuav txuas nrog rau txhua nplooj ntawv uas xav tau kev tiv thaiv password. Cov ntaub ntawv no tsuas yog php-code, yog li nws txuas ntxiv yuav yog "php", thiab peb yuav muab nws lub npe raws li kev coj ua rau cov ntaub ntawv zoo li no - "auth", uas yog, "auth.php". Thiab ntawm no, dhau lawm, tam sim ntawd tom qab qhib <? Php tag, yuav tsum muaj kev qhia pib lub sijhawm:
session_start ();
Peb tuaj yeem nyeem tag nrho cov zog uas tau khaws cia hauv qhov kev sib kho ntawm $ _SESSION superglobal array. Peb yuav tsum txheeb xyuas tus nqi ntawm cov "userName" nce mus nce los - yog tias tus qhua tseem tsis tau nkag mus, nws yuav tsis nyob hauv kab, thiab peb yuav xa nws mus rau nplooj ntawv rau kev nkag mus rau nws tus username thiab password:
yog (! $ _ kho [tso cai ']) {
header ("Qhov chaw: login.php");
tawm;
}
Txhua tus lej uas xav tau kev cawm dim rau auth.php file yuav zoo li no:
<? php
session_start ();
yog (! $ _ kho [admin ']) {
header ("Qhov chaw: enter.php");
tawm;
}
?>
Kauj ruam 3
Kauj Ruam 3: tom qab peb khaws cov ntaub ntawv no ntawm lub server, nws yuav nyob hauv txhua php-nplooj ntawv uas yuav tsum muaj kev tiv thaiv los ntawm cov neeg siv tsis raug cai kom txuas cov ntawv tso cai. Ntawd yog, thaum pib ntawm txhua php ntawv, koj yuav tsum sau tus lej cim no:
<? php
xav tau "auth.php";
?>
Thiab txhawm rau hloov tus nkag mus nkag lo lus zais, koj yuav tsum tau hloov pauv qhov tseem ceeb ntawm cov hloov pauv no rau hauv file.php:
$ validName = 'Kuv yog kuv!';
$ validPass = 'zais password';
$ validName - nkag mus, $ validPass - password.