我从 www.phpacademy.org 上发布的名为“Register & Login”的视频教程中复制了一个登录脚本
我的登录页面包含以下代码...
<?php
if (empty($_POST) === false ) {
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) === true || empty($password) === true) {
$error = '<div id="error" style="font-size:16px; font-family: \'Armata\';">Error: You need to enter <u>Username</u> and <u>Password</u>!</div>';
}
else if (user_exists($username) === false){
$error = '<div id="error" style="font-size:16px; font-family: \'Armata\';">Error: We are not able to get username <u>'.$username.'</u>. Please check your entered details.</div>';
}
else if (user_active($username) === false){
$error = '<div id="error" style="font-size:16px; font-family: \'Armata\';">Error: You need to activate your account by confirming your email address.<br> Check inbox and span folder for confirmation email.</div>';
}
else if (strlen($password) > 32 || (strlen($password) < 6 ) ) {
$error = '<div id="error" style="font-size:16px; font-family: \'Armata\';">Error: Your password must be between 6 and 32 characters.</div>';
}
else {
$login = login($username, $password);
if ($login === false) {
$error = '<div id="error" style="font-size:16px; font-family: \'Armata\';">Error: Your Username or Password is incorrect. Please enter correct details.</div>';
} else {
$_SESSION['uid'] = $login;
header ('Location: /home');
exit ();
}
}
}
?>
然后登录表单
我的登录表单功能页面的代码是
<?php
function sanitize($data) {
return mysql_real_escape_string($data);
}
function user_data($uid){
$data = array();
$uid = (int)$uid;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if ($func_num_args > 0) {
unset ($func_get_args[0]);
$fields = '`' . implode ('`,`',$func_get_args) . '`';
$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `users` WHERE `uid` = $uid"));
return $data;
}
}
function user_logged_in() {
return (isset($_SESSION['uid'])) ? true : false ;
}
function user_exists($username){
$username = sanitize($username);
return (mysql_result(mysql_query("SELECT `uid` FROM `users` WHERE `uname` = '$username'"), 0) == 1) ? true : false;
}
function user_active($username){
$username = sanitize($username);
return (mysql_result(mysql_query("SELECT `uid` FROM `users` WHERE `uname` = '$username' AND `active` = 1"), 0) == 1) ? true : false;
}
function uid_from_uname($username){
$username = sanitize($username);
return mysql_result(mysql_query("SELECT `uid` FROM `users` WHERE `uname` = '$username' "), 0, 'uid');
}
function login($username, $password){
$uid = uid_from_uname($username);
$username = sanitize($username);
$password = md5($password);
return (mysql_result(mysql_query("SELECT `uid` FROM `users` WHERE `uname` = '$username' AND `password` = '$password'"), 0) == 1) ? $uid : false ;
}
?>
在那个视频中,他们在这样的查询中使用了“SELECT COUNT ( user_id
)....”,这在我的系统中不起作用。我正在使用 xampp 1.7.7
问题是在我的数据库表的“id=1”、“username=viral.joshi”和“pass=password”的第一行
它允许我输入并且不显示任何错误,第二行的数据是“id=2”、“username=viral4ever”和“pass=password”,当我输入这个 id pass 时,它显示错误找不到用户名永远病毒。
我编辑了第一行的用户名并将其更改为viral4ever,这样我就可以访问了。所以请帮助我访问每一行。我的 db 表的详细信息是“InnoDB”“latin1_swedish_ci”,列详细信息用户名的排序规则是“utf8_bin”,其他的是“utf8_unicode_ci”,两者都是 varchar 可接受的,uid 是 int。请任何人帮助我。