I have an url rewrited like this : http://localhost/test/boutique/index.php/language/fr
I would know if I can take an element inside because var_dump($_GET['language']) return nothing.
the original url is http://localhost/test/boutique/index.php?language=fr (original). In this case $_GET['language'] works fine.
I need the element : fr when it's rewrited to identified the good language.
thank you.
You should get request URI with $_SERVER["REQUEST_URI"] and parse it.
Here is simple example which might help you get the idea for achieving your goal:
$temp = explode(".php", $_SERVER["REQUEST_URI"]);
// $temp[1] -> /language/fr
$url = explode("/", substr($temp[1], 1));
// $url[0] -> language , $url[1] -> fr
echo $url[1];
Are you trying to get it in CodeIgniter???
If it's in CodeIgniter then try to get the value using code.
$val = $this->segment->uri(3);
Related
I'm trying to setup a page that pulls just a part of the URL but I can't even get it to echo on my page.
Since I code in PHP, I prefer dynamic pages, so my urls usually have "index.php?page=whatever"
I need the "whatever" part only.
Can someone help me. This is what I have so far, but like I said, I can't even get it to echo.
$suburl = substr($_SERVER["HTTP_HOST"],strrpos($_SERVER["REQUEST_URI"],"/")+1);
and to echo it, I have this, of course:
echo "$suburl";
If you need to get the value of the page parameter, simply use the $_GET global variable to access the value.
$page = $_GET['page']; // output => whatever
your url is index.php?page=whatever and you want to get the whatever from it
if the part is after ? ( abc.com/xyz.php?......) , you can use $_GET['name']
for your url index.php?page=whatever
use :
$parameter= $_GET['page']; //( value of $parameter will be whatever )
for your url index.php?page=whatever&no=28
use :
$parameter1= $_GET['page']; //( value of $parameter1 will be whatever )
$parameter2= $_GET['no']; //( value of $parameter2 will be 28 )
please before using the parameters received by $_GET , please sanitize it, or you may find trouble of malicious script /code injection
for url : index.php?page=whatever&no=28
like :
if(preg_match("/^[0-9]*$/", $_GET['no'])) {
$parameter2= $_GET['no'];
}
it will check, if GET parameter no is a digit (contains 0 to 9 numbers only), then only save it in $parameter2 variable.
this is just an example, do your checking and validation as per your requirement.
You can use basic PHP function parse_url for example:
<?php
$url = 'http://site.my/index.php?page=whatever';
$query = parse_url($url, PHP_URL_QUERY);
var_dump(explode('=', $query));
Working code example here: PHPize.online
there is an external page, that passes a URL using a param value, in the querystring. to my page.
eg: page.php?URL=http://www.domain2.com?foo=bar
i tried saving the param using
$url = $_GET['url']
the problem is the reffering page does not send it encoded. and therefore it recognizes anything trailing the "&" as the beginning of a new param.
i need a way to parse the url in a way that anything trailing the second "?" is part or the passed url and not the acctual querystring.
Get the full querystring and then take out the 'URL=' part of it
$name = http_build_query($_GET);
$name = substr($name, strlen('URL='));
Antonio's answer is probably best. A less elegant way would also work:
$url = $_GET['url'];
$keys = array_keys($_GET);
$i=1;
foreach($_GET as $value) {
$url .= '&'.$keys[$i].'='.$value;
$i++;
}
echo $url;
Something like this might help:
// The full request
$request_full = $_SERVER["REQUEST_URI"];
// Position of the first "?" inside $request_full
$pos_question_mark = strpos($request_full, '?');
// Position of the query itself
$pos_query = $pos_question_mark + 1;
// Extract the malformed query from $request_full
$request_query = substr($request_full, $pos_query);
// Look for patterns that might corrupt the query
if (preg_match('/([^=]+[=])([^\&]+)([\&]+.+)?/', $request_query, $matches)) {
// If a match is found...
if (isset($_GET[$matches[1]])) {
// ... get rid of the original match...
unset($_GET[$matches[1]]);
// ... and replace it with a URL encoded version.
$_GET[$matches[1]] = urlencode($matches[2]);
}
}
As you have hinted in your question, the encoding of the URL you get is not as you want it: a & will mark a new argument for the current URL, not the one in the url parameter. If the URL were encoded correctly, the & would have been escaped as %26.
But, OK, given that you know for sure that everything following url= is not escaped and should be part of that parameter's value, you could do this:
$url = preg_replace("/^.*?([?&]url=(.*?))?$/i", "$2", $_SERVER["REQUEST_URI"]);
So if for example the current URL is:
http://www.myhost.com/page.php?a=1&URL=http://www.domain2.com?foo=bar&test=12
Then the returned value is:
http://www.domain2.com?foo=bar&test=12
See it running on eval.in.
i want to fetch youtube videos from the above script but the above code is getting keyword from GET parameter example.com/s=keyword and i want it to get from a example.com/HERE
i mean you can see there is a $_GET['s']
So this function works like this
example.com/s=keyword
and i want it to work like this
example/page/keyword
sorry for my bad english
$keyword = $_GET['s'];
file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&q=$keyword&type=video&key=abcdefg&maxResults=5");
Have a look at $_SERVER[REQUEST_URI]
This will return you the current url. Then process it using simple string or array functions to get the params, like
$current_url = $_SERVER['REQUEST_URI'];
$url_arr = explode("/", $current_url);
Then access the parameters using the array indexes
like $page = $url_arr[0];
I have difficulties in remove some slug from the url.
I have some urls like these below
http://domain1.com/so-section/upload/image1.jpg
http://domain2example.com/so-section/upload/image2.jpg
http://domain3place.com/so-section/upload/image3.jpg
http://domain4code.com/so-section/upload/image4.jpg
http://domain5action.com/so-section/upload/image5.jpg
http://domain6rack.com/so-section/upload/image5.jpg
Obviously, you will see domain are unsame, So I only would like to get "/so-section/upload/imagename.jpg" from the url. Would that be possible to remove whatever domain name come! Please help!
You can use parse_url and then get the path:
$url = parse_url("http://domain1.com/so-section/upload/image1.jpg");
echo $url["path"]; ///so-section/upload/image1.jpg
$pieces = explode('/', $url);
echo '/'. $pieces[4].'/'.$pieces[5].'/'.$pieces[6];
A simple solution to get everything after the domain name.
function GetJunk($url)
{
$start = explode("/",$url); //get everything between "/"
array_shift($start);array_shift($start);array_shift($start); //shift sections
$junk = implode("/",$start); //get everything after the new shifted sections
return $junk; // so-section/upload/image2.jpg
};
Then to use you simply:
echo GetJunk("http://domain2example.com/so-section/upload/image2.jpg");
Here is a working example: http://phpfiddle.org/main/code/4sr-zrm
Hope this helps!
I need to get the last part of the URL while using Zend Framework from a View (.phtml)
So my URL currently is something like: site.com/some/other/path
I need to return "path" -- how can I do this from a view?
Use strrpos() to find the position of the last '/' in the string, and return everything after it:
$url = 'site.com/some/other/path';
echo substr( $url, strrpos( $url, '/' ) + 1 ); // Output: 'path'
To get the URL, you can use:
basename($this->getRequest()->getRequestUri());
as stated by John Cartwright.
Either assign a view variable from the controller:
$path = $this->_request->getRequestUri();
$parts = explode('/', $path);
$lastPathComponent = end($parts);
$this->view->lastPathComponent = $lastPathComponent;
Or, if you are going to use this in a view that's used for multiple controllers (e.g. a layout), create a view helper that returns the last path component, and call it from the view:
<?=$this->escape($this->lastPathComponent())?>
You can get the url from the request object, then apply basename() to the result.
echo basename($this->getRequest()->getRequestUri());