PHP: How to check total no. parameters in URL? - php

i am retrieving the parameters using $_REQUEST. Is there a way of finding total no. of parameters in URL instead of retrieving each one and then counting ?

This will give you the total number of & separated URL query parameters:
count(explode('&', $_SERVER['QUERY_STRING']))
If you only want unique parameters, use $_GET instead:
count($_GET)

Retrieve them with $_GET. This should be enough.
Example:
// url: index.php?a=1&b=2&c=3
echo count($_GET); // 3 params, $_GET['a'], $_GET['b'], $_GET['c']
Note: you can also pass arrays in url ( check here ), and the whole array is counted once.

This will do the trick for you. Try this :
$total = count($_GET);
echo $total;

If you only want the parameters in the URL, it's better to use $_GET.
Since $_REQEUST contains the contents of $_GET, $_POST and $_COOKIE. see php.net
And if you want to know the number of parameters, you can get count the number of parameters that are in the URL using this: count($_GET).

Related

Laravel pagination: Append query parameter without value

I have a pagination-instance where I want to append all query parameter from the request to the next_page_url attribute.
I have query parameter with a value like &name=chris but I also have single parameter without a value like &xyz.
However, when I append all query parameters to the pagination instance, like so:
$query->simplePaginate(50)->appends($request->all());
only parameters with a value are getting appended.
How can I append all parameters to the next_page_url?
Update
I want to append query parameters to get the next chunk of requested data.
If I don't, it always gives back "next_page_url":"http://vue.dev/contacts?page=2". What I want is "next_page_url":"http://vue.dev/contacts?name&page=2"
Take URL http://vue.dev/contacts?page=2&name for example. Although perfectly valid, it's still quite ambiguous. Do we mean to include name? Do we mean to exclude name?
So I'd suggest you to use this URL instead http://vue.dev/contacts?page=2&select=name. If you decide to select more stuff you can just do http://vue.dev/contacts?page=2&select=name,age,gender.
Later in your code just use explode to use the value as an array:
$attributes = explode(',', $request->select);
Useful reading: http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api
Even though Fahmis solution is possible as well, I end up using the approach from this so-question. This has the advantage that php reads the parameter as an array automatically. My url end up looking like this:
http://vue.dev/contacts?page=2&select[]=xyz&select[]=abc

How to store last numbered part of current page url in php variable

My page url just like that
http://wallpapers.wapego.net/main.php?ses=SuNpjmgtjmN7&f=3153038
now i want to take a part of current url that is 3153038 and store it as php integer variable
$_GET['ses'] will give you 'SuNpjmgtjmN7' and $_GET['f'] will give you '3153038'
Check $_GET
Use a GET super global or REQUEST super global variable.
ex -
$value = $_GET['f']; or $value = $_REQUEST['f'];
$_GET super global uses for get values from the URL. $_POST super global uses for put values to the server. I hope you understand it.
Provided your calling this URL in a 'get request' the value of 'f' will be in the $_GET array.
But the best way to store this as an integer value would be to parse it through intval()
$value = intval($_GET['f']);
Best way is to do it like this..
intval($_REQUEST['f']);

server request uri trimming parameters

I am using $_SERVER["REQUEST_URI"] to get the current url. Then I am passing that url to another page via href.
echo "<a href='second.php?url=".$_SERVER["REQUEST_URI"]."'>Click here</a>";
//the url in this case is index.php?tit=most+wanted&id=23&c_id=11&ran=378834GSF844
Then on my second page when I do the below
$mc = $_GET['url'];
echo $mc;
I only get /index.php?tit=most+wanted
What happened to other three parameters? is it possible also to get rid of the slash on the front?
$_GET and $_POST are for single parameters only. $_SERVER['QUERY_STRING'] grabs the entire URL query.
Try to see the values of $_SERVER.
var_dump($_SERVER);
You can see the values and the suitable key you want.
I suggest you use $_SERVER["QUERY_STRING"] instead of $_SERVER["REQUEST_URI"].
About the results you have right now, its because of this character "&".
It acts as the delimiter and the next character from that point to the following character "=" will be the a key from $_GET variable and after that is the value.
i think these what you have right now:
$_GET["url"] = index.php?tit=most+wanted
$_GET["id"] = 23
$_GET["c_id"] = 11
$_GET["ran"] = 378834GSF844
Try using var_dump function to simply see the whole $_GET values.
var_dump($_GET);

php send and catch data in url without any variable name

passing data between pages in php have ways like $_GET, $_POST, $_REQUEST etc. But if we are to send data without any variable in php, there is a way which I seem to have forgotten.
For example , let a sample url be : www.mysite.com?123
Is it possible to catch the value 123 from the corresponding php page ?
any use of $_SERVER here?
You can get it using:
$_SERVER["QUERY_STRING"]
This url: www.mysite.com?123 will provide 123 into $_SERVER["QUERY_STRING"].
But be careful, if your url is likewww.mysite.com?123&re=789, $_SERVER["QUERY_STRING"] will be 123&re=789. It catch every thingg after the ?.
easy one, don't forget the $_GET is an array....
so a url like
www.mysite.com?123
$getArrayKeys = array_keys($_GET);
$firstValue = $getArrayKeys[0]; //work with the first param.
var_dump($getArrayKeys); //display all
OUTPUT
array(1) { [0]=> string(6) "123" }
however you're limiting yourself to a single param here... what happens when you need to pass a second param and they are not in the same order?
Try out with:
$_SERVER["QUERY_STRING"]
I believe that browsers default to HTTP GET, so the variables would be accessible by $_GET.
If you are trying to get "123", then key($_GET) should suffice. However if there are more parameters, consider looping through the keys of the $_GET array.
echo 'First $_GET variable: ' . key($_GET);
echo "All the $_GET variables:";
print_r( array_keys($_GET) );

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