首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP中的简单身份验证脚本不起作用

问PHP中的简单身份验证脚本不起作用
EN

Stack Overflow用户
提问于 2011-06-01 23:44:10
回答 2查看 236关注 0票数 1

从php开始,我编写了一个基本的身份验证脚本,如果"test“数据库的user表中存在一个userid (由user提供),该脚本将打印出mysql服务器上的数据库列表。

问题是,即使数据库中不存在用户if,此脚本也会输出数据库列表。我不确定这个脚本出了什么问题。请看一下脚本,并帮助我理解为什么输出数据库列表,即使数据库中不存在用户in。下面是脚本:

代码语言:javascript
复制
<?php

if(isset($_POST['submitted']))
{
  $userid=$_POST['userid'];
  $userpassword=$_POST['userpassword'];
  $link_id=mysql_connect("localhost","root","pass");
  $result_db_list=mysql_list_dbs($link_id);
  mysql_select_db("test",$link_id);
  if(!($result_ptr=mysql_query("Select userid from user where Userid='$userid'",$link_id))) die ("Please enter correct userid");
     while($test=mysql_fetch_row($result_db_list))
      {
       echo $test[0]."<br>";
      } 

}
else
{
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Authentication Script</title>
<style type="text/css" >
  #header{
  padding-top:0px;
  margin:0px;
  background-color:#CCCCCC;
  }
  .container{
  width:950px;
  margin:0 auto;
  border:1px solid red;
  }
 .authbox {
 padding-top:100px;
 margin:0 auto;
 }
  #footer{
  background-color:#666666;
  color:white;
}
</style>
</head>

<body>
<div id="header">

<div class="container">

<form action="authentication script.php" method="post">
<div class="authbox">UserName: <input type="text" name="userid" /><br/>
Password: <input type="password" name="userpassword" /><br/>
<input type="hidden" name="submitted" value="true" />
<input type="submit" value="Submit" />
</div>
</form>


</div>

</div>

<div id="footer">
Copywright 2010 NT Technologies. 
</div>

</body>
</html>

<?php
}
?>

谢谢,rseni。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-06-01 23:48:31

你的脚本充满了错误。(我希望你至少有magic_quotes on,否则你就有大麻烦了。请注意,无论如何都应该避免使用magic_quotes,并使用预准备语句)

这是因为

代码语言:javascript
复制
  if(!($result_ptr=mysql_query("Select userid from user where Userid='$userid'",$link_id))) 
     die ("Please enter correct userid");

这就是说,如果查询没有选择任何内容,它就不会返回FALSE。

您应该将其更改为:

代码语言:javascript
复制
$result = mysql_query("SELECT COUNT(*) as countUser [etc]");
$r = mysql_fetch_assoc($result);
if ($r['countUser']==0) 
  die('Denied');
票数 3
EN

Stack Overflow用户

发布于 2011-06-01 23:57:51

Appu - yes123是正确的。查看有关mysql_query函数的php.net文档-您将看到,如果成功,它将返回一个资源标识符;如果出错,它将返回FALSE。Error在这里并不意味着没有返回任何行-而是一个错误,比如您试图对一个不存在的表运行此查询。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6203923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档