-2

I have the following code that places a session code "3888" so that when the user brwosers to the URL without the session code, they are redirected. If they have the code in the URL, the page continues to load.

The problem is the code is changing my URL spaces to + signs when outputting the javascript variables: Example: This http://localhost/testing/testingvar.php?region=South AFrica&brand=samsung&model=S 5 changes to: http://localhost/testing/testingvar.php?region=South%20AFrica&brand=samsung&model=S%205&mine=3888

So now var

var region= getURLParameter('region');

is printed out as South+Africa, instead of South Africa.

Anyway to decode the URL to keep the spaces?

Here is the PHP code:

 <?php 

session_start();

$self = "{$_SERVER['PHP_SELF']}?{$_SERVER['QUERY_STRING']}";

if(!empty($_GET['mine']) &&  $_GET['mine'] == "3888" )  {
    unset($_GET['mine']);
    $qs = http_build_query($_GET);
    $self = "{$_SERVER['PHP_SELF']}?{$qs}";
    $_SESSION['mine'] = 1;
   header("Location: $self");
    exit;
} elseif (!isset($_SESSION['mine'])) {
    header('Location: http://redirectedpage.com');
    exit;
}

session_unset();
    session_destroy();
    session_write_close();
?>

I already have this as javascript code:

<script>
    function getURLParameter(name){return decodeURI( (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] || '' );}</script>

This doesn't seem to fix it... Could I use urldecode() in the PHP code somewhere? Without the PHP code, all my variables are intepreted perfectly.

2 Answers2

0

You can use JS's replace() function:

var region = region.replace("+", "%20");

More information at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace.

John Dvorak
  • 26,799
  • 13
  • 69
  • 83
Panda
  • 6,955
  • 6
  • 40
  • 55
  • Thank you so much for your response! The only problem is I have large string with mutliple variables in and not all are called via javascript variables. Some are outputted using code like this: Is there anyway to simply remove all "+" symbols from the URL? – spicyprinter Mar 17 '16 at 06:08
0

I found a solution:

$self = str_replace("+", " ", $self);