can I add "&" as in value of one field - php

I asked how can I add "&" in value of one of the params
for example
www.example.com/index.php?param1=m&c&param2=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&param2=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

Related

Can setrawcookie() method accept URL as a value?

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")

How to send and receive "&" with $_GET and $_POST? [duplicate]

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

Replacing string with special character

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

Are there any tricks to avoid having to urlencode a url in a querystring

I'm trying to put a script together for a client that needs to be able to accept a web address in a query string without it first being urlencoded. An example would be like this:
http://foo.com/script.php?url=www.amazon.co.uk/ESET-Smart-Security-User-Year/dp/B005NPFOBM/ref=sr_1_1?s=software&ie=UTF8&qid=1341685530&sr=1-1
However, when I echo out the contents of $_GET['url'] it gives me the following:
www.amazon.co.uk/ESET-Smart-Security-User-Year/dp/B005NPFOBM/ref=sr_1_1?s=software
So basically it seems to choke on the first ampersand - i'm guessing because it thinks that its another variable.
Aside form urlencoding, are there any tricks to getting this working? I could probably POST it from a form, but this defeats the idea of the script.
For this specific use case, you should use $_SERVER['QUERY_STRING'] instead. This will give you the full query string in one go, you can then split it yourself.
In your example, PHP is assuming that the & is the delimiter for the next GET variable.
you could ask the query parameters, and add them to the URL you received. List the remaining parameters in $_GET in the proper order, and add them add the end of $_GET['url'].
$_GET['url']
+ '&ie=' + $_GET['ie']
+ '&qid=' + $_GET['qid']
+ '&sr=' + $_GET['sr']
Be careful that you might get an extra parameter url someday.
http://foo.com/script.php?url=www.amazon.co.uk/ESET-Smart-Security-User-Year/dp/B005NPFOBM/ref=sr_1_1?s=software&ie=UTF8&qid=1341685530&sr=1-1&url=http://someAmazoneStuff

Safely pass variables from one php file to another php file in the URL

This might be a basic question and I've been searching for a safe and clean way to do this. Im passing a normal string which CAN include special characters (like $ ^ % etc). How can I do this in the url? For example I have a variable called $text which In addto.php from $_GET. How do I then transfer this to more.php?
'more.php?varname='.urlencode($_GET['text']);
urlencode sounds like what you want.
(from the docs)
This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.
You can pass data through an URL, it should be in the form of key/value pairs, but you shouldn't use it to pass too much data because an URL has a limit. You also should not pass sensitive information.
A key/value pair is something like this:
key=value
If you have more then one pair, you need to separate them using the & char. Here is an example:
myScript.php?color1=blue&color2=red
The string after ? is called the Query String. With PHP you can easily access those key/value pairs using the super-global $_GET. So, in myScript.php you do:
$a = $_GET['color1'];
$b = $_GET['color2'];
Now, if you are going to create a dynamic query string, you should use urlencode() at least, so any special characters will be translated to maintain a proper URL format.
Please read the following:
http://php.net/urlencode
http://php.net/manual/en/function.http-build-query.php

Categories