How do I get a parameter value from $_SERVER['HTTP_REFERER']? - php

In a PHP application, $_SERVER['HTTP_REFERER'] has the following value:
http://testing.localhost/userdashboard/test/fc
I have try this $value= striurl($_SERVER['HTTP_REFERER'], 'test');, the value I get is test/fc.
My question is what is the proper way to extract the value of "fc"?
Thanks a lot for any help.

Laravel's Request class has a function called segments() which returns an array of all segments in the url.
here it would be = to ['userdashboard', 'test', 'fc']
So with that in mind, you can grab the last piece with...
$lastSegment = last(request()->segments());

try this
echo end(explode("/",$_SERVER['HTTP_REFERER']));

The function you're looking for is basename().
$base = basename('test/fc');
echo $base; // fc
You might also want to look at parse_url(), which will extract all the elements of a URL into a nice array structure.

Related

How to get first part of a URL

can anyone help me to piece together the puzzled I'm facing. Lets say I have url's
/some-work/
/store/bread/alloy/
and in both of these cases I wanna fetch the first part from it. i.e. some-work, store.
Now I've used parse_url(get_permalink()) to get the array of the url and then fetch the path index of the array to fetch the above string. Now I have also checked strstr PHP function, but I am unable to make it work. Can anyone help?
You can use explode, array_filter and current function like as
$url = "http://www.example.com/some-work/";
$extracted = array_filter(explode("/",parse_url($url,PHP_URL_PATH)));
echo current($extracted);//some-work
Demo

Get last word from URL after equals sign

I can't find a solution for this problem. I have this URL http://examplesite.com/?skill-type=new and I want to get the very last word after the = sign, using PHP only. "skill-type" stays the same all the time Thanks in advance :)
This snippet should do:
$url = 'http://examplesite.com/?skill-type=new';
$skillType = explode('&', parse_url($url, PHP_URL_QUERY))['skill-type'];
Check parse_url for more details.
If you meant that the request comes in to that particular url, just use $_REQUEST['skill-type'] to retrieve the value.
Considering you already have the URL in a variable, let's say
$url = 'http://examplesite.com/?skill-type=new;
One possible way (certainly there are others) would be:
$urlArray = explode('=',$url);
$last = $urlArray[sizeof($urlArray)-1];
Note:
only applicable if you don't know how the URL comes, if you do, consider on using $_GET
Use the PHP $_GET Global Variable
$skill_type = $_GET['skill-type'];
PHP will automatically parse GET variables for you. To access the value of the skill-type variable, you need only use $_GET['skill-type']. For instance:
echo $_GET['skill-type'];
Will display
new
Generate your url by $_SERVER variable and then split it like this
$url = "http://examplesite.com/?skill-type=new";
$array = explode("=", $url);
$last_word = $array[1];
Use substr (url, posstr (url,'=')+1) or if this is the page being loaded $_GET ['skill-type']

How to change variables in link of foo?q=some&s=3&d=new

Consider a php script visited with URL of foo?q=some&s=3&d=new. I wonder if there is a paractical method for parsing the url to create links with new variable (within php page). For example foo?q=**another-word**&s=3&d=new or foo?q=another-word&s=**11**&d=new
I am thinking of catching the requested URL by $_SERVER['REQUEST_URI'] then parsing with regex; but this is not a good idea in practice. There should be a handy way to parse variables attached to the php script. In fact, inverse action of GET method.
The $_GET variable contains an already parsed array of the current query string. The array union operator + makes it easy to merge new values into that. http_build_query puts them back together into a query string:
echo 'foo?' . http_build_query(array('q' => 'another-word') + $_GET);
If you need more parsing of the URL to get 'foo', use parse_url on the REQUEST_URI.
What about using http_build_query? http://php.net/manual/en/function.http-build-query.php
It will allow you to build a query string from an array.
I'd use parse_str:
$query = 'q=some&s=3&d=new';
parse_str($query, $query_parsed);
$query_parsed['q'] = 'foo-bar';
$new_query = implode('&', array_map(create_function('$k, $v',
'return $k."=".urlencode($v);'),
array_keys($query_parsed), $query_parsed));
echo $new_query;
Result is:
q=foo-bar&s=3&d=new
Although, this method might look like "the hard way" :)

PHP Get From URL

I was wondering how I could remove a certain part of a url using PHP.
This is the exact url in questions:
http://www.mysite.com/link/go/370768/
I'm looking to remove the number ID into a variable.
Any help would be great, Thanks!
There are many (many!) ways of extracting a number from a string. Here's an example which assumes the URL starts with the format like http://www.mysite.com/link/go/<ID> and extracts the ID.
$url = 'http://www.mysite.com/link/go/370768/';
sscanf($url, 'http://www.mysite.com/link/go/%d', $id);
var_dump($id); // int(370768)
Use explode()
print_r(explode("/", $url));
You could use mod_rewrite inside of your .htaccess to internally rewrite this URL to something more friendly for PHP (convert /link/go/370768/ into ?link=370768, for example).
I suspect that you are using some kind of framework. There are two ways to check the $_GET variables:
print_r($_GET);
or check the manual of the manual of the framework and see how the GET/POST is passed internally, for example in CakePHP you have all parameters save internally in your controller you can access them like that:
$this->params['product_id'] or $this->params['pass']
There is another solution which is not very reliable and professional but might work:
$path = parse_url($url, PHP_URL_PATH);
$vars = explode('/', $path);
echo $vars[2];
$vars should contain array like this:
array (
0 => link
1 => go
2 => 370768
)

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