PHP and UniApp implement data login verification and single sign-on
Introduction:
With the rapid development of the mobile Internet, users are concerned about login and data sharing between multiple platforms and multiple applications. The demand is getting higher and higher. This article will introduce how to use PHP and UniApp to implement data login verification and single sign-on functions, so that users can easily log in to multiple applications and achieve unified management and sharing of data.
1. PHP implements login verification
<?php if($_SERVER['REQUEST_METHOD'] == 'POST'){ // 获取POST数据 $username = $_POST['username']; $password = $_POST['password']; // 连接数据库 $conn = mysqli_connect('localhost', 'root', 'password', 'database_name'); if(!$conn){ echo "数据库连接失败"; exit; } // 查询用户信息 $sql = "SELECT * FROM user_info WHERE username = '$username' AND password = '$password'"; $result = mysqli_query($conn, $sql); if(mysqli_num_rows($result)>0){ echo "登录成功"; }else{ echo "用户名或密码错误"; } } ?>
This code first receives the POST data sent by the client, then connects to the database to query user information, and finally returns login success or failure based on the query results. news.
2. UniApp implements single sign-on
<template> <view> <input type="text" v-model="username" placeholder="请输入用户名" /> <input type="password" v-model="password" placeholder="请输入密码" /> <button @tap="login">登录</button> </view> </template> <script> export default { data() { return { username: '', password: '' }; }, methods: { login() { uni.request({ url: 'http://localhost/login.php', method: 'POST', data: { username: this.username, password: this.password }, success: (res) => { if(res.data === '登录成功'){ uni.showToast({ title: '登录成功', icon: 'success' }) // 登录成功后的逻辑处理 }else{ uni.showToast({ title: '登录失败', icon: 'none' }) } } }); } } } </script>
In this code, after the user enters the username and password, clicking the login button will trigger the login() method and send a POST to the server through uni.request() Request, sending the username and password entered by the user. According to the data returned by the server, the corresponding prompt information is displayed.
// 存储用户登录信息 uni.setStorageSync('isLogin', true); uni.setStorageSync('username', this.username); // 判断用户是否登录 if(uni.getStorageSync('isLogin')){ // 已登录,执行相应操作 }else{ // 未登录,跳转到登录页面 uni.navigateTo({ url: '/pages/login' }); }
Depending on the login status, we can perform different logic processing. User login information can be stored in the local cache through the uni.setStorageSync() method, and user login information stored in the local cache can be obtained through the uni.getStorageSync() method.
Conclusion:
Through the combination of PHP and UniApp, we can realize data login verification and single sign-on functions. Users can use the same account to log in to multiple applications, thereby achieving unified management and sharing of data. I hope this article will be helpful to your study and work!
The above is the detailed content of PHP and UniApp implement data login verification and single sign-on. For more information, please follow other related articles on the PHP Chinese website!