Application.cfc

I have this in all individual pages:


<cfif Not structKeyExists( session, 'language' )>
  <cfset session.language = "russian" />
<cfelseif structKeyExists( Url, 'language' )>
  <cfset session.language = Url.language />
</cfif>

Where url.language is triggered when a different language flag is pressed! On the individual pages this is working fine but as soon as I try to add it to my Application.cfc it isn’t any longer? What is the best place within the application.cfc to make this work?

Okay! I have added it to OnRequestStart function. It is working, but i’m not sure if this is the appropriate place. ??

Blimey Donald, I can’t keep up with all your questions, lol.

I don’t see the benefit of the url variable. If they click a flag can you not just change the session variable to the new language?

Or do you have a user case of having to change it for just one page load? In which case I’d probably look to use the request scope.

URL variables can be played with by the user so it keeps everything protected server-side and gives you cleaner address.

As for where it goes, yeah onRequestStart is as good a place as any to put it.

I know I ask a lot of questions right now :rolleyes: I try to set up a my own little CMS for my regular clients and new ones coming in. As I said before there are more and more new sites I’m making requiring multiple languages. I feel I’m on the right track, and of most things, I’m actually sure they are okay since they usually work, but that doesn’t mean it is the most appropriate way and I would like to be sure, that’s all. :slight_smile:

Oh by the way this is what I have on the different flags:


?language=greek

That’s why the Url.language!

You thought about looking at Open Source CMSs instead of building your own?

Frameworks like Mura and FarCry are widely support in the community and tools like this are usually “out the box” and come with a pile of other things on top.

Saves you reinventing the wheel everytime.

I just downloaded Mura and reading into it right now as well!

Where I’m working on now is not really reinventing the wheel :lol: It’s more for me to get a way better understanding of what is possible in Coldfusion, more that basic things I have used so far. If I can in a small scale can use that as well would be a plus :smiley:

Yeah; using a pre-canned software package teaches you nothing about making a wheel, just how to put one already made onto the car. You’ll learn lots more (but spend more time) building your own.

When using URL params, make sure you always sanitize them first.

<cfparam name="url.variable" type="string" default="" />

This way, you don’t have to worry about doing isDefined checks on url.variable because it will always exist, even if the URL doesn’t have it.

But when the user puts in something like:

file.cfm?language=blah

Then even defaulting a page to:

<cfparam name="url.language" type="string" default="en" />

won’t help you. Because it will be set by the URL, but also changed to ‘blah’. A simple CFCASE check will let you compare it against acceptable values.

First of all thanks for the heads up :slight_smile:

To the second part of your post! So in other words instead of using the declaration in onRequestStart as before, it should now look something like:


<cfparam name="url.language" type="string" default="" />
  <cfif Not structKeyExists( session, 'language' )>
    <cfset session.language = "greek" />
  <cfelse>
    <cfset session.language = Url.language />
</cfif>

Does your website require that the language of the website exists with every page call? If so, I would recommend it be set once and then stored into session for the remainder of the visit (and stored on client cookie for subsequent visits)

If your site DOES require a URL variable on every page call, then having this code on the onRequestStart would probably be sufficient.