一号优惠经验分享网
51福利网的各种薅羊毛福利经验笔记

uni-app的index.vue文件中怎么获取用户ip和ua然后显示在页面并传递给1.com/a.php 完整vue代码

为了在uni-app中获取用户IP地址和用户代理(UA)并将其发送给远程服务器,您可以使用uni.getNetworkType API来获取用户IP地址,并使用navigator.userAgent来获取用户代理。

以下是一个简单的示例,展示如何在index.vue文件中实现这一功能:

51福利网
<template>  
  <view>  
    <text>您的IP地址是: {{ userIP }}</text>  
    <text>您的User-Agent是: {{ userAgent }}</text>  
    <button @click="sendDataToServer">发送数据到服务器</button>  
  </view>  
</template>  
  
<script>  
export default {  
  data() {  
    return {  
      userIP: '',  
      userAgent: navigator.userAgent,  
    };  
  },  
  mounted() {  
    this.getUserIP();  
  },  
  methods: {  
    getUserIP() {  
      uni.getNetworkType({  
        success: (res) => {  
          this.userIP = res.networkType;  
        },  
      });  
    },  
    sendDataToServer() {  
      const xhr = new XMLHttpRequest();  
      xhr.open('POST', 'https://1.com/a.php', true);  
      xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  
      xhr.onreadystatechange = function () {  
        if (xhr.readyState === 4 && xhr.status === 200) {  
          console.log(xhr.responseText); // 处理服务器响应  
        }  
      };  
      let postData = `ip=${this.userIP}&ua=${this.userAgent}`;  
      xhr.send(postData);  
    },  
  },  
};  
</script>

注意:由于跨域问题,您可能无法直接将数据发送到 https://1.com/a.php。您可能需要配置CORS或在您的服务器端设置代理来处理此请求。此外,出于安全考虑,不建议在客户端暴露敏感信息或直接发送数据到服务器。

修改上面代码

uni-app获取ip地址没有现成的API,原理就是请求一个公共的网址,根据返回的数据截取出自己的IP地址

自动发送 不需要点按钮发送

③ua简化字符,传输纯字符

为了自动发送数据,您可以在mounted()生命周期钩子中调用sendDataToServer()方法,这样在页面加载时就会自动发送数据。以下是修改后的代码:

<template>  
  <view>  
    <text>您的IP地址是: {{ userIP }}</text>  
    <text>您的User-Agent是: {{ userAgent }}</text>  
  </view>  
</template>  
  
<script>  
export default {  
  data() {  
    return {  
      userIP: '',  
      userAgent: navigator.userAgent,  
    };  
  },  
  mounted() {  
    getUserIP() {  
				   uni.request({  
				     url: 'http://pv.sohu.com/cityjson?ie=utf-8',  
				     method: 'POST',  
				     success: (res) => {  
				       const reg = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;  
				       let ip = reg.exec(res.data);  
				       if (ip) {  
				         this.userIP = ip[0];  
				       } else {  
				         console.log('无法获取IP地址');  
				       }  
				     }  // 注意这里使用了箭头函数  
				   });  
				 },
				   
				  simplifyUserAgent() {  
				        // 去除符号和空格  
				        const simplified = this.userAgent.replace(/[^a-zA-Z0-9]/g, '').replace(/\s/g, '');  
				        this.simplifiedUserAgent = simplified;  
				      },   
				   
				   
				
			 
			     
			  
			  sendDataToServer() {  
			    if (!this.userIP || !this.simplifiedUserAgent) {  
			      this.getUserIP(); // 获取IP  
			      this.simplifyUserAgent(); // 简化User-Agent  
			    }  
			      
			    const xhr = new XMLHttpRequest();  
			    xhr.open('GET', `https://www.1.com/appindexipua.php?ip=${this.userIP}&ua=${this.simplifiedUserAgent}`, true);  
			    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  
			    xhr.onreadystatechange = function () {  
			      if (xhr.readyState === 4 && xhr.status === 200) {  
			        console.log(xhr.responseText); // 处理服务器响应  
			      }  
			    };  
			    xhr.send(); // 发送数据到服务器  
			  }
	   
};  
</script>

现在,当页面加载时,会自动发送用户IP和用户代理数据到服务器。请确保您的服务器端点(https://1.com/a.php)已正确配置并能够接收和处理这些数据。

除了ip不能传递都正常,那位大神可以指导一下

https://1.com/a.php中接收这两个参数,您可以使用超级全局变量$_POST来获取通过POST方法发送的数据。具体代码如下:

<?php  
// 获取IP地址  
$ip = $_GET['ip'];  
// 获取User-Agent  
$userAgent = $_GET['ua'];  
  ?>

打赏
一号优惠 · 51福利网薅羊毛福利具有时效性,如已失效,请留言
文章名称:《uni-app的index.vue文件中怎么获取用户ip和ua然后显示在页面并传递给1.com/a.php 完整vue代码》-一号优惠 · 51福利网
免责申明:本站所有活动信息均来自网络,如有失效,不实或侵权,请联系我们删除。谢谢

评论 抢沙发

一号优惠经验分享网最新最全薅羊毛,现金红包线报网

一号优惠经验分享网提供每日最新内部优惠,薅羊毛活动,现金红包领取,免费福利和网赚福利手机赚钱线报,打造中国最受欢迎的网赚信息发布平台!51福利网

51联盟线报群赚钱·合作·帮助

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册