1

I've been trying to resolve this for hours now and have looked through many articles here but I still can't find an answer that works.

I want users to be able to set their default currency and not have to set it on every page, so I have this session code on all my pages:

<?php
session_start();

if (isset($_GET['currency'])) {
$_SESSION['currency'] = $_GET['currency'];
}

?>

My currency selector is this:

<a href="index.php?currency=EUR">EUR</a>
<a href="index.php?currency=USD">USD</a>
<a href="index.php?currency=GBP">GBP</a>

When I display the currency, I use this:

<?php print isset($_SESSION['currency']) ? "{$_SESSION['currency']}" : 'usd'; ?>

The idea is to use USD as the default currency but this set up simply doesn't work without the ?currency= variables and when I do set the variable, it's not being saved.

What am I missing?

Thanks

Marex
  • 59
  • 7
  • @FunkFortyNiner Thanks for noticing. No, there's no space in the actual code. – Marex Aug 18 '20 at 15:01
  • 1
    I deleted my comment about it seeing the edit. Did you start the session in the 2nd file? What does error reporting show? – Funk Forty Niner Aug 18 '20 at 15:01
  • @FunkFortyNiner I use the same code on all pages, so I start the session on every page. There are no errors. – Marex Aug 18 '20 at 15:03
  • 1
    And where's the cookie/browser debugging? – mario Aug 18 '20 at 15:03
  • `` - I don't see where the session is started in that. – Funk Forty Niner Aug 18 '20 at 15:05
  • @mario It doesn't show up in cookie or session storage :[ – Marex Aug 18 '20 at 15:08
  • @FunkFortyNiner Do I have to start session whenever I want to echo the variable? Anyway, I tried with it, but it still didn't work. – Marex Aug 18 '20 at 15:11
  • I'd start by enabling error reporting for all pages, looking at the HTML source and running a `var_dump()`. There isn't much else I can do for you here, sorry. In any case, the session must be started in all pages using sessions and making sure that they are set and not empty. – Funk Forty Niner Aug 18 '20 at 15:13
  • 1
    @FunkFortyNiner Fixed it! Encoding must be UTF-8 without BOM for it to work! – Marex Aug 18 '20 at 15:26
  • Hey, right on... glad to hear it! You know, strangest thing here. I had something nagging at me behind the back of my head earlier about that, and I don't know why. Must be my instinct at work. I added another duplicate under the one that @mario used to close the question with, which I believe would be ok. I am wondering though if this is a special case. What do you think Mario? Should it be reopened or are the duplicates (and the one that I added) enough? – Funk Forty Niner Aug 18 '20 at 15:31
  • @FunkFortyNiner Looks good as is. The general duplicate seems to fit best then, since it was just a case of missing error_reporting / headers already sent. I don't feel it needs reopening or a more specific answer here. (And the initial assumption in the question title would be a bit misleading if anyone came across it.) – mario Aug 18 '20 at 17:34
  • @mario Ok Mario, will do, thanks. – Funk Forty Niner Aug 18 '20 at 17:35

0 Answers0