Artifacts Gallery Guilds Search Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining?
a Summer night - @809.43 (what is this?)
Activity rating: Four Stars Posts & Arts: 58/1k.beats Random | Recent Posts | Guild Recents
News: Love is not possession  :4u: Guild Events: Summerween Watch-a-thon

+  MelonLand Forum
|-+  Life & The Web
| |-+  ✁ ∙ Web Crafting
| | |-+  ☔︎ ∙ I need Help!
| | | |-+  Put something at end of url


« previous next »
Pages: [1] Print Embed
Author Topic: Put something at end of url  (Read 38 times)
Noah_S
Full Member ⚓︎
***
View Profile WWWArt


Noah. Earth. I make website. I.. I.. Noah. Earth.
⛺︎ My Room
iMood: Noah_S
RSS: RSS

Guild Memberships:
Artifacts:
I got robbed by Dan Q on Melonland!I met Dan Q on Melonland!A coin!EnbyAceArtist's Palette
« on: a Summer day » Embed

Choppy writing because I'm sick and brain isn't working

I'm attempting to make a template switcher.

My website is https://art-noah-s.eu/index.php

If I change the url to for example https://art-noah-s.eu/index.php?template=astroid_template_two_purple_green it will show the page with the template called astroid_template_two_purple_green

If I click on a link it goes back to my default template.

Is there a way that would automatically add for example ?template=astroid_template_two_purple_green to the end of every internal link on a page?

I don't want to manually edit the links because then I can't have them also link to ?template=astroid_template_two_pink_blue and whatever other templates I make.

Logged

perfectionist_better.gifweb_designer.gifspeed_of_time.gifbutton290.pngmy_dream_wedding.gifClippy beats AI

Artifact Swap: IT'S THE FINAL COUNTDOWNPaw printLarge watercraftGrowing ballFlappyLucky!IM GONNA BREAK PANGEA!Melonland has encountered a bugMerry ChristmasMessage BuddyMelon bite
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!
« Reply #1 on: a Summer day » Embed

I'd say... you're solving the problem wrong!

When the user changes theme, you probably want to store it in a cookie (or at least a session cookie). That way, you don't need to modify every link going forward. Also: it'll work even if they re-type the URL of your homepage, etc.

Here's how I'd do that. I'd add some PHP code to the top of your index.php that looks a bit like this:


Code
<?php
// Use sessions so we've got easy session cookies:
sesion_start();

// A list of valid themes so we can ignore invalid requests:
$valid_themes = ['default_theme', 'astroid_template_two_purple_green'];

// If the user has requested a theme change, apply it:
if(
  isset($_GET['theme']) &&                // must have asked for a theme change
  in_array($_GET['theme'], $valid_themes) // requested theme must be in the valid list
) {
  $_SESSION['theme'] = $_GET['theme'];    // write it to the session cookie!
}

// Get the current theme: either from the session cookie if it's set,
// or else use the default one (which we'll say is whichever one is first
// in the $valid_themes list above!):
$theme = isset($_SESSION['theme']) ? $_SESSION['theme'] : $valid_themes[0];

// Now go on and write the rest of your page...

If you're really determined to modify links, then it can be done, but it's harder. You either have to manually edit every link to include the current theme if set, or you need to use an output buffer (ob_start) to capture all the page content, use a DOMParser to find the (internal) links, and modify them before flushing the buffer. Much harder than my proposed solution, and not as feature-rich.

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif

Artifact Swap: I met Dan Q on Melonland!Polyamorousradio polyDoctor RedactedJoined 2025!
Noah_S
Full Member ⚓︎
***
View Profile WWWArt


Noah. Earth. I make website. I.. I.. Noah. Earth.
⛺︎ My Room
iMood: Noah_S
RSS: RSS

Guild Memberships:
Artifacts:
I got robbed by Dan Q on Melonland!I met Dan Q on Melonland!A coin!EnbyAceArtist's Palette
« Reply #2 on: a Summer day » Embed

Does the ?template=astroid_template_two_purple_green mean that I've successfully used $_GET? I tried a lot last summer, but I couldn't do it.

Logged

perfectionist_better.gifweb_designer.gifspeed_of_time.gifbutton290.pngmy_dream_wedding.gifClippy beats AI

Artifact Swap: IT'S THE FINAL COUNTDOWNPaw printLarge watercraftGrowing ballFlappyLucky!IM GONNA BREAK PANGEA!Melonland has encountered a bugMerry ChristmasMessage BuddyMelon bite
Noah_S
Full Member ⚓︎
***
View Profile WWWArt


Noah. Earth. I make website. I.. I.. Noah. Earth.
⛺︎ My Room
iMood: Noah_S
RSS: RSS

Guild Memberships:
Artifacts:
I got robbed by Dan Q on Melonland!I met Dan Q on Melonland!A coin!EnbyAceArtist's Palette
« Reply #3 on: a Summer day » Embed

So far my code is

Code
<form>
  <label for="template">Choose a template:</label>
  <select name="template" id="template">
    <option value="astroid_template_two_purple_green">Purple and green (dark)</option>
    <option value="astroid_template_two_pink_blue">Pink and blue (light)</option>
  </select>
<input type="submit" value="Choose">
</form>

Logged

perfectionist_better.gifweb_designer.gifspeed_of_time.gifbutton290.pngmy_dream_wedding.gifClippy beats AI

Artifact Swap: IT'S THE FINAL COUNTDOWNPaw printLarge watercraftGrowing ballFlappyLucky!IM GONNA BREAK PANGEA!Melonland has encountered a bugMerry ChristmasMessage BuddyMelon bite
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!
« Reply #4 on: a Summer day » Embed

Does the ?template=astroid_template_two_purple_green mean that I've successfully used $_GET? I tried a lot last summer, but I couldn't do it.

Yes. In HTTP, GET requests have their parameters on the end of the URL ?param1=value1&param2=value2. This part of a URL (the ? and everything after it) is called the query string. POST requests can put parameters in the URL in the same way, but typically hide them away in the "body" of the request (they can also do a mixture of both).

In PHP, you can access parameters from the query string by using $_GET: so with the example above, the variable $_GET['param1'] would be 'value1' and the variable $_GET['param2'] would be 'value2'. Note that users can put anything they like in parameters so you can't trust them: that's why my suggested PHP code checked that the requested template was in a list of pre-approved templates before it did anything with them. It probably wouldn't have hurt not to (the user could just end up selecting an invalid theme!), but it's good practice to validate user input!

(In PHP, POST body parameters go into $_POST; you also get $_REQUEST, which contains POST parameters if they're present, failing that GET parameters, so it's useful if you want to support both for some reason.)

Anyway, that code all looks good: my PHP and your HTML. The only thing remaining is to make sure that the selected theme gets loaded. Based on my PHP code, that's probably as simple as doing something like this in the right place:


Code
<link rel="stylesheet" href="/stylesheets/<?php echo $theme; ?>.css" />

Depending on which theme the user last selected, this would render as one of these two options:

Code
<link rel="stylesheet" href="/stylesheets/default_theme.css" />
<link rel="stylesheet" href="/stylesheets/dastroid_template_two_purple_green.css" />

I don't know if it interests you, but a long while ago I started writing, but didn't finish, a PHP Cookery Book geared towards smolweb users. There might be something in there that helps, too!

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif

Artifact Swap: I met Dan Q on Melonland!Polyamorousradio polyDoctor RedactedJoined 2025!
Pages: [1] Print Embed 
« previous next »
 

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021 | Privacy Notice | Send Feedback | Supporters ♥ Forum Guide | Rules | RSS | WAP | Mobile


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi
MelonLand @000

Minecraft: Online
Join: craft.melonking.net