PHP redirect to change session variable

0

I have to perform on a site, a functionality that allows that when you enter after the url, a / + currency, change the selected currency.

The coins are set in a PHP session variable, so I thought about doing a htaccess redirect to a URL with a file that makes the change of currency and then redirect to the url where it was initially called without the / usd .

The code I use to perform the currency conversion is as follows:

<?php
session_start(); 
if(isset($_GET['moneda'])){
  $moneda = $_GET['moneda'];
  $_SESSION['moneda'] = strtoupper($moneda);
}
?>

The problem is that I do not know how to redirect to the same place I was before. I do the redirection in .htaccess with the following line

Redirect 301 /usd /cambio_moneda.php?moneda="usd"

I do not know if this is the right way to make this type of session variables settings by the url.

    
asked by Aaron 31.07.2018 в 22:30
source

2 answers

0

You can try the redirect with a header location :

header('Location: origen.php');
    
answered by 31.07.2018 в 23:55
0

Fix it with a rewrite rule in .htaccess as follows.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^usd/(.*)$  cambio_moneda.php?moneda=usd&sitio=$1 [L]

Then from cambio_moneda.php what I do is take that site and do a reedirection towards it.

session_start();
if(isset($_GET['moneda'])){
  $sitioarederigir = "https://".$_SERVER['HTTP_HOST']."/".$_GET['sitio'];
  $moneda = $_GET['moneda'];
  $_SESSION['moneda'] = strtoupper($moneda);
  header("Location: $sitioarederigir");
 }
    
answered by 01.08.2018 в 17:56