php编写修改个⼈信息代码,php实现简单的⽤户注册,登录,修改
个⼈信息接⼝
1. 连接数据库
白羊歌词数据库名为eating, user表⽤于存放⽤户的信息
//connect_mysql.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
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
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
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
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;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论