doushan7997 2016-11-15 17:40
浏览 44
已采纳

MySQL num_rows返回误报

I have this query below:

// They have a token and estimate id
if (isset($_GET['estimate_token']) && isset($_GET['estimate_id']))
{
    if ($select = $db -> prepare("SELECT estimate_id FROM estimates WHERE estimate_token =?"))
    {
        $select -> bind_param('s', $_GET['estimate_token']);
        $select -> execute();
        $select -> store_result();
        $select -> bind_result($estimate_id);
        $select -> fetch();
        if ($select -> num_rows == '0')
        {
            header ("Location: ./login.php");
        }else{
        }
        $select -> close();
    }
}

Customers are given a link via email with the token and an estimate id from the database. When they click the link it brings them to the correct estimate. The problem I am having is that if the customers manually replaces the estimate_id or estimate_token with any number in the url it still keeps you on the website where it should kicking you to the login.php. This is bad because it allows customers to view other estimates in the system.

I think the problem lies in the $select -> num_rows throwing a false positive.

  • 写回答

3条回答 默认 最新

  • dongruoqiong9017 2016-11-15 17:49
    关注

    You logic is invalid - you must find a record where both token and id equals to $_GET values, so you need to use query like:

    $SELECT estimate_id FROM estimates WHERE estimate_token =? AND estimate_id = ?
    

    This will select only one certain record.

    Full code is something like:

    if ($select = $db -> prepare("SELECT estimate_id FROM estimates WHERE estimate_token = ? and estimate_id = ?"))
    {
        // supposing id is `int`
        $select -> bind_param('si', $_GET['estimate_token'], $_GET['estimate_id']);
        $select -> execute();
        $select -> store_result();
        // if you need to know just if row exists
        // there's no need for this two lines
        //$select -> bind_result($estimate_id);
        //$select -> fetch();
        if ($select -> num_rows == 0)
        {
            header ("Location: ./login.php");
        } else {
        }
        $select -> close();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退