Pass arbitrary url as $_GET variable - php

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

Related

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

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

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

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

Current page url in PHP

I've this url:
http://localhost/alignment/?page_id=52&q=2
I want to get the portion: ?page_id=52 , how can I do that?
$_SERVER['QUERY_STRING']
or
$url = "http://localhost/alignment/?page_id=52&q=2";
$bits = explode("?", $url);
$querystring = $bits[1]; // this is what you want
but the first one will be much more reliable and is easier. :)
EDIT
if you meant that you just wanted that one variable use:
$_GET['page_id']
This is called a query string. The main portion of the query string is separated by the rest of the URL with a ?. Each argument in the query string is separated by a &.
PHP:
$_SERVER['QUERY_STRING'];
If you want to get the individual pieces, use:
$_GET['page_id']; //etc...
You can get the whole query string with $_SERVER['QUERY_STRING'], but you would have to parse out page_id part. If you insist on doing things manually the function parse_str may come in handy.
A better choice would be to just use the predefined $_GET global variable. $_GET['page_id'] would give you value 52.
If you have it as a string, use parse_url. Documentation here. Get the query value of it. or if its current request use $_SERVER['QUERY_STRING']
echo parse_url($url,PHP_URL_QUERY);
This should do it:
echo $_SERVER['QUERY_STRING'];
Assign the url to string and then explode() it,
http://php.net/manual/en/function.explode.php, using the ? as a delimiter

Categories