Is there a way to persist url parameters throughout the site as you click around? I’m working on a demo site which sets a url parameter on the first page to determine what’s shown on the following pages. I would use session variables but the internal users are likely to share links between one another and the session wouldn’t exist then. I’ve tried googling but all that returns are articles related to php frameworks like Cake. Is there a way to achieve this without using a framework?
Example of the Flow:
- Select Page > User selects Use Case and presses Submit to view Demo Site
- Inner Pages > Detect URL parameter and show data based on Use Case.
- As user clicks through the demo site the parameter persists so it shows the relevant data.
- When user returns to Select Page they can choose a new Use Case and repeat above actions.
You could simply add a url parameter with the selection, and make sure it’s always found in all internal links. I assume you don’t want to do that because it’s too many changes.
Some alternate ideas:
IDEA 1:
Save the initial selection inside the session or inside a cookie. Also add an optional url parameter for all pages, which also holds the selection. To avoid changing all of your internal links, check server-side if the selection url parameter was given. If it wasn’t, redirect immediately to the same url, just with the url parameter added (according to the session/cookie). This way the url parameter will always appear without changing all the internal links everywhere, and users can still share links.
For implementation sake, let’s assume our session variable is
$_SESSION['sel']and that our optional URL parameter ispsel. Let’s assume all pages in the site are served via php. The following lines of php should appear in the top of all pages (can be placed in a separate php file and included in each page):This is pseudo code, I’m on my iPad so excuse me if it’s a bit short and not perfect. What this line does is check if the URL param is defined, and if not, redirect (to the same page) and define it according to our session variable. This way even if we have an internal link which doesn’t include the URL param, this redirect will add it. There’s a small bug regarding
?in the request uri, I’ll let you fix it.This code does not change all of the internal links, all it does is the following: when an internal link is visited and the URL param is omitted (as it normally would), this code will stop the page from displaying without the param and redirect to the same page with the param.
Another important piece of code is to change our session variable in case the URL param was defined. This can be done by adding this else block to the code above:
This way, when the URL is shared by 2 users, it will always include the
pselURL param and when the new user first visits it, it will set his session variable accordingly.IDEA 2:
Create a “fake” directory for every selection. For example, let’s say you site was http://example.com/index.php then you’ll have http://example.com/sel1/index.php and http://example.com/sel2/index.php. These directories aren’t real, and using .htaccess rewrites (or even symbolic links in the FS) will actually point to the original files. Except, now you have this magic parameter in the path. This is useful because the path will remain the same through all relative links, meaning you don’t have to change all internal links. If you need to access the current selection, you can easily get it by parsing the path of the current url.
This idea I think is a bit better because it achieves the same thing without redirects. It will be easiest to explain with a symbolic link, but keep in mind this can also be done with .htaccess rewrite rules. A symbolic link in Linux is a path that links to another path. If I run the following console commands (usually using SSH on my server):
It will create 2 directories inside . (the local directory), named sel1 and sel2, but they are just links to the original current directory. If this current directory is my web server document root, the effect is that accessing:
Will actually access:
Note that this isn’t a copy of the directory. If I make changes to index.php or any other file in the local directory, these changes will also be reflected in
/sel1and/sel2How does this help? Instead of accessing my regular site, if I want to show selection 2 I will use the URL
http://example.com/sel2/index.php. Because of the symbolic link, this URL will behave exactly like my original websitehttp://example.com/index.php.Now let’s assume I want to check in php if I’m showing selection 2 or not, I can simply examine the variable
$_SERVER['REQUEST_URI']and see if it includes the path/sel2or not.This path will remain persistent between all internal URLs because they are usually relative.