I am trying to pass id=%ADD_CODE as url params but when I am trying to get in php it is replacing %a with �. I need to keep the url param values the way it is.
Example
http://localhost/detect.php?id=%ADD_CODE%SUBID1&cid=%COOKIEID
Result when I am trying to echo key and value
id �DD_CODE%SUBID1
cid %COOKIEID
Any ideas?
Use Urlencode
localhost.com/detect.php?id=urlencode($userinput)
Make sure you are using urlencode() on your parameters:
http://ca2.php.net/urlencode
Related
I have a URL like this:
abc.com/my+string
When I get the parameter, it obviously it replaces the + with a space, so I get my string
I replaced the + in the url with %2B, then I use rawurldecode(), but the result is the same. Tried with urldecode() but I still can't get the plus sign in my variable, it's always an empty space.
Am I missing something, how do I get exactly my+string in PHP from the url abc.com/my%2Bstring ?
Thank you
In general, you don’t need to URL-decode GET parameter values manually, since PHP already does that for you, automatically. abc.com?var=my%2Bstring -> $_GET['var'] will contain my+string
The problem here was that URL rewriting was in play. As http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_b explains,
mod_rewrite has to unescape URLs before mapping them, so backreferences will be unescaped at the time they are applied.
So mod_rewrite has decoded my%2Bstring to my+string, and when you rewrite this as a query string parameter, you effectively get ?var=my+string. And when PHP applies automatic URL decoding on that value, the + will become a simple space.
The [B] flag exists to make mod_rewrite re-encode the value again.
Like this:
echo urldecode("abc.com/my%2Bstring"); // => abc.com/my+string
echo PHP_EOL;
echo rawurldecode("abc.com/my%2Bstring"); // => abc.com/my+string
Further if you want to get the actual my+string, you can utilize the powers of parse_url function which comes with PHP itself, although you have to provide a full URL into it.
Other way is just to explode the value by a / and get it like this:
$parts = explode('/', 'abc.com/my+string'); // => Array(2)
echo $parts[1] ?? 'not found'; // => string|not found
Also read the documentation on both: urldecode and rawurldecode.
Example here.
Can I pass URL as string to setrawcookie() in PHP ?
For example:
setrawcookie('Cookie', 'Owner=me&Website=http://my.website.com')
I am asking, because for some reason my cookie in the browser looks like:
Owner=Me&Website=
I assume the issue is tied with the character set.
Encode the cookie value, and decode when retrieving it back.
http://php.net/manual/en/function.htmlentities.php
Change:
setrawcookie('Cookie', 'Owner=me&Website=http://my.website.com')
To:
setrawcookie('Cookie', "Owner=me&Website=http://my.website.com")
If I have a URL like asdf.com/index.php?a=0&b=2, then using $_GET for a would be 0 and for b would be 2. However, the term I put into a single $_GET function has an ampersand in it already, like a=Steak&Cheese. Is there a way to make ampersands work without the $_GET variable thinking its job ends when the ampersand shows up (therefore not pulling the entire term)?
urlencode() it so & turns into %26.
If you need to make a query string out of some parameters, you can use http_build_query() instead and it will URL encode your parameters for you.
On the receiving end, your $_GET values will be decoded for you by PHP, so the query string a=Steak%26Cheese corresponds to $_GET = array('a' => 'Steak&Cheese').
Yes, you must URL Encode before request URL. Read this http://www.w3schools.com/TAGS/ref_urlencode.asp
Here is a previous post covering this in jquery AJAX requests, but to summarize you have to encoded the uri. This will convert the ampersand value to a ascii value.
Ampersand in GET, PHP
I asked how can I add "&" in value of one of the params
for example
www.example.com/index.php?param1=m&c¶m2=value2
It this possible ?
I need to get it by the query_str() as
param1=m&c
param2=value2
It this possible ?
Sure, but you need to url encode it:
www.example.com/index.php?param1=m%26c¶m2=value2
In PHP this could be done with the urlencode function.
As far as decoding the value is concerned, you don't need to do anything special, just read it as you are usually reading GET query string parameters:
$_GET["param1"]
Just encode the & to %26 with PHP urlencode and then use urldecode when retreiving the values
Given some arbitrary url:
view.php?viewid=blahblahblah
Is there a way I can encode a query string which has the above URL as a variable and preserves it's (view.php's) query string?
Obviously, for some URL:
obvious.php?obvid=foobarzot&old_url=view.php?viewid=blahblahblah
will not work at all, but is there a php function with which I could encode view.php so that I could pass it around?
Cheers!
urlencode the URL before adding it to the query string.
I think you're looking for urlencode()