php实现简单的用户注册,登录,修改个人信息接口

php实现简单的用户注册,登录,修改个人信息接口


1. 连接数据库

数据库名为eating, user表用于存放用户的信息

//connect_mysql.php
<?php
header("Content-type:text/html;charset=utf-8"); //定义编码和页面
header("Access-Control-Allow-Origin: *");   //跨域问题

session_start();    //开启会话

$host = 'localhost'; //主机地址
//$host = '118.89.25.120';
$database = 'eating';   //数据库名
$username = 'root'; //数据库的用户名
$password = '123456'; //数据库的密码
/*
 连接数据库
 */
$link = mysqli_connect($host, $username, $password);    
mysqli_select_db($link, "eating");
mysqli_query($link,"set names 'utf8'");//编码转化
if (!$link) {

    die("could not connect to the database.\n" . mysqli_error($link));//诊断连接错误
}
?>

2. 用户登录

  php实现用户登录,用户名为电话号码, php后台接受并解析前端通过http的post方法发送的json数据, 判断用户是否存在,
若不存在则返回-1, 若用户存在,则检验用户名和密码是否一致, 若一致则返回1, 若密码错误则返回-2.

//login.php
<?php
header("Content-type:text/html;charset=utf-8");
header("Access-Control-Allow-Origin: *");   //跨域

session_start();

if(isset($_POST["button"]) && $_POST["button"] == "登录") {
    $tel = $_POST["tel"];   //用户电话号码
    $password = $_POST["password"]; //用户密码

    //include ("connect_mysql.php");
    require_once('connect_mysql.php');

    //判断用户是否存在
    $sql = "SELECT * FROM user WHERE user_tel = '$_POST[tel]';";

    $result = mysqli_query($link, $sql);    //执行sql语句,返回查询后的结果集
    $rows = mysqli_num_rows($result); //返回结果集中行的数量

    if ($rows == 0) {     //用户不存在
        $json_arr = array('success' => -1);
    }
    else {
        $sql = "SELECT password FROM user WHERE password = '$_POST[password]' AND user_tel = '$_POST[tel]'";
        $result = mysqli_query($link, $sql);
        $rows = mysqli_num_rows($result);

        if ($rows == 1) { //密码正确
            $json_arr = array('success' => 1);

            $_SESSION["is_login"] = "true";
            $_SESSION["tel"] = $tel;
            $_SESSION["password"] = $password;
        }
        else {  //密码错误
            $json_arr = array('success' => -2);
        }
    }
    $login_json = json_encode($json_arr, TRUE); //转化为json数据
    echo $login_json;//发送json数据
}

3.用户注册

  用户注册帐号时使用手机号注册,php后台首先检查用户提交的信息是否完整,若不完整则返回0;接着检验
用户的手机号是否已被注册,0若手机号已经被注册,则向前端返回-1;若手机号没有被注册,则检验用户两次输
入的密码是否一致,若一致,则注册成功,向前端返回1,若不一致返回-2;否则返回-3.

// register.php
<?php

header("Access-Control-Allow-Origin: *");

session_start();

if(isset($_POST["submit"]) && $_POST["submit"] == "马上注册") {
    $tel = $_POST["tel"];
    $password = $_POST["password"];
    $password_conf = $_POST["confirm"];
    $hometown = $_POST["hometown"];
    $tasty = $_POST["tasty"];
    $type_of_cooking = $_POST["type_of_cooking"];

    if($tel == "" || $password == "" || $password == "" || $password_conf == "" || $hometown == "" || $tasty == "" || $type_of_cooking == "") {  //用户信息不完整

        $json_arr = array('success' => 0);
    }
    else {
        if($password == $password_conf) {
            include "connect_mysql.php";
            //require_once('connect_mysql.php');

            $sql = "select tel from user where tel = '$_POST[tel]'"; //SQL语句
            $result = mysqli_query($link, $sql);    //执行SQL语句
            $num = mysqli_num_rows($result); //统计执行结果影响的行数
            if($num) {   //如果已经存在该用户

                $json_arr = array('success' => -1);
            }
            else {   //不存在当前注册用户名称

                $sql_insert = "insert into user (user_tel, password, hometown, tasty, type_of_cooking)
                      values('$_POST[tel]', '$_POST[password]', '$_POST[hometown]','$_POST[tasty]', '$_POST[type_of_cooking]')";
                $res_insert = mysqli_query($link, $sql_insert);
                //$num_insert = mysql_num_rows($res_insert);
                if($res_insert) {   //注册成功
                    $json_arr = array('success' => 1);
                }
                else { //系统忙碌,请稍后重试
                    $json_arr = array('success' => -3);
                }
            }
        }
        else {  //密码不一致
            $json_arr = array('success' => -2);
        }
    }
    $register_json = json_encode($json_arr, TRUE);
    echo $register_json;
}
?>

4.修改用户信息

//alter_info.php
<?php

header("Content-type:text/html;charset=utf-8");
header("Access-Control-Allow-Origin: *");

session_start();

if (isset($_POST["submit"]) && $_POST["submit"] == "确认修改") {
    include "connect_mysql.php";
    //require_once('connect_mysql.php');

    $tel = $_POST['tel'];
    $new_hometown = $_POST['new_hometown']; //家乡
    $new_tasty = $_POST['new_tasty'];   //口味
    $new_type_of_cooking = $_POST['new_type_of_cooking'];   // 品类(干/汤)

    $sql = "UPDATE user SET hometown = '$new_hometown', tasty = '$new_tasty', type_of_cooking = '$new_type_of_cooking' 
            WHERE user_tel = '$_POST[tel]';";
    $result = mysqli_query($link, $sql);

    if ($result) {  //修改信息成功
        $json_arr = array('success' => 1);
    }
    else {  //修改信息失败
        $json_arr = array('success' => 0);
    }

    $login_json = json_encode($json_arr);
    echo $login_json;
}

5.修改密码

//alter_password.php
<?php

header("Content-type:text/html;charset=utf-8");
header("Access-Control-Allow-Origin: *");

session_start();

if (isset($_POST["submit"]) && $_POST["submit"] == "修改密码") {
    //include "connect_mysql.php";
    require_once('connect_mysql.php');

    $tel = $_POST['tel'];
    $old_password = $_POST['old_password'];

    $sql = "SELECT password, user_tel FROM user WHERE password = '$old_password' AND user_tel = '$tel';";
    $result = mysqli_query($link, $sql);
    $row = mysqli_num_rows($result);

    if ($row == 1) {
        $new_password = $_POST['new_password'];
        $new_password_conf = $_POST['new_password_conf'];

        if ($new_password == $new_password_conf) {
            $sql = "UPDATE user SET password = '$new_password' WHERE user_tel = '$tel';";
            $result = mysqli_query($link, $sql);

            if ($result) {  //密码修改成功
                $arr = array('success' => 1);
            }
        }
    }
    else {  //  密码修改失败
        $arr['success'] = 0;
    }
    $json_arr = json_encode($arr, TRUE);
    echo $json_arr;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38253837/article/details/78487787
文章标签: 数据库 php 后台
个人分类: php服务端开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭