RESSOURCES 
 
Codage et décodage du contenu de QUERY_STRING
Domaine public 16 octobre 2001
 
 


Cet exemple (plutôt pédagogique) permet de présenter de manière plus lisible le contenu de la variable d'environnement QUERY_STRING, tel qu'il apparait dans la barre d'adresse. Attention, les fonctions fournies sont en version béta.

<?php

/*

This code is released into the public domain. That means zero support and NO
warranty of ANY kind.

I like to create and use my own session id's. But in those cases where I don't
want to use a form post, they can look ugly when displayed in the user's address
bar. So I use this code to beautify them. I call it a "barcode", because that's
kind of what it looks like in an address bar. I'm sure you could find other uses
for the code as well.

It's not setup to work on ASCII characters lower than 32 (space). If you need those,
you'll have to take out the -32 and +32 in the code. Leaving them in shortens the
barcoded string.

In the example, the barcoded string's length is 198 characters versus the 28
characters of the original string (a little over 7 to 1 for the example). You have
to keep in mind that URL's are limited to 1024 bytes. But that's still enough room
for a 32-character alphanumeric session id ('sid=zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'
produces a barcoded string length of 285 characters -- slightly less than 8 to 1).

And while this is by no means secure (it is only intended to make ugly URL query
strings less ugly), it might take someone a little longer to figure out just what
is going on, and when the underlying string _is_ found, it won't help much because
of the randomness of the session id.

Because $PATH_INFO is available for parsing with the module version of PHP, it
will look better barcoding the entire query string and placing it after a
slash, i.e. header("Location: somewhere.php/$barcodedString"). For the CGI version,
use something like header("Location: somewhere.php?bc=$barcodedValue").

===================================================================================

This set of constant definitions should work in a URL in any browser (even lynx :)

define('ZERO','i');
define('ONE','j');
define('DELIM','l'); // lowercase L

===================================================================================

But because they look better for the example given; they work in a URL for the
browsers I have (Win98 IE 5.5 and Win98 NS 4.5), and they also look a bit better
in my address bars, I'm using the following set of definitions. So only use these
at the risk of them possibly not working for all browsers. Of course, you can use
whatever set of constant definitions you want (within reason).

*/

define('ZERO','l'); // lowercase L
define('ONE','|'); // pipe symbol -- I made sure this worked using phpinfo()
define('DELIM','i');

function enBarCode($inStr){
  $bc='';
  $x=0;
  while($inStr[$x]!=''){
  
// characters in a string can be accessed like a zero-based array
    $bc.=str_replace('1',ONE,str_replace('0',ZERO,decbin(ord($inStr[$x])-32))).DELIM;
    $x++;
  }
  return substr($bc,0,-1);
}

function deBarCode($inBCStr){
  $bcArray=explode(DELIM,$inBCStr);
  $str='';
  foreach($bcArray as $bcChar){ // foreach() is php4+ only; for php3 use list...each
    $str.=chr(bindec(str_replace(ZERO,'0',str_replace(ONE,'1',$bcChar)))+32);
  }
  return $str;
}

// just copy and paste the whole code to see the following example

$originalString='sid=U9fnHW8d0kew47JqBESy53x9';

$barcodedString=enBarCode($originalString);

$decodedString=deBarCode($barcodedString);

echo "
<font face='Arial,Helvetica,sans-serif'>

originalString<br>
$originalString<br><br>

barcodedString<br>
$barcodedString<br><br>

decodedString<br>
$decodedString

</font>
";

?>

 
Accueil | Haut de page