闅忕潃浜掕仈缃戠殑涓嶆柇鍙戝睍锛岃秺鏉ヨ秺澶氱殑浜洪€夋嫨鎼缓鑷繁鐨勪釜浜哄崥瀹㈡潵鍒嗕韩鐢熸椿鐐规淮銆佽褰曞涔犲績寰楁垨灞曠ず涓撲笟鎶€鑳姐€傚浜庝竴涓畬鏁寸殑鍗氬绯荤粺鑰岃█锛岀敤鎴锋敞鍐屼笌鐧诲綍鍔熻兘鏄潪甯搁噸瑕佺殑缁勬垚閮ㄥ垎锛屽畠鑳藉涓虹敤鎴锋彁渚涗釜鎬у寲鐨勬祻瑙堜綋楠岋紝鍚屾椂涔熸湁鍔╀簬鍗氫富鏇村ソ鍦扮鐞嗚鑰呯兢浣撱€?/p>
涓€銆佸噯澶囧伐浣?/h2>
鍦ㄥ紑濮嬩箣鍓嶏紝璇风‘淇濅綘宸茬粡瀹夎骞堕厤缃ソ浜嗘湇鍔″櫒鐜锛堜緥濡俋AMPP锛夛紝骞朵笖鍏峰鍩烘湰鐨凥TML銆丆SS銆丣avaScript浠ュ強PHP缂栫▼鐭ヨ瘑銆傝繕闇€瑕佸垱寤轰竴涓狹ySQL鏁版嵁搴撶敤鏉ュ瓨鍌ㄧ敤鎴蜂俊鎭瓑鏁版嵁銆備负浜嗙畝鍖栨搷浣滐紝杩欓噷鎴戜滑浣跨敤phpMyAdmin鍥惧舰鐣岄潰宸ュ叿鏉ヨ繘琛屾暟鎹簱鐨勭浉鍏宠缃€?/p>
浜屻€佸垱寤虹敤鎴疯〃
鎵撳紑phpMyAdmin鍚庯紝閫夋嫨瑕佷娇鐢ㄧ殑鏁版嵁搴擄紝鐒跺悗鐐瑰嚮鈥淪QL鈥濇爣绛鹃〉锛屽湪鏂囨湰妗嗗唴杈撳叆濡備笅SQL璇彞浠ュ垱寤哄悕涓簎sers鐨勬柊琛ㄦ牸锛?/p>
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
涓婅堪浠g爜灏嗗缓绔嬩竴涓寘鍚玦d锛堣嚜澧炰富閿級銆佺敤鎴峰悕(username)銆佸瘑鐮?password)鍙婄數瀛愰偖浠?email)瀛楁鐨勬暟鎹〃缁撴瀯銆傚叾涓紝瀵嗙爜搴旈噰鐢ㄥ姞瀵嗗舰寮忎繚瀛橈紝閬垮厤鐩存帴鏄庢枃瀛樺偍銆?/p>
涓夈€佺紪鍐欐敞鍐岄〉闈?/h2>
鎺ヤ笅鏉ワ紝鎴戜滑灏嗗垱寤轰竴涓畝鍗曠殑HTML+PHP娣峰悎鏂囦欢浣滀负鐢ㄦ埛娉ㄥ唽鍏ュ彛銆傝鏂囦欢閫氬父鍛藉悕涓簉egister.php锛屽苟鏀剧疆浜庣綉绔欐牴鐩綍涓嬶細
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 鑾峰彇鎻愪氦鐨勬暟鎹?
$username = $_POST['username'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$email = $_POST['email'];
// 杩炴帴鏁版嵁搴?
$conn = new mysqli("localhost", "root", "", "your_database_name");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// 妫€鏌ョ敤鎴峰悕鏄惁宸插瓨鍦?
$stmt = $conn->prepare("SELECT id FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
echo "鐢ㄦ埛鍚嶅凡瀛樺湪锛岃灏濊瘯鍏朵粬鐢ㄦ埛鍚嶃€?;
} else {
// 鎻掑叆鏂扮敤鎴?
$stmt = $conn->prepare("INSERT INTO users (username, password, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $username, $password, $email);
if ($stmt->execute()) {
echo "娉ㄥ唽鎴愬姛锛佽鍓嶅線鐧诲綍椤甸潰杩涜鐧诲綍銆?;
} else {
echo "娉ㄥ唽澶辫触锛? . $stmt->error;
}
}
$stmt->close();
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>鐢ㄦ埛娉ㄥ唽</title>
</head>
<body>
<form action="" method="post">
鐢ㄦ埛鍚? <input type="text" name="username" required><br>
瀵嗙爜: <input type="password" name="password" required><br>
鐢靛瓙閭欢: <input type="email" name="email" required><br>
<input type="submit" value="娉ㄥ唽">
</form>
</body>
</html>
涓婅堪浠g爜棣栧厛妫€鏌ヨ姹傛柟娉曟槸鍚︿负POST锛屽鏋滄槸锛屽垯浠?_POST鍏ㄥ眬鏁扮粍涓幏鍙栫敤鎴疯緭鍏ョ殑淇℃伅锛屽苟閫氳繃password_hash()鍑芥暟瀵瑰瘑鐮佽繘琛屽姞瀵嗗鐞嗭紱鎺ョ潃鍒╃敤mysqli绫昏繛鎺ュ埌鎸囧畾鐨凪ySQL鏁版嵁搴擄紝骞舵墽琛屼竴绯诲垪鏌ヨ鎿嶄綔鏉ラ獙璇佹彁渚涚殑鐢ㄦ埛鍚嶆槸鍚﹀凡琚崰鐢ㄣ€傝嫢涓嶅瓨鍦ㄩ噸澶嶆儏鍐碉紝鍒欐彃鍏ヤ竴鏉℃柊鐨勮褰曞埌users琛ㄤ腑锛屾渶鍚庤繑鍥炵浉搴旂殑鎻愮ず淇℃伅缁欏墠绔〉闈€傚綋涓嶆槸POST璇锋眰鏃讹紝鍒欐甯告樉绀烘敞鍐岃〃鍗曘€?/p>
鍥涖€佺紪鍐欑櫥褰曢〉闈?/h2>
绫讳技鍦帮紝鎴戜滑闇€瑕佸啀鍒涘缓涓€涓猯ogin.php鏂囦欢鐢ㄤ簬澶勭悊鐢ㄦ埛鐨勭櫥褰曢€昏緫锛?/p>
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 鑾峰彇鎻愪氦鐨勬暟鎹?
$username = $_POST['username'];
$password = $_POST['password'];
// 杩炴帴鏁版嵁搴?
$conn = new mysqli("localhost", "root", "", "your_database_name");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// 鏌ヨ鐢ㄦ埛
$stmt = $conn->prepare("SELECT FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
if ($user && password_verify($password, $user['password'])) {
// 鐧诲綍鎴愬姛
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $user['username'];
header("Location: dashboard.php"); // 璺宠浆鍒扮敤鎴蜂富椤垫垨鍏朵粬椤甸潰
} else {
// 鐧诲綍澶辫触
echo "鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒銆?;
}
$stmt->close();
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>鐢ㄦ埛鐧诲綍</title>
</head>
<body>
<form action="" method="post">
鐢ㄦ埛鍚? <input type="text" name="username" required><br>
瀵嗙爜: <input type="password" name="password" required><br>
<input type="submit" value="鐧诲綍">
</form>
</body>
</html>
杩欐浠g爜鍚屾牱閬靛惊浜嗗厛鍒ゆ柇璇锋眰绫诲瀷鐨勮鍒欙紝涓嶅悓涔嬪鍦ㄤ簬瀹冧細灏濊瘯鏍规嵁鐢ㄦ埛鎻愪緵鐨勫嚟鎹幓鏌ユ壘瀵瑰簲鐨勮处鎴疯褰曪紝骞惰皟鐢╬assword_verify()鍑芥暟瀵规瘮瀹為檯瀛樺偍鐨勬暎鍒楀€间笌褰撳墠杈撳叆涔嬮棿鐨勫尮閰嶅害銆傚鏋滀袱鑰呭惢鍚堬紝鍒欒涓鸿璇佽繃绋嬮『鍒╁畬鎴愶紝姝ゆ椂鍙互鑰冭檻灏嗘煇浜涙爣璇嗙瀛樺叆$_SESSION瓒呭叏灞€鍙橀噺浠ヤ究鍚庣画浣跨敤锛屼笌姝ゅ悓鏃堕噸瀹氬悜鑷崇洰鏍囧湴鍧€锛涘弽涔嬪垯杈撳嚭閿欒娑堟伅鎻愰啋鐢ㄦ埛閲嶆柊杈撳叆姝g‘淇℃伅銆?/p>
浜斻€佹€荤粨
閫氳繃浠ヤ笂姝ラ锛屾垜浠凡缁忔垚鍔熷疄鐜颁簡鍩轰簬PHP璇█寮€鍙戠殑鍗氬绯荤粺鐨勭敤鎴锋敞鍐屼笌鐧诲綍鍔熻兘銆傚湪瀹為檯椤圭洰褰撲腑鍙兘杩樹細娑夊強鍒版洿澶氬鏉傜殑涓氬姟鍦烘櫙鍜屾妧鏈粏鑺傦紝姣斿楠岃瘉鐮佹牎楠屻€佺涓夋柟骞冲彴鎺堟潈缁戝畾绛夛紝浣嗘棤璁哄浣曪紝鎺屾彙濂藉熀纭€姒傚康濮嬬粓鏄В鍐抽棶棰樼殑鍏抽敭鎵€鍦ㄣ€傚笇鏈涜繖绡囨枃绔犺兘澶熷府鍔╁ぇ瀹跺缓绔嬭捣瀵硅棰嗗煙鐨勫垵姝ヨ璇嗭紝骞舵縺鍙戣繘涓€姝ユ帰绱㈢殑鍏磋叮銆?/p>
本文由阿里云优惠网发布。发布者:编辑员。禁止采集与转载行为,违者必究。出处:https://aliyunyh.com/145423.html
其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。