login.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  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. include('database_connect .php');//链接数据库
  11. $name = $_POST['name'];//post获得用户名表单值
  12. $passowrd = $_POST['password'];//post获得用户密码单值
  13. if ($name && $passowrd){//如果用户名和密码都不为空
  14. $sql = "select * from user_info where user_name = '$name' and password='$passowrd'";//检测数据库是否有对应的username和password的sql
  15. $result = mysql_query($sql);//执行sql
  16. $rows=mysql_num_rows($result);//返回一个数值
  17. if($rows){//0 false 1 true
  18. echo getJson(0,"登陆成功")//如果成功跳转至welcome.html页面
  19. exit;
  20. }else{
  21. echo getJson(-1,"用户名或密码错误");
  22. }
  23. }else{//如果用户名或密码有空
  24. echo getJson(-2,"表单填写不完整");
  25. }
  26. mysql_close();//关闭数据库