Coldfusion url变量问题

发布于 2024-12-03 23:25:05 字数 558 浏览 1 评论 0原文

有时我们会传递 url 字符串,如下所示:

http://www.oursite.com/index.cfm?layout=displayNews&newsArticle=1837

注意 url 中的 "&"。这会导致接收页面出现问题,该页面将 url 变量视为 url.AMP;newsArticle 我们无法始终控制它是以这种方式到达还是以“&”形式到达。

在接收页面中,我们希望处理任一格式可能到达的可能性。我做了一些无效的尝试,例如

<cfif structkeyexists(url,'AMP;NEWSARTICLE')>
     <cfset url.newsArticle = evaluate('#url.AMP;NEWSARTICLE#')> <!--- this line errors on the semicolon after AMP --->
</cfif>

任何指针都非常感谢。

Sometimes we are passing url strings such as follows:

http://www.oursite.com/index.cfm?layout=displayNews&newsArticle=1837

Notice the "&" in the url. This causes issues on the receiving page, which sees the url variable as url.AMP;newsArticle We cannot always control whether it arrives this way or as "&".

In the receiving page, we would like to deal with the possibility that either format may arrive. I've made a few weak attempts that don't work, e.g.

<cfif structkeyexists(url,'AMP;NEWSARTICLE')>
     <cfset url.newsArticle = evaluate('#url.AMP;NEWSARTICLE#')> <!--- this line errors on the semicolon after AMP --->
</cfif>

Any pointers greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷夜 2024-12-10 23:25:05

替换 cgi.query_string 中的字符串不会解决问题,因为此时 URL 范围已填充。

您可以将值作为结构引用:

<cfset url.newsarticle = url['amp;newsarticle']>

因此,将其与 structKeyExists 结合起来:

<cfif structkeyexists(url,'AMP;NEWSARTICLE')>
    <cfset url.newsarticle = url['amp;newsarticle]>
</cfif>

进一步扩展,您可以过滤掉整个 URL 范围:

<cfloop collection="#url#" item="key">
    <cfif left(key,4) eq "amp;">
        <cfset url[replace(key,"amp;","")] = url[key]>
    </cfif>
</cfloop>

Replacing strings in cgi.query_string won't fix the problem, as the URL scope is populated at that point.

You can reference the value as a struct:

<cfset url.newsarticle = url['amp;newsarticle']>

So combine that with structKeyExists:

<cfif structkeyexists(url,'AMP;NEWSARTICLE')>
    <cfset url.newsarticle = url['amp;newsarticle]>
</cfif>

Extending this further, you can filter out the entire URL scope:

<cfloop collection="#url#" item="key">
    <cfif left(key,4) eq "amp;">
        <cfset url[replace(key,"amp;","")] = url[key]>
    </cfif>
</cfloop>
野侃 2024-12-10 23:25:05

您可以尝试

 replaceNoCase(cgi.query_string, '&', '&', 'all')

然后正常处理它..这样只有 html 实体被替换...

-sean

You might try a

 replaceNoCase(cgi.query_string, '&', '&', 'all')

Then just process it normally.. that way only the html entities get replaced...

-sean

南烟 2024-12-10 23:25:05

可能有一种更干净的方法......但这似乎对我有用(语法假设 CF9):

<cfscript>
    cleanURL = {};

    for ( var item in URL ) {
        if ( left( item, 4 ) == "amp;" ) {
            key = replaceNoCase( item, 'amp;', '' ); // right() also works here
        } else {
            key = item;
        }

        cleanURL[ key ] = URL[ item ];
    }
</cfscript>

<cfdump var="#cleanURL#" />

There may be a cleaner way... but this seems to work for me (syntax assumes CF9):

<cfscript>
    cleanURL = {};

    for ( var item in URL ) {
        if ( left( item, 4 ) == "amp;" ) {
            key = replaceNoCase( item, 'amp;', '' ); // right() also works here
        } else {
            key = item;
        }

        cleanURL[ key ] = URL[ item ];
    }
</cfscript>

<cfdump var="#cleanURL#" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文