dtlzdofl66441 2012-06-11 07:51
浏览 34
已采纳

我的PHP代码发送错误发送短信[关闭]

Am a newbie and i am getting an error on line 26 of my code i cant understand what causes the error as it is making me not to be able to send sms anyhelp will be appreciated

        <?php
        $gw_host="10.0.0.9";
     $value=$_POST['value'];
     urlencode($message)=$_POST['message'];
     $con = mysql_connect("localhost","db_host","xxxxxx");

         if (!$con)
         {
         die('Could not connect: ' . mysql_error());
         }

         mysql_select_db("aic_sms", $con);

     $result = mysql_query("SELECT phn_number FROM users WHERE message=$value");
     if($result){
     while($row = mysql_fetch_array($result)) {
         function sendSmsMessage($phn_number, $message)
        {
            $ch= curl_init();
        curl_setopt($ch, "http://10.0.0.15/process_sms/sendsms.php?recipient=$phn_number&msg=" . urlencode($message));
        curl_exec($ch);
        curl_close($ch);

           }
       }
    else {
       echo mysql_error();
   }
   }

   echo 'Message sent successfully';    



      mysql_close($con);
      ?>
  • 写回答

1条回答 默认 最新

  • duanchensou8685 2012-06-11 08:16
    关注

    Ok, first off you have a function within a loop this will cause the function tobe defined twice then kill your script, with functions you define them once then use the function name to execute the code within the function, also the actual curl function you are using wont work as your not setting the correct options.

    Plus you should use prepared query's to execute SQL statements, as in your current script you are vulnerable to sql injection, someone malicious could post someval OR 1=1 this would cause mysql to return everything as 1=1 which is true.

    Heres how you should perhaps do it and build appon it. Hope it helps

    if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST['value']) && !empty($_POST['message'])){
    
        $value  = $_POST['value'];
        $message= $_POST['message'];
    
        try {
            $dbh = new PDO("mysql:host=localhost;dbname=aic_sms", 'username', 'password');
    
            $stmt = $dbh->prepare("SELECT phn_number FROM users WHERE message=:message");
    
            /*** bind the paramaters ***/
            $stmt->bindParam(':message', $value, PDO::PARAM_STR);
    
            /*** execute the prepared statement ***/
            $stmt->execute();
    
            /*** fetch the results ***/
            while($row = $stmt->fetch())
            {
                $result[$row['phn_number']] = sendSmsMessage($row['phn_number'], $message);
            }
            //perhaps do somthing with the $result array
            echo 'Messages sent successfully';   
        }
        catch(PDOException $e)
        {
            die($e->getMessage());
        }
    }else{
        //Show form or whatever
    }
    
    
    function sendSmsMessage($phn_number, $message){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, "http://10.0.0.15/process_sms/sendsms.php?recipient=$phn_number&msg=" . urlencode($message));
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    
        $html = curl_exec($curl);
        curl_close($curl);
    
        return $html;
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器