My current address is: http://localhost/bookstore/bookedit.php?book_id=12
The $_SERVER['PHP_SELF'] variable is a string '/bookstore/bookedit.php',
But I would like to get the string 'bookedit.php?book_id=12',
Do we have any function or variable can do this?
Thanks!
If you do a var_dump($_SERVER) you will see all of the server variables you have available to you
$_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
You can use the Request URI variable.
$_SERVER['REQUEST_URI']
$data = basename($_SERVER['PHP_SELF']);
$data .= $_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] : '';
Related
I am using PHP to set the current URL as a variable using
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
This would echo the string:
http://www.example.com/landing-page-demo/
I would like to replace the 'landing-page' part of the string with 'confirmation-page' and then save this updated URL as another variable.
I was thinking of using str replace, is this the most ideal method of doing this? Not sure how to approach the problem
Indeed, short of knowing regular expressions, str_replace will do the trick.
Perform str_replace on $_SERVER['REQUEST_URI']
Example:
$url = 'http://' . $_SERVER['SERVER_NAME'] . str_replace("landing", "confirmation", $_SERVER['REQUEST_URI']);
My current solution using Str Replace:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; // Full URL var
$redirecturl = str_replace("landing","confirmation", $url);
Using str_replace is the easiest solution however you can do your logic with pathinfo($url)
$url = "http://www.example.com/landing-page-demo/";
$newURL = str_replace('landing', 'confirmation', $url);
echo $newURL;
Hello I'm currently working with php to generate a menu with a own build CMS system.
I'm making a dynamic link with : $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."/";
Than I'm adding . $row_menu['page_link'] from the database. At first it works perfect:
as example =
$row_menu['page_link'] = page2;
$url . $row_menu['page_link'];
it will return as example : http://example.com/page2
But when I click again, it adds page2 again like : http://example.com/page2/page2
How do i prevent this?
Thanks in advance!
Because at first time your $_SERVER['REQUEST_URI'] will be like http://example.com but when the user click on the link then the value of $_SERVER['REQUEST_URI'] would become http://example.com/page2.That's why it is appending two times.
Instead you can use HTTP_REFERER like
$url = $_SERVER['HTTP_REFERER'].$row_menu['page_link'];
Considering that your $_SERVER['HTTP_REFERER'] will results http://example.com.Also you can try like
$protocol = 'http';
$url = $protocol .'//'. $_SERVER['HTTP_HOST'] .'/'. $row_menu['page_link'];
REQUEST_URI will give you whatever comes after example.com, so leave that out all together.
$url = $_SERVER['HTTP_HOST'] . "/" . $row_menu['page_link'];
You can find a full list of the $_SERVER references here.
Try this:
$requested_uri = $_SERVER['REQUESTED_URI'];
$host = $_SERVER['HTTP_HOST'];
$uri_segments = explode('/',$requested_uri);
$row_menu['page_link'] = 'page2';
if($row_menu['page_link'] == $uri_segments[sizeof($uri_segments)-1]) {
array_pop($uri_segments);
}
$uri = implode('/',$uri_segments);
$url = 'http://'.$host.'/'.$uri.'/'.$row_menu['page_link'];
echo $url;
How can I get my full current url in php but minus all querystrings?
Example
echo 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
Would echo something like assuming query strings were in place...
http://www.example.com/example?tab=foo&dslip=yes
How can I get the same as above but cut off all the query strings?
How is this done in php.
Thanks.
PHP_SELF is what you need I believe.
echo('http' . ((empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off') ? '' : 's') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']);
Or the __FILE__ constant, depending on your exact configuration and situation.
Try this
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$part = explode('?',$url);
echo $part[0];
I think the solution is this:
echo 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
the $_SERVER['SCRIPT_NAME'] variable returns path of your script without the query string like you want.
if you like to see all variables available in php just try this
phpinfo();
Best regards
I use $_SERVER['QUERY_STRING'] to get the query sting.
A example would be a=123&b=456&c=789
How could I remove the b value from the query string to obtain a=123&c=789 where b can be any value of any length and is alpha numeric.
Any ideas appreciated, thanks.
A solution using url parsing:
parse_str($_SERVER['QUERY_STRING'], $result_array);
unset($result_array['b']);
$_SERVER['QUERY_STRING'] = http_build_query($result_array);
The value is going to be $_GET['b'].
How about:
str_replace('&b='.$_GET['b'], '', $_SERVER['QUERY_STRING']);
you can use this function:
function Remove_QS_Key($url, $key) {
$url = preg_replace('/(?:&|(\?))'.$key.'=[^&]*(?(1)&|)?/i', "$1", $url);
return $url;
}
to remove any key you want, e.g.
echo Remove_QS_Key("http://domain.com/?a=b&ref=dusername&c=d&e=f&g=h", "ref");
result
http://www.domain.com/?a=b&c=d&e=f&g=h
Try this:
$query_new = preg_replace('/(^|&)b=[^&]*/', '', $query);
All the answers look good, but it will be more flexible if you do:
// Make a copy of $_GET to keep the original data
$getCopy = $_GET;
unset($getCopy['b']); // or whatever var you want to take out
// This is your cleaned array
var_dump($getCopy);
// If you need the URL-encoded string, just use http_build_query()
$encodedString = http_build_query($getCopy);
You simply make a variable using $_GET and exclude b query string in build process:
$query_string_new = 'a=' . urlencode($_GET['a']) . '&c=' . urlencode($_GET['c']);
The $query_string_new should now contain a=123&c=789
Pear already has a class(Net_URL2) that handles URL parsing/building:
Install via Composer: https://packagist.org/packages/pear/net_url2
Install as include: https://github.com/pear/Net_URL2/blob/master/Net/URL2.php
Example code:
$url = new Net_URL2('http://www.example.com/?one=1');
$url->setQueryVariable('two', 2);
echo $url; // http://www.example.com/?one=1&two=2
Here is a function to replace a query parameter: (like example.com?a=1&b=2 -> example.com?a=5&b=2)
function replace_qs_key($key, $value) {
$current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") .
"://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$current_url_without_qs = strtok($current_url, '?');
parse_str($_SERVER['QUERY_STRING'], $query_params);
$query_params['page'] = $value;
$_SERVER['QUERY_STRING'] = http_build_query($query_params);
$new_url = $current_url_without_qs .'?'. $_SERVER['QUERY_STRING'];
return $new_url;
}
How can I get the full URL, like http://www.domain.com/page.php?id=someid&page=1, not just http://www.domain.com/page.php?
$_SERVER['QUERY_STRING'] has the query string portion of the URL.
A quick answer:
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'];