吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 100|回复: 8
收起左侧

[经验求助] tp5的代码求助,就一行代码

[复制链接]
a25926 发表于 2024-4-14 22:55
25吾爱币
[PHP] 纯文本查看 复制代码
if (!empty($user)) {
            $token = getRandomStr(64);
            cache($token, $user);
            $user->login_time = time();
            $user->ip = $this->request->ip();
            $user->save();
            $init_count=$user->getData('init_count');
            $ip=$user->ip;





这一行:
$user->ip = $this->request->ip();
我想把获取IP的方式改成通过HTTP头中的X-Forwarded-For来获取。


$request =     hinkRequest::instance();

$ip = $request->header('x-forwarded-for');

就是不知道如何放进去改成我现有的代码,我尝试改了跑不起来,大牛求帮忙
参考资料如下:
www.php点cn/faq/549336.html

最佳答案

查看完整内容

[md]试试这个如何 ```php if (!empty($user)) { $token = getRandomStr(64); cache($token, $user); $user->login_time = time(); // 获取客户端真实 IP $request = think\Request::instance(); $ip = $request->header('x-forwarded-for'); // 如果 X-Forwarded-For 为空,则使用默认的 IP 获取方式 if (empty($ip)) { $ip = $request->ip(); } $user->ip = ...

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Focalors 发表于 2024-4-14 22:55

试试这个如何

if (!empty($user)) {
    $token = getRandomStr(64);
    cache($token, $user);
    $user->login_time = time();

    // 获取客户端真实 IP
    $request = think\Request::instance();
    $ip = $request->header('x-forwarded-for');

    // 如果 X-Forwarded-For 为空,则使用默认的 IP 获取方式
    if (empty($ip)) {
        $ip = $request->ip();
    }

    $user->ip = $ip;
    $user->save();
    $init_count = $user->getData('init_count');
}

首先获取 X-Forwarded-For 头中的 IP 地址,如果这个头为空,就使用默认的 $request->ip() 来获取 IP。然后将 IP 赋值给 $user->ip

 楼主| a25926 发表于 2024-4-14 23:12
Focalors 发表于 2024-4-14 23:03
[md]试试这个如何
```php
if (!empty($user)) {

大佬 我试了 这行报错了            $request = think\Request::instance();
Focalors 发表于 2024-4-14 23:17
a25926 发表于 2024-4-14 23:12
大佬 我试了 这行报错了            $request = think\Request::instance();

报错可能是因为没有正确引入 think\Request 类。请确保你在文件的开头引入了该类。例如:

use think\Request;
 楼主| a25926 发表于 2024-4-14 23:19
Focalors 发表于 2024-4-14 23:17
[md]报错可能是因为没有正确引入 `think\Request` 类。请确保你在文件的开头引入了该类。例如:

```ph ...

大哥我改成个了非常感谢,因为我看到其他有一处引用token协议头的代码我拿过来了 ,
[PHP] 纯文本查看 复制代码
            $token = getRandomStr(64);
            cache($token, $user);
            $user->login_time = time();
            // 获取客户端真实 IP
            $ip = $this->request->header('x-forwarded-for');

            // 如果 X-Forwarded-For 为空,则使用默认的 IP 获取方式
            if (empty($ip)) {
                $ip = $request->ip();
            }

            $user->ip = $ip;
            $user->save();
            $init_count = $user->getData('init_count');
            $user->ip = $this->request->ip();
            $user->save();
            $init_count=$user->getData('init_count');
            //$token = $this->request->header("token");
Focalors 发表于 2024-4-14 23:21
a25926 发表于 2024-4-14 23:19
大哥我改成个了非常感谢,因为我看到其他有一处引用token协议头的代码我拿过来了 ,[mw_shl_code=php,tru ...

楼主客气,没帮上什么忙
shuisanyue 发表于 2024-4-14 23:30
看看这样可以吗?
if (!empty($user)) {
            $token = getRandomStr(64);
            cache($token, $user);
            $user->login_time = time();
            $user->ip = $this->request->header('X-Forwarded-For');
            $user->save();
            $init_count=$user->getData('init_count');
            $ip=$user->ip;
}

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
a25926 + 1 + 1 这样可以 已经解决了 非常感谢!真不好意思 已经给了最佳答案您才回复的不.

查看全部评分

shuisanyue 发表于 2024-4-15 00:40
这样测试了一下是可以的:
<?php
// 从X-Forwarded-For头中获取客户端的真实IP地址
function getRealClientIp() {
    // 检查X-Forwarded-For头是否存在
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        // 取X-Forwarded-For头中的第一个IP地址,这通常是客户端的IP
        $ipList = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $ip = trim($ipList[0]);
    } else {
        // 如果X-Forwarded-For头不存在,回退到REMOTE_ADDR
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

// 调用函数并打印客户端的真实IP地址
$clientIp = getRealClientIp();
echo "Client's real IP address: " . $clientIp;
?>
 楼主| a25926 发表于 2024-4-15 02:54
shuisanyue 发表于 2024-4-15 00:40
这样测试了一下是可以的:

收到 !辛苦了 非常感谢
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-1 10:10

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表