开发者

How to strip out a url variable

I have a url.LoginID, and I'd like to remove it from the address bar when the user clicks on the link to login. It has to be a bookmark, it can't be a form submit.

Q: How do I remove ?LoginID from Index.cfm?LoginI开发者_Python百科D=XYZ&AssignmentID=123

It's probably something along the lines of:

<cflocation url="#cgi.SCRIPT_NAME#?#cgi.QUERY_STRING#" addtoken="no">


Looks like you are on the right track.

If loginID is the only thing in the query string, you can simply cflocation to the destination page without the query string.

If there is other data in the query string, then you can do something like this:

<cfset q = reReplaceNoCase(cgi.query_string, "LOGINID=[^&]+&?", "")>
<cflocation url="#cgi.SCRIPT_NAME#?#q#">

This essentially removes loginid and everything until either the en of the string or the next URL variable.


As usual, there's already a UDF that someone has written available on CFLIB: queryStringDeleteVar

You can then do like so

<cflocation 
    url="#cgi.SCRIPT_NAME#?#queryStringDeleteVar("LoginID",cgi.QUERY_STRING)#" 
    addtoken="no"
>

CGI.QUERY_STRING is actually the default for the second arg, so this will work just as well

<cflocation 
    url="#cgi.SCRIPT_NAME#?#queryStringDeleteVar("LoginID")#" 
    addtoken="no"
>

Here's the code for queryStringDeleteVar:

<cfscript>
/**
 * Deletes a var from a query string.
 * Idea for multiple args from Michael Stephenson (michael.stephenson@adtran.com)
 * 
 * @param variable      A variable, or a list of variables, to delete from the query string. 
 * @param qs      Query string to modify. Defaults to CGI.QUERY_STRING. 
 * @return Returns a string. 
 * @author Nathan Dintenfass (michael.stephenson@adtran.comnathan@changemedia.com) 
 * @version 1.1, February 24, 2002 
 */
function queryStringDeleteVar(variable){
    //var to hold the final string
    var string = "";
    //vars for use in the loop, so we don't have to evaluate lists and arrays more than once
    var ii = 1;
    var thisVar = "";
    var thisIndex = "";
    var array = "";
    //if there is a second argument, use that as the query string, otherwise default to cgi.query_string
    var qs = cgi.query_string;
    if(arrayLen(arguments) GT 1)
        qs = arguments[2];
    //put the query string into an array for easier looping
    array = listToArray(qs,"&");        
    //now, loop over the array and rebuild the string
    for(ii = 1; ii lte arrayLen(array); ii = ii + 1){
        thisIndex = array[ii];
        thisVar = listFirst(thisIndex,"=");
        //if this is the var, edit it to the value, otherwise, just append
        if(not listFind(variable,thisVar))
            string = listAppend(string,thisIndex,"&");
    }
    //return the string
    return string;
}
</cfscript>


There are number of ways to do this, here is one way using a list loop to read through your existing parameters and check for the one you want to ignore:

<cfset newParams = "" />

<cfloop list="#cgi.query_string#" delimiters="&" index="i">
    <cfif listFirst(i, "=") neq "loginID">
        <cfset newParams = listAppend(newParams, i, "&") />
    </cfif>
</cfloop>


<cflocation url="#cgi.script_name#?#newParams#" addtoken="no">

Hope that helps!


Suppose you don't really want to remove the ? to keep the URL valid, so simple regex should work:

QUERY_STRING = ReReplaceNoCase(cgi.QUERY_STRING, "LoginID=.+\&", "");

BTW, I'm not sure why do you keep LoginID in URL at all, it may be insecure approach. Using sessions sounds like a better idea.

Edit: Ben's regex is better, because my version is so simple that will "eat" all key=value pairs before last one.


Insert famous Zawinski two problem regex quote and solve differently:

<cfset copy = duplicate(url)>
<cfset structDelete(copy, "loginid")>
<cfset entries = []>
<cfloop collection="#copy#" item="key">
    <cfset arrayAppend(entries, "#key#=#copy[key]#")>
</cfloop>
<cfoutput>#arrayToList(entries, "&")#</cfoutput>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜