为了账号安全,请及时绑定邮箱和手机立即绑定

无法访问或分配登录用户名变量 - mysql - php

无法访问或分配登录用户名变量 - mysql - php

PHP
收到一只叮咚 2021-07-03 05:49:02
我找不到一种方法来访问我用来在我的 php 网络表单上登录的用户名。我看过这些帖子,但我认为我的情况略有不同,因为它们都在顶部声明了用户名变量。我只是使用 sql 查询登录。这是登录脚本<?php$uname=$_POST['uname'];$password=$_POST['password']; session_start();$con=mysqli_connect("localhost","root","g7trj98o6fyr5","login");//mysqli("localhost","username of database","password of database","database name")$result=mysqli_query($con,"SELECT * FROM `login_info` WHERE `uname`='$uname' && `password`='$password'");$count=mysqli_num_rows($result);if($count==1){    echo "Login success";    $_SESSION['log']=1;    header("refresh:2;url=welcome.php");}else{    echo "please fill proper details";    header("refresh:2;url=index.php");}?>我希望能够使用当前登录的用户进行 if 检查,以将他们与所有其他在线用户区分开来。我有这个 if 检查哪个应该只将编辑按钮放在登录的用户旁边。while($row = mysqli_fetch_array($result)){echo "<tr>";echo "<td>" . $row['uname'] . "</td>";echo "<td>" . $row['clickrate'] . "</td>";if($logedInUsername == $row['uname'])    echo "<td>" . $row['yourword'] . "<a href='edityourword.php?edit=$row[yourword]'> edit</a></td>";else    echo "<td>" . $row['yourword'] . "</td>";echo "</tr>";}echo "</table>";?>Welcome.php(if 语句所在的位置)<?phpfunction add_ant(&$connection){    mysqli_query($connection, "UPDATE `login_info` SET `clickrate`=`clickrate`+'1' WHERE `uname`='rvbvakama' && `password`='pass'");}session_start();if(isset($_SESSION['log'])){$_SESSION['uname'] = $_POST['uname'];echo "<script type='text/javascript'>alert('$logedInUsername');</script>";$con=mysqli_connect("localhost","root","pass","login"); //mysqli("localhost","username of database","password of database","database name")if(array_key_exists('add',$_POST)){   add_ant($con);}$result = mysqli_query($con,"SELECT * FROM login_info");if (!$result) {    printf("Error: %s\n", mysqli_error($con));    exit();}echo "<table border='1'><tr><th>username</th><th>clickrate</th><th>yourword</th></tr>";
查看完整描述

2 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

不安全,但把:


<?php

$uname=$_POST['uname'];

$password=$_POST['password']; 

session_start();


$con=mysqli_connect("localhost","root","g7trj98o6fyr5","login");//mysqli("localhost","username of database","password of database","database name")

$result=mysqli_query($con,"SELECT * FROM `login_info` WHERE `uname`='$uname' && `password`='$password'");

$count=mysqli_num_rows($result);

if($count==1)

{

    echo "Login success";

    $_SESSION['log']=1;

    $_SESSION['uname'] = $_POST['uname'];

    header("refresh:2;url=welcome.php");


}

else

{

    echo "please fill proper details";

    header("refresh:2;url=index.php");

}

?>

然后检查:


if($logedInUsername == $_SESSION['uname'])

...

您还需要在welcome.php 中将$logedInUsername 设置为$row['uname'] 或检查


if($row['uname'] == $_SESSION['uname'])


查看完整回答
反对 回复 2021-07-09
?
PIPIONE

TA贡献1829条经验 获得超9个赞

登录成功后在 login.php 中执行这个 $_SESSION['uname'] = $_POST['uname'];


在welcome.php 中执行这个 $logedInUsername = $_SESSION['uname'];


现在当前登录的用户名存储在$logedInUsernamewelcome.php中以供使用,这是因为SESSION是php中的全局变量,可以从任何地方访问。


while 循环和其中的 if 语句现在应如下所示:


while($row = mysqli_fetch_array($result))

{

echo "<tr>";

echo "<td>" . $row['uname'] . "</td>";

echo "<td>" . $row['clickrate'] . "</td>";

if($row['uname'] == $logedInUsername)

    echo "<td>" . $row['yourword'] . "<a href='edityourword.php?edit=$row[yourword]'> edit</a></td>";

else

    echo "<td>" . $row['yourword'] . "</td>";

echo "</tr>";

}

echo "</table>";

?>


查看完整回答
反对 回复 2021-07-09
  • 2 回答
  • 0 关注
  • 121 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信