我有两个名为login.php
和configDb.php
的文件。我在sql_query($query)
中有一个名为configDb.php
的函数,就像这样:
function sql_query($query) {
$link=mysqli_connect("localhost", "root", "", "basheer");
if (mysqli_connect_error()) {
echo mysqli_connect_errno();
}
$q= mysqli_query($link, $query);
while ($row=mysqli_fetch_array($q))
{
return $row['username']." ".$row['password'];
}
}
我在l ogin.php
中包含了文件,我可以在数组变量中获取结果:
$username = $_POST['user'];//
$password = $_POST['pass'];
$query="select * from tbladmin where username='Admin'";//Sql query
require 'include/config_db.php';
$user= sql_query($query);//passed the function result to an array var
print_r($user);//prints the result correctly
echo '<br/>';
if ($username == $user['username'] && $password == $user['password'])
//I am stuck here with $user['username] and $user['password']
{
header("location: index.php");
} else {
$message = "Invalid user name or password, please try again!";
}
如何将$username
和$password
与$user
数组变量进行比较?
答案 0 :(得分:2)
使函数返回数组而不是字符串
更改:
return $row['username']." ".$row['password'];
要:
return array("username" => $row['username'], "password" => $row['password']);
答案 1 :(得分:1)
你必须返回一个数组:return $row;
而不是$row['username']." ".$row['password'];
它是一个字符串
答案 2 :(得分:0)
<?php
session_start();
require_once('connectie.php');
if(isset($_POST['submit'])){
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$username = $db->real_escape_string($username);
$password = $db->real_escape_string($password);
$sql = "SELECT id, username, password FROM gebruikers WHERE username = '$username' AND password = '$password'";
$result = $db->query($sql);
if($result->num_rows == 1){
$row = $result->fetch_object();
$_SESSION['loggedin'] = true;
$_SESSION['id'] = $row->id;
$_SESSION['username'] = $row->username;
print_r($result);
header('location: ../index.php ');
}else{
session_destroy();
echo "NOPE";
}
}
?>
这就是我在登录系统中使用的内容。
答案 3 :(得分:0)
最佳选择是将您的POST参数与数据库中存在的用户名和密码进行比较并获取计数。如果count为1,则表示认证成功。 否则返回0
$query = "select count(*) from tbname where username = '".$_POST['username']."' and password = '".$_POST['password']."'";
$run = mysqli_query($query);
$result = mysqli_fetch_array($run);
if($result['count']==1){
header("location: index.php");
}else{
$message = "Invalid user name or password, please try again!";
}