简体   繁体   English

Mysql PDO PHP更改数据库名称不会引发任何错误,但会杀死我的页面

[英]Mysql PDO PHP Changed database name throws no error but kills my page

i have a problem with my database and error checking. 我的数据库和错误检查有问题。

If a hacker decided to change the name of my database or any of the tables it will kill my page. 如果黑客决定更改我的数据库或任何表的名称,它将杀死我的页面。

I found this out after trying my webpage on another computer where i had the favorites folder named favourites. 我在另一台计算机上尝试了我的网页后发现了这一点,在该计算机上,我的收藏夹文件夹名为“收藏夹”。

I have tried everything from try catch error handling to checking for a count and then throwing to an error page if 0 count returned. 我已经尝试了从尝试捕获错误处理到检查计数,然后如果返回0计数则抛出错误页面的所有方法。 Iv tried allsortsto no avail. iv尝试allsortsto无济于事。

i would normally send my user to a error page. 我通常会将我的用户发送到错误页面。 But i cannot re-directg with a header location because the checking is in the middle of a PDO and while loop 但是我无法使用标头位置重新定向,因为检查是在PDO和while循环中间

il show you the code. il给你看代码。

How can i avoid this problem 我如何避免这个问题

If the favorites database name is changed to anything other than favorites. 如果收藏夹数据库名称更改为收藏夹以外的其他名称。 it kills the page... But half of the page gets displayed. 它杀死了页面...但是页面的一半被显示。 But i cannot redirect with header location if i try to catch the error.. Which is not even registering as an error.. 但是,如果我尝试捕获错误,则无法重定向标头位置。甚至没有注册为错误。

if ($_SESSION['loggedin']==='001'){


if ($db = new PDO("mysql:host=localhost;dbname=favourrites", '????', '')){
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

   //add ;pagination order by limit
   $query11="SELECT * FROM `$useremail` WHERE `faveID` = '$listID' ";

   $stat11=$db->prepare($query11);
    $stat11->execute();

        while($row = $stat11->fetch()){
        $faveID=$row['faveID'];
        }


          if(!isset($faveID)) {$faveID="000";}
          else                                              
          if(empty($faveID)) {$faveID="000";}
          else
          if(!$faveID) {$faveID="000";}


     if ($faveID===$listID){
     echo"
         <img id='favicon[$fid]' data-variable-uid='{$UID}' data-variable- 
          listid='{$listID}' data-variable-accountname='{$accountname}' 
          src='../images/mainpage/fave1.png' title='Allready Added To 
          Favorites' class='iconlarge' style='opacity:0.9' 
          onclick='favecheck($fid);'></img>";            
    }
    else
        {               
         echo" 
         <img id='favicon[$fid]' data-variable-uid='{$UID}' data-variable- 
         listid='{$listID}' data-variable-accountname='{$accountname}' 
         src='../images/mainpage/fave2.png' title='Add To Your Favorites 
         List' class='iconlarge' style='opacity:1' 
         onclick='favecheck($fid);'></img>"; 
        }

}else{

error_reporting(E_ALL); 
ini_set("display_errors", 1);
header("Location: ../imageupload/error");
die();


echo '<script type="text/javascript">';
echo 'window.location.href="../imageupload/error.php";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; 
exit();

}

}       

The above code has added attempts to re-direct but none have worked so far.including many others 上面的代码增加了重定向的尝试,但到目前为止都没有成功,包括许多其他

like checking for a count result ect... 像检查计数结果等...

How do i redirect to my error page. 我如何重定向到我的错误页面。

I tried a function call, but im not familiar with PHP functions and i attempted to call it like a jscript function. 我尝试了一个函数调用,但是我不熟悉PHP函数,并且试图像jscript函数一样调用它。

Any help is greatly needed. 非常需要任何帮助。

Thank you. 谢谢。

if a hacker has access to change the db name, its kind of all over. 如果黑客有权更改数据库名称,则该名称将全部更改。

but you can do this 但是你可以做到

try {
    $db = $db = new PDO("mysql:host=localhost;dbname=favourrites", '????', '')
} catch (PDOException $e) {
    //echo 'Connection failed: ' . $e->getMessage(); // i suggest you do something with the error
    header("Location: ../imageupload/error");
    die();
}

in the catch do the redirect. 在赶上做重定向。 But again you really should be in a position where this could ever happen. 但是,您确实应该处于有可能发生这种情况的位置。

Have 2 database users, 1 is a "super-user" that is used by yourself when you're directly accessing the database. 有2个数据库用户,其中1个是“超级用户”,您在直接访问数据库时会使用它。

The second user is the "applications user" which your app/page uses to access it's own database, this one is "locked down" so that it just has permission to do just what it needs to it's own tables. 第二个用户是您的应用程序/页面用来访问其自己的数据库的“应用程序用户”,该用户被“锁定”,因此它仅具有对自己的表执行所需操作的权限。 Anything that it should not be able to do, it's set so that it doesn't have permission to do that action/access a table. 它应该执行的所有操作均已进行了设置,以使其无权执行该操作/访问表。

You should ALWAYS use prepared statements for ALL queries so that you don't accidentally open up any security holes (SQL Injection attacks) if you were to change the source of any data to that provided by a user. 如果要将任何数据源更改为用户提供的数据源,则应始终所有查询使用准备好的语句,以免意外打开任何安全漏洞(SQL注入攻击)。 ( NEVER trust any data submitted by any user, no matter how well you know them. (无论您对信息的了解程度如何,都永远不要信任任何用户提交的任何数据。

If the database is unavailable for any reason depending on your app you might want to consider exiting the script, echoing a user-friendly error message to the user (but not giving away any technical details, log the details of the error to your error logs) 如果数据库由于某种原因由于您的应用程序而不可用,则您可能要考虑退出脚本,向用户回显用户友好的错误消息(但不放弃任何技术细节,请将错误的详细信息记录到错误日志中) )

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

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