php send and catch data in url without any variable name - php

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

Related

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: How to check total no. parameters in URL?

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

How to using get method without field name?

for example the link is:
http://www.test.com/abc.php?config.scp
Is it possible to get the value "config.scp" in the php program? Thanks!
That data is contained in $_SERVER['QUERY_STRING']
If you want a simple string, use $_SERVER['QUERY_STRING'].
If you still need an array with rest of the variables, use $_GET. If you var_dump( $_GET ) on link you provided, you should get:
array(1) {
["config_scp"]=>
string(0) ""
}
You can easily parse it now.
There's one gotcha with dot in that particular query string tho. PHP variables cannot contain dots, so it's changed into _.
Yes... in that case data is field name but be aware that dot is not allowed in $_GET array index.
Also, you can explode $_SERVER['QUERY_STRING'] with & character and look into resulting array for element's value you need.
Hy
See this example:
$url = 'http://www.test.com/abc.php?config.scp';
$p_url = end(explode('?', $url));
echo $p_url;

php variable-less argument?

just wondering. i've seen sites with this kind of url http://www.something.com/?somedata with the 'somedata' is the value of some 'unmentioned' variable
how can i do something like that? all I know is the traditional http://www.something.com/index.php?arg=somedata
thanks a lot
This is just a variable without value. You can get that string using list($value) = array_keys($_GET); - assuming you have ensured there is exactly one value in the $_GET array (count($_GET) === 1), otherwise you will get an error (if $count === 0) or unwanted behavior (if $count > 1).
You can usually access the full query string using $_SERVER["QUERY_STRING"].
I think every main-stream web server provides that variable, I'm 100% sure about Apache and pretty sure about IIS.
It is simply a shortcut for a boolean value.
somedata is equivalent to somedata=1 or somedata=true
When it's checked server side, the presence itself of the variable is enough to write a condition.
if ( isset($_GET['somedata']) ) {
//do something
}
What you see as "the value of some 'unmentioned' variable" is more generally considered as a query string parameter without a value. So for foo=bar&baz, foo has the value bar and baz has no value (in PHP its value will be an empty string).
Since the other answers are providing different methods of accessing that parameter name, here are my two cents. You can get the first key of the $_GET array by using the key function. If no key is available, key will return NULL.
Visiting ?somedata=somevalue
var_dump(key($_GET), $_GET);
/*
string(8) "somedata"
array(1) {
["somedata"]=>
string(9) "somevalue"
}
*/
Visiting ?somedata
var_dump(key($_GET), $_GET);
/*
string(8) "somedata"
array(1) {
["somedata"]=>
string(0) ""
}
*/
Visiting ?
var_dump(key($_GET), $_GET);
/*
NULL
array(0) {
}
*/
They probably use some kind of URL rewriting (see Apache mod_rewrite) where the url is transformed from http://www.something.com/?somedata to http://www.something.com/index.php?arg=somedata
For your given URL you can just iterate over the $_GET array:
foreach ($_GET as $key => $value) {
if ($key == 'somedata') {
//do something
}
}
You'll find the parameter in the keys of $_GET.
You also could extract the keys from $_GET with array_keys($_GET).
Most web servers, since the beginning of the web, allow to define a default file name to be delivered for directories. So if you load http://example.com/ it serves such file (typically index.html). In PHP enabled systems, you normally use index.php for such purposes, although the exact name can be changed. In Apache:
http://httpd.apache.org/docs/2.2/en/mod/mod_dir.html#directoryindex
As about ?somedata, it's just a variable without a value (or, more exactly, its value is an empty string). You can use it if you only need to know whether the variable is set or not:
<?php
if( isset($_GET['somedata']) ){
// Do some stuff
}
?>

Categories