my url is like below:
http://www.xyz.org/abc/list.php?id=1
now "$_SERVER['PHP_SELF']" gives me only "abc/list.php" this portion
and "$_SERVER['REQUEST_URI']" gives me "abc/list.php?id=1" this
now if i want to fetch only the portion "?id=1" how to do that.
coz im having problems in the paging query for this.
thanxx in advance...
It's $_SERVER['QUERY_STRING'].
Use $_GET['id'] to access the id parameter.
Take a look at http://php.net/parse_str, http://php.net/parse-url and http://php.net/http_build_query
Depending on what you're doing http://bradym.net/php/modify-query-string-parameters may also be helpful.
$_GET['id'] or $_REQUEST['id']
if your not sure which GET values u'll receive u also could
$fullRequest = exlode('?', $_SERVER['REQUEST_URI']);
$params = array();
foreach(explode('&', $fullRequest) as $part){
foreach(explode('=', $part) as $keyVal){
$params[$keyVal[0]] = $keyVal[1];
}
}
Related
my url is
http://localhost/helpinghand_web/home/userAutologin?tval=Eo7TIhTJqQnfysn8mwu2nXOQ0yJvY36hprJ99GJH9NBZHTh1LU&page=profile
there are two parameter tval and page how can i get it.
following are not working..
$var=$_GET['tval'];$var=$this->input->get('tval');
use this
$tval = $this->input->get('tval');
$page = $this->input->get('page');
You can use this:
$params = $this->input->get(null, true);
to get all the params passed and then use:
$params['tval']
You can use $tval = $this->input->get('tval') for get single data & $tval = $this->input->get() for get all date in singel variable in array format.
I managed the query you can use it.
If you call www.example.com?tval=1100, it will echo 1100, provided the Home
controller is routed correctly.
You can also use the http://ellislab.com/codeigniter%... to get these parameters.
For your query: $this->input->get('tval', TRUE);
i have this URL:
www.blublub.de/niedersachsen.html?veranstaltung=Neumarkt+Osnabrück~W011Cb5ETzN2EJoG9N~10.04.2014
And i like to get the id-number **"W011Cb5ETzN2EJoG9N"** from the URL.
How can i implement this in php ?
please help me.
$parts=explode('~', $_GET['veranstaltung']);
$id=$parts[1];
try this
<?php
$veranstaltung=$_GET['veranstaltung'];
$str=explode('~',$veranstaltung);
$idnumber=$str[1];
echo $idnumber;
?>
get and explode it
try to get and explode with (~) your url query string
$url = $_GET['veranstaltung'];
$myid = explode('~', $url);
echo $myid[1]; // output - W011Cb5ETzN2EJoG9N
Use regular exp for this
if (preg_match_all("/~(.*?)~/",$_GET['veranstaltung'],$result){
print_R($result)
}
I send string from cliet to server by GET method. Its a street and buils number in format like that:
Lenina, 51
On server i parse it:
$str = $_GET["street"];
$adr_array = preg_split("/[\s,]+/", $str);
$street = $adr_array[0];
$build = $adr_array[1];
if($build!=null){
...do something....
}
But what if i get only:
Lenina
Of course i get error becouse $adr_array[1] not exist.
In this case how to handle that i not got build number?
You can check if is set element in array or not:
//Your code omitted
$build = isset($adr_array[1]) ? $adr_array[1] : null;
//Your code omitted
P.S. Are you sure you want to receive street and building number like that? Maybe you want to have separate input fields? I advise doing so because it will make your code cleaner. But this is only suggestion because I don't know what your UI look likes.
You could use array_key_exists:
if(array_key_exists(1, $adr_array)){
//Street number given
}
or like so:
$build = array_key_exists(1, $adr_array) ? trim($adr_array[1]) : null;
Currently I have a url thats like this,
http://website.com/type/value
I am using
$url = $_SERVER['REQUEST_URI'];
$url = trim($url, '/');
$array = explode('/',$url);
this to get the value currently but my page has Facebook like's on it and when it is clicked it adds all these extra variables. http://website.com/type/value?fb_action_ids=1234567&fb_action_types= and that breaks that value that I am trying to get. Is there another way to get the specific value?
Assuming you know that this will always be a valid URL, you can use parse_url.
list(, $value) = explode('/', parse_url($url)['path']);
I'd use a preg_replace
explode('/', preg_replace('/?.*$/', '', $url));
You could also use:
$array = explode('/',$_SERVER['PATH_INFO']);
Or, this:
$array = explode('/',$_SERVER['PHP_SELF']);
With this, you do not need the trim() call or the temp var $url - unless you use it from something else.
The reason for two options is I don't know if /type/value is being passed to an index.php or if value is in fact a php file. Either way, one of the two options will give you what you need.
I need to be able to remove a URL from a variable, I'm wondering how i do this.
Example - Say my script returns http://www.example.com/file.php?id=1234 i need to be able to remove the http://www.example.com/file.php?id= bit, just leaving the id number. If anyone can help, it would be great :)
Something like this?
$var = 'http://www.example.com/file.php?id=1234';
$query = parse_url($var, PHP_URL_QUERY);
$query_components = parse_str($query);
$id = $query_components['id'];
You can use regular expressions:
preg_match("/id=(\\d+)/", $url, $matches);
$id = $matches[1];
Just use $id = $_GET['id'];.
See the docs.
And don't forget to validate and sanitize.
The "id" in this case is being sent to your script as a GET variable, therefore you would access it as follows:
$id = $_GET['id'];
If you mean to say that this URL is not yours to control, then you would do this instead:
print_r(parse_url($url)); // Then analyze the output.