I am trying to get php to encode after = but before &.
$actual_link = "$_SERVER[REQUEST_URI]";
$out = substr(strstr($actual_link, '?'), strlen('?'));
$out = urlencode(base64_encode($out));
header('Location: http://'.$_SERVER[HTTP_HOST].'/workenv/img/create.php?'.$out.'');
^ That encodes the whole string not each one. For example:
http://www.example.com/data.php?title=hi&apple=true
Would Be:
http://www.example.com/data.php?title=URLENCODEANDBASE64&apple=URLENCODEANDBASE64`
Try this:
<?php
$actual_link = "http://www.example.com/data.php?title=hi&apple=true";
$out = substr(strstr($actual_link, '?'), strlen('?'));
$arr = explode('&', $out);
for($i=0;$i<count($arr);$i++){
$arr2 = explode('=', $arr[$i]);
$arr2[1] = urlencode(base64_encode($arr2[1]));
$arr[$i] = implode('=', $arr2);
}
$out = implode('&', $arr);
echo $out;
?>
You can easily do this with php
built-in functions:parse_url
to break down the elements of the initial url,parse_str
to break down the query string andhttp_build_query
to rebuild the query string
Example:
$url = 'http://www.example.com/data.php?title=hi&apple=true';
$parts = parse_url($url);
var_dump($parts);
/* will output
array(4) {
["scheme"]=>
string(4) "http"
["host"]=>
string(15) "www.example.com"
["path"]=>
string(9) "/data.php"
["query"]=>
string(19) "title=hi&apple=true"
}
*/
parse_str($parts["query"],$qsArray);
var_dump($qsArray);
/* will output
array(2) {
["title"]=>
string(2) "hi"
["apple"]=>
string(4) "true"
}
*/
foreach($qsArray as $key=>$value){
$encodedArray[$key]=base64_encode($value);
}
var_dump($encodedArray);
/*will output
array(2) {
["title"]=>
string(4) "aGk="
["apple"]=>
string(8) "dHJ1ZQ=="
}
*/
$encodedQS = http_build_query($encodedArray);
// http_build_query will take care of the url encoding, see below
var_dump($encodedQS);
/*will output
string(31) "title=aGk%3D&apple=dHJ1ZQ%3D%3D"
*/
$parts["query"] = "?" . $encodedQS; // prepend the "?" to the query string
$parts["scheme"] .= "://"; // append the :// bit to the scheme
$encodedURL = implode('',$parts);
var_dump($encodedURL);
/*will output
string(63) "http://www.example.com/data.php?title=aGk%3D&apple=dHJ1ZQ%3D%3D"
*/