1. <?php
  2. session_start();
  3. if (isset($_POST['userid']) && isset($_POST['password'])) {
  4. $userid= $_POST['userid'];
  5. $password= $_POST['password'];
  6. $db_conn = new mysqli('localhost', 'webauth', 'webauth', 'auth');
  7. if (mysqli_connect_errno()) {
  8. echo 'Connection failed'.mysqli_connect_errno();
  9. exit();
  10. # code...
  11. }
  12. $query= 'select * from authorized_users'."where name= '".$userid."'"."&& password= sha1('".$password."')";
  13. $result= $db_conn->query($query);
  14. if ($result->num_rows) {
  15. $_SESSION['valid_user']= $userid;
  16. # code...
  17. }
  18. $db_conn->close();
  19. }
  20. ?>
  21. <html>
  22. <body>
  23. <h1>Home page</h1>
  24. <?php
  25. if (isset($_SESSION['valid_user'])) {
  26. echo "You are logged in as: ".$_SESSION['valid_user'].'<br />';
  27. echo '<a href= "logout.php">Log Out</a>';
  28. }
  29. else{
  30. if(isset($userid)){
  31. echo 'Could not log you in.<br />';
  32. }
  33. else {
  34. echo 'You are not logged in.<br />';
  35. }
  36. echo '<form method= "post" action= "authmain.php">';
  37. echo '<table>';
  38. echo '<tr><td>Userid: </td>';
  39. echo '<td><input type= "text" name= "userid"></td></tr>';
  40. echo '<tr><td>Password: </td>';
  41. echo '<td><input type= "password" name= "password"></td></tr>';
  42. echo '<tr><td colspan= "2" align= "center">';
  43. echo '<input type= "submit" value= "log in"></td></tr>';
  44. echo '</table></form>';
  45. }
  46. ?>
  47. <br />
  48. <a href="members_only.php">Members section</a>
  49. </body>
  50. </html>

if ($result->num_rows) 返回NULL,怎么回事