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条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大