login.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. header("Content-Type: text/html; charset=utf8");
  3. function getJson($result,$message){
  4. $json_array = array(
  5. "result"=>$result,
  6. "message"=>$message
  7. );
  8. return json_decode($json_array);
  9. }
  10. if(!isset($_POST["submit"])){
  11. exit("错误执行");
  12. }//检测是否有submit操作
  13. include('database_connect .php');//链接数据库
  14. $name = $_POST['name'];//post获得用户名表单值
  15. $passowrd = $_POST['password'];//post获得用户密码单值
  16. if ($name && $passowrd){//如果用户名和密码都不为空
  17. $sql = "select * from user_info where user_name = '$name' and password='$passowrd'";//检测数据库是否有对应的username和password的sql
  18. $result = mysql_query($sql);//执行sql
  19. $rows=mysql_num_rows($result);//返回一个数值
  20. if($rows){//0 false 1 true
  21. echo getJson(0,"登陆成功")//如果成功跳转至welcome.html页面
  22. exit;
  23. }else{
  24. echo getJson(-1,"用户名或密码错误");
  25. }
  26. }else{//如果用户名或密码有空
  27. echo getJson(-2,"表单填写不完整");
  28. }
  29. mysql_close();//关闭数据库