I have several pages that access my data-base directly and get values from a table.
Now depending on the users Geo location, we want to return a different value.
I have a PHP script that gets the geo location, but I am trying to figure out a way to avoid tracking down every page that accesses the data-base to add this logic.
I realize if the site was using an MVC architecture I could just add the logic to the model. Unfortunately that is not the case.
So my question is, is my only option to add this logic to every page that accesses that data from the DB. Or is there a better way I am unaware of?
P.S. My question title probably isn’t the best description of my problem but I was unsure how to word it.
You need not add the detection logic to every page that accesses the data, but to a common page, say index page. Are you having a common page?
Like webbiedave has said, you will have to apply some changes to your query to alter the data.
I have implemented user location based data display in one project. We had to display a list of online classes that will be taken by some tutors. The list of classes is displayed depending on the browser’s/registered user’s geo-location (country). User region (inside a country I guess) will not change the basic logic for doing this. We also did not use any framework, so I think this may be useful for you:-
This is roughly what we did:-
1. Geo location manipulation part –
We have an
indexfile (like a front controller of a framework) which is run for every request (for every module) which has all the user geo-location manipulation related code.Following are the main points about that manipulation code-
– we used a php library to do the detection
– Detect the user location (country in our case) and store it in session.
– Do not detect again/everytime if already stored in session.
– clear stored geo location on user logout and detect again.
2. Location dependent condition to your queries
– Now you just have to add the detected location to your query in one place, put it inside a function, add a location parameter to it and call it from your module. Reuse the function as much as possible.