简体   繁体   English

PHP PDO,请求用户名

[英]PHP PDO, request users name

I have php dpo request:我有 php dpo 请求:

<?php
    include '../reg/db.php';
    $stmt = $db->query("SELECT * FROM users");
    while ( $row = $stmt->fetch() ) {
    $password_hashed = $row['password'];
    $username = $row['name'];
}

Then, when I need to echo name of user, which just logged in, but it shows wrong name !然后,当我需要回显刚刚登录的用户名时,却显示错误的名称 (ex I am Alex, he write I am john (John is already in DB, dont understand why it chooses other name) (例如我是亚历克斯,他写我是约翰(约翰已经在数据库中,不明白为什么选择其他名字)

if ( password_verify( $_POST['password'], $password_hashed )  ) { ?>
   <div class="enterReg">
     <div class="frameworkForUser">
     <div class="usernameMargin">
     <? echo $username; ?>
   </div>
<? } ?>

How could I get name of the user which just logged in?我怎么能得到刚刚登录的用户的名字? Surfed the internet a bit but haven't found anything about it .-.在互联网上浏览了一下,但没有找到任何关于它的信息.-.

please reference the user properties that you want to select the SQL queries using their post variables.请参考您要使用其发布变量选择 SQL 查询的用户属性。

something like:就像是:

$username =$_POST['username'];
$password = password=$_POST['password'];

 $stmt = $db->query("SELECT * FROM users where username='$username' and password= '$password'");

While this might solve your problem but Please do not use this code in production for fear of sql inject,html scripting Attack etc.虽然这可能会解决您的问题,但请不要在生产中使用此代码,因为害怕sql 注入、html 脚本攻击等。

You can better use Mysqli Prepared statement or better use PDO.您可以更好地使用Mysqli Prepared 语句或更好地使用 PDO。

to sanitize against html elements attack, you can use strip_tags() function要清理 html 元素攻击,您可以使用 strip_tags() 函数

Example例子

$username =strip_tags($_POST['username']);
$password = strip_tags(password=$_POST['password']);

Updated Answer更新答案

Option 1 using sessions选项 1 使用会话

<?php


    include '../reg/db.php';
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);


if ($username == ''){
echo "username is empty";
exit();
}


if ($password == ''){
echo "Password is empty";
exit();
}



$stmt = $db->prepare('SELECT * FROM users where username = :username and password = :password');

        $result->execute(array(
            ':username' => $username, ':password' => $password));

$count = $stmt->rowCount();
$row = $result->fetch();

if( $count == 1 ) {
echo "login sucessful";
// Redirect me to a Dasboard.php to display the login user
echo "<script>window.location='dashboard.php'</script>";


//Initialize a session
session_start()

//Prevent session Fixation Attack using session generated Id;
session_regenerate_id();

// pass users info in a session if things where ok.
// Prevent xss Attack using Htmlspecialchar or htmlentities() functions

$_SESSION['name'] = htmlspecialchars($row['name']);
$_SESSION['username'] = htmlspecialchars($row['username']);

// Do not pass users password in a session
//$_SESSION['password']=htmlspecialchars($row['password']);

}
else {

echo "<font color=red>Either Username or Password is Wrong</font>";
}


?>

on dashboard.php you can now have the code below depending on your implementations.....dashboard.php上,您现在可以根据您的实现拥有以下代码.....

// initialize sessions

session_start();

// the the authenticated users parameters

Name: <?php echo $_SESSION['name']; ?><br>
userName: <?php echo $_SESSION['username']; ?><br>

option 2 No session usage选项 2 不使用会话

<?php


    include '../reg/db.php';
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);


if ($username == ''){
echo "username is empty";
exit();
}


if ($password == ''){
echo "Password is empty";
exit();
}



$stmt = $db->prepare('SELECT * FROM users where username = :username and password = :password');

        $result->execute(array(
            ':username' => $username, ':password' => $password));

$count = $stmt->rowCount();
$row = $result->fetch();

if( $count == 1 ) {
echo "login sucessful";
// Redirect me to a Dasboard.php to display the login user
//echo "<script>window.location='dashboard.php'</script>";


$name = htmlspecialchars($row['name']);
$username = htmlspecialchars($row['username']);

echo "userame: $username </br>";
echo "Name: $name </br>";


}
else {

echo "<font color=red>Either Username or Password is Wrong</font>";
}


?>

Let me know if you are having any issue.如果您有任何问题,请告诉我。 Remember the password in the code is not hashed.请记住,代码中的密码没有经过哈希处理。 it assumes plain text password is in your database..它假设纯文本密码在您的数据库中..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM