I have some raw cookies . Basically blocks like this :
$block_simple = 'amsterdam=^a2p=4e52bafe90000000000.^sfLMD=1144330254^sbf=#c0000000a0000000004^cos=5^cv=15555^sin=in^js=1^dv=4e52b496^; dp1=bpcid/1907355535033e818^a1p/04e540618^fm/5.3.24e7a1bc1^kms/in52151b98^pbf/#80000000045033e818^mpc/0|34e5fe398^reg/^flagReg=1^52151b98^tzo/-3c52151b96^u1p/Y2xlbW8xMDQ35033e818^u1f/bill5033e818^idm/14e568347^; cssg=f27d4a5f1310a47ac5c0d1b0ffe1e901; nonsession=BAQAAATHFlnoQAAaAAEAACVAz6BhjbGVtbzEwNDcABAAJUDPCQWNsZW1vMTA0NwFkAAJQM gYIzIAygAgV7i2GGYyN2Q0YTVmMTMxMGE0N2FjNWMwZDFiMGZmZTFlOTAxAKoAAVAz6BgwAMsAAk5Su6A4MgFMABhQM gYNGU1MmI0OTguMC4xLjIuMTA3LjQuMC4zAU0AF1Az6Bg0ZTUyYjQ2Ny4wLjEuMy40MC4wLjAuMwAQAAlQM gYY2xlbW8xMDQ3ADMAC1Az6BhCQTIgOEpRLEdCUgDzACJQM gYJDIkbG9FSFZIa2ckejFOaDdjZTE4NWpKTENJWXpHemRrMQCaAApOU BBY2xlbW8xMDQ3aACcADhQM gYblkrc0haMlByQm1kajZ3Vm5ZK3NFWjJQckEyZGo2d0drSVdvQzVLR3FRdWRqNng5blkrc2VRPT0AnQAIUDPoGDAwMDAwMDAxM5dSPx0CaJX1ZoMRrBnZ/7dgQSM*; cid=8jLVETzyhohsSktA#190735553; npii=btrm/svid=572133042035033e6be^tguid/f27d4a5f1310a47ac5c0d1b0ffe1e9015033e6be^cguid/f27d65741310a47a26f398e3fe7999045033e6be^; lucky9=2524611; ds1=ats/1314038674493; secses=BAQAAATHFlnoQAAaAAUsAF1Az6Bg0ZTUyYjQ5OC4wLjEuMi44OS40LjAuM70LVKpcj67yqpxdXqgT56WI5Ov ; ds2=ssts/1314043029160^';
and I need to convert them in blocks like this
$block_coded = 'amsterdam=%5Ea2p%3D4e52bafe90000000000.%5EsfLMD%3D1144330254%5Esbf%3D%23c0000000a0000100004%5Ecos%3D5%5Ecv%3D15555%5Esin%3Din%5Ejs%3D1%5Edv%3D4e52b464%5E; dp1=bpcid/1907355535033e7e7^a1p/04e5405e7^fm/5.3.24e7a1bc1^kms/in52151b67^pbf/%2380000000045033e7e7^mpc/0%7C34e5fe367^reg/%5EflagReg%3D1%5E57bb58e5^tzo/-3c52151b64^u1p/Y2xlbW8xMDQ35033e7e7^idm/14e568347^u1f/bill5033e7e7^; cssg=f27d4a5f1310a47ac5c0d1b0ffe1e901; s=BAQAAATHFlnoQAAWAAAEACU5T9RJjbGVtbzEwNDcAEgAKTlQF53Rlc3RDb29raWUAAwAFTlQF5zE2Mzg0APQAIk5UBeckMiRsb0VIVkhrZyR6MU5oN2NlMTg1akpMQ0lZekd6ZGsxAWUAAk5UBecjMgFFAAhQM+fnNDM3ZWRjNzUABgABTlQF5zAAqAABTlP1EjEA+AAgTlQF52YyN2Q0YTVmMTMxMGE0N2FjNWMwZDFiMGZmZTFlOTAxAUoAGE5UBec0ZTUyYjJjOS4wLjEuMi4xMDkuNC4wLjMADAAJTlQF5zIwNzkzMjQ4NwA9AAlOVAXnY2xlbW8xMDQ3euTn0ezKbK6+M6o3TtjWa5K1jLQ*; nonsession=BAQAAATHFlnoQAAaAAEAACVAz5+djbGVtbzEwNDcABAAJUDPCQWNsZW1vMTA0NwFkAAJQM+fnIzIAqgABUDPn5zAAygAgV7i152YyN2Q0YTVmMTMxMGE0N2FjNWMwZDFiMGZmZTFlOTAxAMsAAk5Su284MQFMABhQM+fnNGU1MmI0NjcuMC4xLjIuMTA3LjQuMC4zAU0AF1Az5+c0ZTUyYjQ2Ny4wLjEuMy40MC4wLjAuMwAQAAlQM+fnY2xlbW8xMDQ3ADMAC1Az5+dCQTIgOEpRLEdCUgDzACJQM+fnJDIkbG9FSFZIa2ckejFOaDdjZTE4NWpKTENJWXpHemRrMQCaAApOU+BBY2xlbW8xMDQ3aACcADhQM+fnblkrc0haMlByQm1kajZ3Vm5ZK3NFWjJQckEyZGo2d0drSVdvQzVLR3FRdWRqNng5blkrc2VRPT0AnQAIUDPn5zAwMDAwMDAxYMkmH+tedAVimO9p45ia+VNV6Wg*; cid=8jLVETzyhohsSktA%23190735553; npii=btrm/svid%3D572133042035033e6be^tguid/f27d4a5f1310a47ac5c0d1b0ffe1e9015033e6be^cguid/f27d65741310a47a26f398e3fe7999045033e6be^; lucky9=2524611; ds1=ats/1314038674493; ns1=BAQAAATHFlnoQAAaAAKUADFAz5+cyMDc5MzI0ODcvMDtGXDKnc1RHs8+T/0pDNtDJKyP2xQ**; secses=BAQAAATHFlnoQAAaAAUsAF1Az5+c0ZTUyYjQ2Ny4wLjEuMi44OS40LjAuM9lRXjfUdgATs53TH5Qcyhx8OdqF; shs=BAQAAATHFlnoQAAaAAVUADk5b3hIyMDYyNDU1NzAwMDMsMSg9MHLlFoepc4NzCZHM8McIjWiy; ds2=ssts/1314043024564^';
I'm not even sure what encrypting/encoding method is used for the 2nd block because is quite weird. A straightforward solution is my request .
**UPDATE: arnaud576875 Provided a solution
function urlencode_cb($matches) {
return $matches[1] . rawurlencode($matches[2]);
}
$block_coded = preg_replace_callback('#(\w+?=)([^\s;]+)\s*#', 'urlencode_cb', $block_simple);
but it doesn't work for all the blocks (actually fails to 50% of them ). Here is an example where the above function is not working .
$block_simple ='amsterdam=^a2p=4e53c10790000000000.^lrtjs=1.1^sfLMD=1144330254^sbf=#c0000000a0008000004^cos=5^cv=15555^sin=in^lvmn=2|0|130519409054|180712051981|^js=1^dv=4e53c023^; dp1=bvrvi/0|0|4e60ef26^pcid/1907355535034f3a6^a1p/04e5511a6^fm/5.3.24e7a1bc1^kms/in52162726^pbf/#6000000088000000045034f3a6^mpc/0|34e60ef26^reg/^flagReg=1^52162726^tzo/-3c52162723^u1p/Y2xlbW8xMDQ35034f3a6^idm/14e568347^u1f/bill5034f3a6^; cssg=f27d4a5f1310a47ac5c0d1b0ffe1e901; nonsession=BAQAAATHFlnoQAAaAAEAACVA086ZjbGVtbzEwNDcABAAJUDPCQWNsZW1vMTA0NwFkAAJQNPOmIzIACAAcTntNJjEzMTQxMDkzMDR4MTMwNTE5NDA5MDU0eDB4Mk4AqgABUDTzpjAAygAgV7nBpmYyN2Q0YTVmMTMxMGE0N2FjNWMwZDFiMGZmZTFlOTAxAMsAA05Txy4xNDMBTAAYUDTzpjRlNTNjMDI2LjAuMS4yLjEwNy40LjAuMwFNABdQNPOmNGU1M2I3OGMuMC4xLjMuNDAuMC4wLjMAEAAJUDTzpmNsZW1vMTA0NwAzAAtQNPOmQkEyIDhKUSxHQlIA8wAiUDTzpiQyJGxvRUhWSGtnJHoxTmg3Y2UxODVqSkxDSVl6R3pkazEAmgAKTlPgQWNsZW1vMTA0N2gAnAA4UDTzpm5ZK3NIWjJQckJtZGo2d1ZuWStzRVoyUHJBMmRqNndHa0lXb0M1S0dxUXVkajZ4OW5ZK3NlUT09AJ0ACFA086YwMDAwMDAwMardeUkKMs4Os7ChwB6XUI1HBogs; cid=8jLVETzyhohsSktA#190735553; npii=btrm/svid=572133042035034eabb^cguid/f27d65741310a47a26f398e3fe7999045034eabb^tguid/f27d4a5f1310a47ac5c0d1b0ffe1e9015034eabb^; lucky9=2524611; ds1=ats/1314038674493; secses=BAQAAATHFlnoQAAaAAUsAF1A086Y0ZTUzYzAyNi4wLjEuMi44OS41LjAuMxLtD8ybEPsI/sx10s7RA5 bEe8A; ds2=ssts/1314111522013^';
$block_coded='amsterdam=%5Ea2p%3D4e53c10790000000000.%5Elrtjs%3D1.1%5EsfLMD%3D1144330254%5Esbf%3D%23c0000000a0008100004%5Ecos%3D5%5Ecv%3D15555%5Elvmn%3D2%7C0%7C130519409054%7C180712051981%7C%5Esin%3Din%5Ejs%3D1%5Edv%3D4e53bfa6%5E; dp1=bvrvi/0%7C0%7C4e60eeaa^pcid/1907355535034f32a^a1p/04e55112a^fm/5.3.24e7a1bc1^pbf/%236000000088000000045034f32a^mpc/0%7C34e60eeaa^kms/in521626aa^reg/%5EflagReg%3D1%5E57bc6427^tzo/-3c521626a6^u1p/Y2xlbW8xMDQ35034f32a^u1f/bill5034f32a^idm/14e568347^; cssg=f27d4a5f1310a47ac5c0d1b0ffe1e901; s=BAQAAATHFlnoQAAWAAAEACU5T9RJjbGVtbzEwNDcAAwAFTlURKjE2Mzg0AUUACFA08yo0MzdlZGM3NQFlAAJOVREqIzIABgABTlURKjAAqAABTlP1EjEBSgAYTlURKjRlNTNiZmFhLjAuMS4yLjEwOS41LjAuMwAMAAlOVREqMjA3OTMyNDg3AO4AF05VESowBmh0dHA6Ly93d3cuZWJheS5jb20vBwASAApOVREqdGVzdENvb2tpZQD0ACJOVREqJDIkbG9FSFZIa2ckejFOaDdjZTE4NWpKTENJWXpHemRrMQD4ACBOVREqZjI3ZDRhNWYxMzEwYTQ3YWM1YzBkMWIwZmZlMWU5MDEAPQAJTlURKmNsZW1vMTA0N5ldduI42WNQc0BpBDVJ17THzygM; nonsession=BAQAAATHFlnoQAAaAAEAACVA08ypjbGVtbzEwNDcABAAJUDPCQWNsZW1vMTA0NwFkAAJQNPMqIzIACAAcTntMqjEzMTQxMDkzMDR4MTMwNTE5NDA5MDU0eDB4Mk4AygAgV7nBKmYyN2Q0YTVmMTMxMGE0N2FjNWMwZDFiMGZmZTFlOTAxAKoAAVA08yowAMsAA05TxrIxNDIBTAAYUDTzKjRlNTNiZmFhLjAuMS4yLjEwNy40LjAuMwFNABdQNPMqNGU1M2I3OGMuMC4xLjMuNDAuMC4wLjMAEAAJUDTzKmNsZW1vMTA0NwAzAAtQNPMqQkEyIDhKUSxHQlIA8wAiUDTzKiQyJGxvRUhWSGtnJHoxTmg3Y2UxODVqSkxDSVl6R3pkazEAmgAKTlPgQWNsZW1vMTA0N2gAnAA4UDTzKm5ZK3NIWjJQckJtZGo2d1ZuWStzRVoyUHJBMmRqNndHa0lXb0M1S0dxUXVkajZ4OW5ZK3NlUT09AJ0ACFA08yowMDAwMDAwMaIuK/YCex+5DdAYEm4BqgIwoGfu; cid=8jLVETzyhohsSktA%23190735553; npii=btrm/svid%3D572133042035034eabb^cguid/f27d65741310a47a26f398e3fe7999045034eabb^tguid/f27d4a5f1310a47ac5c0d1b0ffe1e9015034eabb^; lucky9=2524611; ds1=ats/1314038674493; ns1=BAQAAATHFlnoQAAaAAKUADFA08yoyMDc5MzI0ODcvMDusUc6LAcEB+oxRkYBCNdqXjlbT6A**; secses=BAQAAATHFlnoQAAaAAUsAF1A08yo0ZTUzYmZhYS4wLjEuMi44OS41LjAuM8+VafwG8wCngDZdwt073uO7PTRH; shs=BAQAAATHFlnoQAAaAAVUADk5b3hIyMDYyNDU1NzAwMDMsMSg9MHLlFoepc4NzCZHM8McIjWiy; ds2=ssts/1314111517271^';
you can use rawurlencode or urlencode (the only difference between them is the second that encode also the "~" character)
http://php.net/manual/en/function.rawurlencode.php
http://php.net/manual/en/function.urlencode.php
I think you want to apply rawurlencode on each value. You could do this:
$cookies = preg_split('/[; ]+/', $block_coded, -1, PREG_SPLIT_NO_EMPTY);
foreach ($cookies as &$cookie) {
list($name, $value) = explode('=', $cookie, 2);
$cookie = $name.'='.rawurlencode($value);
}
$block_coded = implode('; ', $cookies);
my php cookie value encode function:
<?
function encode_cookie_value($value)
{return strtr($value,
array_combine(str_split($tmp=",; \t\r\n\013\014"),
array_map('rawurlencode', str_split($tmp))
)
);
}
setrawcookie('kk', encode_cookie_value('jk=jk?jk-/":jk;jk jk,jk'));
?>
Related
I have a php variable containing some special characters, inside a Codeigniter 3 controller:
page_url = 'search=' . $expression . '&page';
In a template, I use this variable:
In the in the browser I see the characters mentioned above in this form:
posts/search?search%3Dharum%26page=2
The = sign turns to %3D, "&" to %26.
I tried page_url = urldecode($page_url); but it does not work.
How do I keep the original characters?
Please use utf8 decode and try again
echo utf8_decode(urldecode("search%3Dharum%26page=2"));
Try this decode function.
function decode($url)
{
$special = array(
'%21' => '!', '\\' => '%5C', // so on you need to define.
);
foreach($special as $key => $value)
{
$result = str_replace($key, $value, $url);
}
return $result;
}
echo decode("search=%21");
Your problem is not that easy to reproduce. The following rextester demo produces the text in the form you request, using basically your code: http://rextester.com/YTY13099
<?php
$page=2;
$expression='harum';
$page_url = 'posts/search?search=' . $expression . '&page=' . $page;
?>
resulting in
Link
Could it be that the problem is caused by that fact that the script is part of a codeigniter controller? I do not know anything about codeigniter but I can image that further processing takes place there.
I have "/foo/bar/url/" coming straight after my domain name.
What I want is to find penultimate slash symbol in my string and replace it with slash symbol + hashtag. Like so: from / to /# (The problem is not how to get URL, but how to handle it)
How this could be achieved? What is the best practice for doing stuff like that?
At the moment I'm pretty sure that I should use str_replace();
UPD. I think preg_replace() would be suitable for my case. But then there is another problem: what should regexp look like in order to make my issue solved?
P.S. Just in a case I'm using SilverStripe framework (v3.1.12)
$url = '/foo/bar/url/';
if (false !== $last = strrpos($url, '/')) {
if (false !== $penultimate = strrpos($url, '/', $last - strlen($url) - 1)) {
$url = substr_replace($url, '/#', $penultimate, 1);
}
}
echo $url;
This will output
/foo/bar/#url/
If you want to strip the last /:
echo rtrim($url, '/'); // print /foo/bar/#url
Here is a method that would function. There are probably cleaner ways.
// Let's assume you already have $url_string populated
$url_string = "http://whatever.com/foo/bar/url/";
$url_explode = explode("\\",$url_string);
$portion_count = count($url_explode);
$affected_portion = $portion_count - 2; // Minus two because array index starts at 0 and also we want the second to last occurence
$i = 0;
$output = "";
foreach ($url_explode as $portion){
$output.=$portion;
if ($i == $affected_portion){
$output.= "#";
}
$i++;
}
$new_url = $output;
Assuming you now have
$url = $this->Link(); // e.g. /foo/bar/my-urlsegment
You can combine it like
$handledUrl = $this->ParentID
? $this->Parent()->Link() + '#' + $this->URLSegment
: $this->Link();
where $this->Parent()->Link() is e.g. /foo/bar and $this->URLSegment is my-urlsegment
$this->ParentID also checks if we have a parent page or are on the top level of SiteTree
I might be tooooo late for answering this question but I thought this might help you. You can simply use preg_replace like as
$url = '/foo/bar/url/';
echo preg_replace('~(\/)(\w+)\/$~',"$1#$2",$url);
Output:
/foo/bar/#url
In my case this solved my problem:
$url = $this->Link();
$url = rtrim($url, '/');
$url = substr_replace($url, '#', strrpos($url, '/') + 1, 0);
This problem is little complicated since i'm newbee to php encoding.
My site uses utf-8 encoding.
After a lot of tests, i found some solution. I use this kind of code:
function chr_conv($str)
{
$a=array with pattern('%CE%B2','%CE%B3','%CE%B4','%CE%B5' etc..);
$b=array with replacement characters(a,b,c,d, etc...);
return str_replace($a, $b2, $str);
}
function replace_old($str)
{
$a1 = array ('index.php','/http://' etc...);
$a2 = array with replacement characters('','' etc...);
return str_replace($a1, $a2, $str);
}
function sanitize($url)
{
$url= replace_old(replace_old($url));
$url = strtolower($url);
$url = preg_replace('/[0-9]/', '', $url);
$url = preg_replace('/[?]/', '', $url);
$url = substr($url,1);
return $url;
}
function wbz404_process404()
{
$options = wbz404_getOptions();
$urlRequest = $_SERVER['REQUEST_URI'];
$url = chr_conv($urlRequest);
$requestedURL = replace_old(replace_old($url));
$requestedURL .= wbz404_SortQuery($urlParts);
//Get URL data if it's already in our database
$redirect = wbz404_loadRedirectData($requestedURL);
echo sanitize($requestedURL);
echo "</br>";
echo $requestedURL;
echo "</br>";
}
When incoming url is:
/content.php?147-%CE%A8%CE%AC%CF%81%CE%B9-%CE%BC%CE%B5-%CF%80%CF%81%CE%AC%CF%83%CE%B1%28%CE%A7%CE%BF%CF%8D%CE%BC%CF%80%CE%BB%CE%B9%CE%BA%29";
I get:
/content.php?147-psari-me-prasa-choumplik
I want only:
/psari-me-prasa-choumplik
without the content.php?147- before URL.
BUT the most important problem is that I get ENDLESS LOOP instead of correct URL.
What am i doing wrong?
Have in mind that .htaccess solution won't work since i have a lighttpd server, not Apache.
If you need
I am assuming it's not always ?147- that you need to skip. But always after the first hyphen. In which case, before the echo add the following:
$requestedURL = substr($requestedURL, strrpos( $requestedURL , '-') +1 );
This will search for the position of the first hyphen and return that, add one so you skip the hyphen itself, and use that to cut the $requestedURL string up after the hyphen to the end of the string.
If it's always /content.php?127- then replace strrpos( $requestedURL , '-') +1 with the number 17.
If I have the url mysite.com/test.php?id=1. The id is set when the page loads and can be anything. There could also be others in there such as ?id=1&sort=new. Is there a way just to add another to the end without finding out what the others are first then building a new url? thanks.
As an alternative to Kolink's answer, I think I would utilize http_build_query(). This way, if there is nothing in the query string, you don't get an extra &. Although, it won't really make a difference at all. Kolink's answer is perfectly fine. I'm posting this mainly to introduce you to http_build_query(), as you will likely need it later:
http_build_query(array_merge($_GET, array('newvar'=>'123')))
Basically, we use http_build_query() to take everything in $_GET, and merge it with an array of any other parameters we want. In this example, I just create an array on the fly, using your example parameter. In practice, you'll likely have an array like this somewhere already.
"?".$_SERVER['QUERY_STRING']."&newvar=123";
Something like that.
Use this function: https://github.com/patrykparcheta/misc/blob/master/addQueryArgs.php
function addQueryArgs(array $args, string $url)
{
if (filter_var($url, FILTER_VALIDATE_URL)) {
$urlParts = parse_url($url);
if (isset($urlParts['query'])) {
parse_str($urlParts['query'], $urlQueryArgs);
$urlParts['query'] = http_build_query(array_merge($urlQueryArgs, $args));
$newUrl = $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'] . '?' . $urlParts['query'];
} else {
$newUrl = $url . '?' . http_build_query($args);
}
return $newUrl;
} else {
return $url;
}
}
$newUrl = addQueryArgs(array('add' => 'this', 'and' => 'this'), 'http://example.com/?have=others');
I use $_SERVER['QUERY_STRING'] to get the query sting.
A example would be a=123&b=456&c=789
How could I remove the b value from the query string to obtain a=123&c=789 where b can be any value of any length and is alpha numeric.
Any ideas appreciated, thanks.
A solution using url parsing:
parse_str($_SERVER['QUERY_STRING'], $result_array);
unset($result_array['b']);
$_SERVER['QUERY_STRING'] = http_build_query($result_array);
The value is going to be $_GET['b'].
How about:
str_replace('&b='.$_GET['b'], '', $_SERVER['QUERY_STRING']);
you can use this function:
function Remove_QS_Key($url, $key) {
$url = preg_replace('/(?:&|(\?))'.$key.'=[^&]*(?(1)&|)?/i', "$1", $url);
return $url;
}
to remove any key you want, e.g.
echo Remove_QS_Key("http://domain.com/?a=b&ref=dusername&c=d&e=f&g=h", "ref");
result
http://www.domain.com/?a=b&c=d&e=f&g=h
Try this:
$query_new = preg_replace('/(^|&)b=[^&]*/', '', $query);
All the answers look good, but it will be more flexible if you do:
// Make a copy of $_GET to keep the original data
$getCopy = $_GET;
unset($getCopy['b']); // or whatever var you want to take out
// This is your cleaned array
var_dump($getCopy);
// If you need the URL-encoded string, just use http_build_query()
$encodedString = http_build_query($getCopy);
You simply make a variable using $_GET and exclude b query string in build process:
$query_string_new = 'a=' . urlencode($_GET['a']) . '&c=' . urlencode($_GET['c']);
The $query_string_new should now contain a=123&c=789
Pear already has a class(Net_URL2) that handles URL parsing/building:
Install via Composer: https://packagist.org/packages/pear/net_url2
Install as include: https://github.com/pear/Net_URL2/blob/master/Net/URL2.php
Example code:
$url = new Net_URL2('http://www.example.com/?one=1');
$url->setQueryVariable('two', 2);
echo $url; // http://www.example.com/?one=1&two=2
Here is a function to replace a query parameter: (like example.com?a=1&b=2 -> example.com?a=5&b=2)
function replace_qs_key($key, $value) {
$current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") .
"://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$current_url_without_qs = strtok($current_url, '?');
parse_str($_SERVER['QUERY_STRING'], $query_params);
$query_params['page'] = $value;
$_SERVER['QUERY_STRING'] = http_build_query($query_params);
$new_url = $current_url_without_qs .'?'. $_SERVER['QUERY_STRING'];
return $new_url;
}