This question already has an answer here:
Change single variable value in querystring [closed]
(1 answer)
Closed 6 years ago.
I have a URL as a string in $url.
I want to replace a specific parameter (if it exists) in the URL.
For example
$url = "http://www.xxx.xxx?data=1234324&id=abc&user=walter";
I'd like check if id exists and if it does, I want to replace the value of that id to a specific value. But the value of the id isn't always the same and it's not always in the same place.
You can extract your query with PHP's parse_url function:
$b = parse_url($url, PHP_URL_QUERY);
From here you can use parse_str to get an associative array:
parse_str($b, $arr);
Now you can access the parameters
$arr['data'];
$arr['id'];
$arr['user'];
If you want to check if the id parameter exists you can use
if (isset($arr['id'])) {
//Do something
}
Related
This question already has answers here:
PHP check if url parameter exists
(6 answers)
PHP to check if a URL contains a query string
(4 answers)
Closed 5 years ago.
I am making a forum that accesses threads based off the category in the URL using the GET method. I want to redirect to an error page if no parameters exist in the url, but I want this to be a generic piece of code that can be used around my whole site.
For example:
The url would normally contain the category id:
localhost/myforum/threads.php?categoryid=1
I want it so that when the url is:
localhost/myforum/threads.php
it is to redirect to an error page, and that this piece of code is usable all around the website
The most reliable way is to check if the URL contains a question mark:
if (false !== strpos($_SERVER['REQUEST_URI'], '?')) {
// There is a query string (including cases when it's empty)
}
Try:
$gets = parse_url($url));
if($gets['query'] == "")
{
echo "No GET variables";
}
Just:
if (empty(array_diff($_GET, ['']))) {
header("Location: /path/to/error.php");
}
EDIT: Updated to remove empty values
You can use is_set to check if the parameter exists like this,
isset($_GET)
This question already has answers here:
PHP to check if a URL contains a query string
(4 answers)
Closed 3 years ago.
i want to create a link like the below:
<a href="'.$_SERVER["REQUEST_URI"].'?&action=approve&holiday='.$result["sequence"].'">
the $_SERVER["REQUEST_URI"] includes any $_GET variables already set, but i am not sure whether to put a ? or & after this in the href because $_SERVER["REQUEST_URI"] could already include a $_GET variable therefore it would need & and not a ?
Check if it includes '?' or not.
$extra = 'action=approve&holiday='.$result["sequence"];
$glue = (strpos($_SERVER["REQUEST_URI"], '?') === false) '?' : '&';
Then, you can use this:
echo '<a href="'.$_SERVER["REQUEST_URI"]. $glue . extra .'">';
But, if you don't need the current passed parameters in URL, you can use the way #Utkanos said
You need to build the URL in parts. All of the data you need is contained in the $_SERVER superglobal.
$_SERVER['REQUEST_SCHEMA'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'//...
PHP_SELF denotes the URI beyond the hostname, e.g. "/foo/bar.htm" in "mydomain.com/foo/bar.htm"
http://php.net/manual/en/reserved.variables.server.php
This question already has answers here:
GET URL parameter in PHP
(7 answers)
Get the full URL in PHP
(27 answers)
Closed 6 years ago.
I would like to capture the lang variable and then translate the page based on this variable.
global $jwp_lang;
$url = $_SERVER["REQUEST_URI"];
echo $url;
for example if the url contains http://localhost/about/?lang=fr I would like to capture this value.
You can easily capture the value of lang variable using php Super Global variable $_GET :
$lang = $_GET['lang'];
echo $lang;
It is better to pass the URL parameters using add_query_var, and get the parameter using get_query_var.
Because, they can handdle the set, and get of multiple parameters, and is the recommended way of getting URL passed as parameters.
This question already has answers here:
How do I extract query parameters from a URL string in PHP?
(3 answers)
Closed 8 years ago.
This would be an example:
redirect
dynamic_word can be changed because it is dynamic. When click "redirect", dynamic_word will be extracted. So, how to extract it in redirect.php file ? Thanks !
Use $_GET to get parameters from an URL
<?php
$thatName = $_GET['q'];
echo $thatName;
Result
dynamic_word
If samitha's correct looking answer is incorrect then perhaps you mean you would like to extract the dynamic word from a string.
In that case you could do
<?php
$string = 'http://mywebsite.com/redirect.php&q=dynamic_word';
$ex_stirng = explode('&q=', $string);
$dynamic_word = $ex_string(1);
?>
Or even use the strstr function:
http://www.php.net/manual/en/function.strstr.php
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Construct a PHP variable name based on other variable values and static text
$value = '200';
$_200 = 'other';
How could I echo the contents of the second variable, getting its name from the first variable? So basically read the value of $value, prepend an _ and use it as a variable name.
Same as always.
echo ${'_'.$value};