PHP代码导致我的页面变慢

时间:2018-03-19 02:38:52

标签: php html

美好的一天,这是我第一次在这里发帖。插入PHP代码时,我的页面响应时间过长:

<?php
   include("config.php");
   session_start();

   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

      $sql = "SELECT * FROM Administrators WHERE username = '$myusername' and password = '$mypassword'";
      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];

      $count = mysqli_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
         session_register("myusername");
         $_SESSION['login_user'] = $myusername;

         header("location: index.html");
      }else {
         $error = "Your User Name or Password is invalid";
      }
   }
?>

1 个答案:

答案 0 :(得分:0)

  • 衡量执行时间的方法:
    • 在您要衡量的每个部分之前插入microtime(1);;
    • 打印每个部分的实时时间并分析原因;
  • 建议:
    • 获取$ sql的内容并在其前面添加EXPLAIN,在mysql中运行整个查询以查看它是否花费了太多时间。
    • 例如,EXPLAIN SELECT * FROM Administrators WHERE username = 'myusername' and password = 'mypassword';
    • 避免SELECT *,使用SELECT用户,传递(你需要的部分)。