> For the complete documentation index, see [llms.txt](https://wiki.vtx.team/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.vtx.team/panel/whmcs-ssologin.md).

# WHMCS 自动登录接口

因 WHMCS 无法真正关闭 CSRF TOKEN，导致客户端在所有请求前都会先请求一遍 CSRF TOKEN，造成客户端内购相应缓慢等问题。

同时，WHMCS 的支付接口为自身渲染，客户端无法适配，在创建账单后需要跳转浏览器支付。

而所有的跳转（包括账单、流量重置等）都需要重新输入账号密码登录，为提升体验，增加如下自动登录接口，避免跳转后需要输入账号密码登录的问题。

#### 使用方法：将如下代码保存名为 ssologin.php 的文件，放入 WHMCS 根目录

{% hint style="danger" %}
请注意将代码中 `$adminUsername = 'vortex'` 的 `'vortex'` 修改为 WHMCS 的管理员用户名
{% endhint %}

```php
<?php

require "init.php";

// 获取用户的 Cookie 字符串
$cookie = $_SERVER['HTTP_COOKIE'] ?? '';

// 使用正则表达式提取包含 "WHMCS" 后面有随机内容的 token 值
$pattern = '/WHMCS.*?=(\w+)/';
preg_match($pattern, $cookie, $matches);

// 若未找到 token
if (empty($matches[1])) {
    // 设置 HTTP 状态码为 403
    http_response_code(403);
    // 返回错误消息
    $err = [
        "result" => "error",
        "message" => "未获取到 Token",
    ];
    // 输出 JSON 格式的错误消息
    echo json_encode($err);
    exit;
}

// $token = $matches[1];
$clientId = WHMCS\Session::get("uid");
// 从查询字符串中获取 destination 参数
$destination = $_GET['destination'] ?? 'clientarea:product_details';

$command = 'CreateSsoToken';
$postData = array(
    'client_id' => $clientId,
    'destination' => $destination,

);
$adminUsername = 'vortex'; // 填写管理员用户名 Optional for WHMCS 7.2 and later

$results = localAPI($command, $postData, $adminUsername);
header('Content-Type: application/json');
// 构建要返回的数据
$responseData = ['data' => $results];

echo(json_encode($responseData));

?>
```

> 在此鸣谢 @StarGleam 为 Vortex 定制的该自动登录接口
