Pastebin


send data (safely) through URL

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
// @see original topic https://www.phphulp.nl/php/forum/topic/array-waarden-versturen-naar-andere-pagina/102027
// @see working example http://fangorn.thijma.nl/test/phphulp/robert.jansen/url.data.php
error_reporting(E_ALL);
ini_set('display_errors''stdout');
header('Content-Type: text/html; charset=UTF-8');

// make data HTML safe
function escape($in) {
    return htmlspecialchars($inENT_QUOTES'UTF-8');
}

// dump array in a HTML safe way
function dump($a) {
    echo '<pre>'.escape(print_r($atrue)).'</pre>';
}

function encodeUrlData($array) {
    return urlencode(base64_encode(serialize($array)));
}

function decodeUrlData($string) {
    return unserialize(base64_decode($string));
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>send data via URL</title>
</head>

<body><?php
// test data
$test = array(
    => 'Schönen Tag, Welt!',
    => 'Une bonne journée, tout le monde!',
    => 'يوم جيد، العالم',
    => '좋은 일, 세계!',
    => 'Một ngày tốt lành, thế giới!',
    => 'こんにちは、世界!',
);
?><a href="?test=<?php echo encodeUrlData($test?>">test</a>

<h2>Test data</h2><?php
dump($test);

?><h2>$_GET</h2><?php
dump($_GET);

?><h2>Transported data</h2><?php
if (isset($_GET['test'])) {
    dump(decodeUrlData($_GET['test']));
}
?></body>
</html>