You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter 3.x解析URL中urlencode编码JSON参数的问题

Fixing JSON Parameter Parsing in CodeIgniter 3.x

Hey there! I’ve dealt with exactly this kind of issue before when passing encoded JSON through URI segments in CodeIgniter 3.x. Let’s break down the problem and fix it step by step.

First, Understand the Root Issue

Looking at your example URL, the encoded JSON has extra backslashes (%5C in URL encoding) around the quotes. This usually happens because either:

  1. The frontend escaped the JSON quotes before URL-encoding it, or
  2. CodeIgniter’s global XSS filtering/magic quotes setting is auto-escaping the URI segment.

Step 1: Retrieve the Encoded Parameter Correctly

In your controller method, you can directly accept the encoded JSON as a method parameter (if your route is configured to pass it) or fetch it via CodeIgniter’s URI class:

public function myfunction($encodedJson = null) {
    // Check if the parameter exists
    if (is_null($encodedJson)) {
        show_error("Missing JSON parameter in URL");
        return;
    }

    // Alternative: Fetch via URI segment (adjust the segment number to match your route structure)
    // $encodedJson = $this->uri->segment(3);
}

Step 2: Decode and Clean the JSON String

You’ll need to reverse the URL encoding and strip any extra escape slashes before parsing the JSON:

// First, decode the URL-encoded string
$decodedStr = urldecode($encodedJson);

// Strip the extra backslashes that are breaking JSON parsing
$cleanedJson = stripslashes($decodedStr);

// Parse the cleaned JSON into an associative array
$userData = json_decode($cleanedJson, true);

// Always validate the JSON parsing result
if (json_last_error() !== JSON_ERROR_NONE) {
    show_error("Failed to parse JSON: " . json_last_error_msg());
    return;
}

// Now you can use the parsed data!
echo "User ID: " . $userData['uid'];
echo "<br>Username: " . $userData['uname'];

Step 3: Disable Unnecessary Auto-Escaping in Config

If you keep seeing extra backslashes, adjust your CodeIgniter config to turn off auto-escaping for this scenario:

  1. Open application/config/config.php
  2. Set $config['global_xss_filtering'] = false; (unless you need global XSS filtering, in which case handle XSS validation manually for this parameter)
  3. If you’re on an older PHP version with magic_quotes_gpc enabled, add this to your index.php file to strip deep slashes:
if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value) {
        return is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
    }
    $_GET = stripslashes_deep($_GET);
    $_POST = stripslashes_deep($_POST);
    $_COOKIE = stripslashes_deep($_COOKIE);
}

Alternative: Use Base64 for More Reliable Transfer

If you want to avoid escape slash headaches entirely, encode the JSON as Base64 before URL-encoding it on the frontend:

Frontend (JavaScript example):

const userJson = '{"uid":"1234","uname":"kishor"}';
const base64Json = btoa(userJson);
const encodedParam = encodeURIComponent(base64Json);
// Pass encodedParam as the final URL segment

Backend CodeIgniter Handling:

public function myfunction($encodedBase64 = null) {
    if (is_null($encodedBase64)) {
        show_error("Missing parameter");
        return;
    }

    $base64Str = urldecode($encodedBase64);
    $jsonStr = base64_decode($base64Str);
    $userData = json_decode($jsonStr, true);

    if (json_last_error() !== JSON_ERROR_NONE) {
        show_error("Invalid JSON data");
        return;
    }

    // Use $userData as needed
}

This method avoids any quote-escaping issues because Base64 uses URI-safe characters.

内容的提问来源于stack exchange,提问作者kishor10d

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:28:33