PHP create link using already set url variables [duplicate] - php

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

Related

PHP Cut URL from some variable query [duplicate]

This question already has answers here:
Strip off specific parameter from URL's querystring
(22 answers)
Closed 4 years ago.
if I have some URL like these:
http://localhost/myfile.php?start=2018&end=2019&page=1&status=
Did anynone know how to replace any words from that link that contain page= ?
The result that I want is :
http://localhost/myfile.php?start=2018&end=2019&status=
Even if the link is like this:
http://localhost/myfile.php?start=2018&end=2019&status=&page=3
I still want the result will be without page variable and value, so it might be:
http://localhost/myfile.php?start=2018&end=2019&status=
any idea how to do this? Especially without regex (regex is my last option)
You can use parse_url(), parse_str() and http_build_query():
// Your URL
$str = 'http://localhost/myfile.php?start=2018&end=2019&page=1&status=';
// Get parts
$parts = parse_url($str);
// Get array of arguments
parse_str($parts['query'], $args);
// Remove unwanted index
unset($args['page']);
// Rebuild your URL
echo $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '?' . http_build_query($args);
// Output: http://localhost/myfile.php?start=2018&end=2019&status=
I encouraged to read documentation or to print_r() vars of this sample code for a better understanding.

How to check if ANY php parameter exists in url [duplicate]

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)

PHP replace GET-value of a variable [duplicate]

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
}

GET a URL parameter with PHP [duplicate]

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.

PHP Regular expression - forget everything after? [duplicate]

This question already has answers here:
Beautiful way to remove GET-variables with PHP?
(12 answers)
How to remove content from url after question mark. preg_match or preg_replace?
(2 answers)
Closed 8 years ago.
I have this url: http:www.blabla.com/x/x/x/x?username=testuser
I need a string to read this url, but forget everything and including the ? mark.
So it becomes this: http:www.blabla.com/x/x/x/x
The reason for this is because I am making this variable:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
And this code:
if($host == "http:www.blabla.com/x/x/x/x") {
echo "lul";
}
But right now, the URL changes depending on what user is on, and it has to execute the echo no matter what user is on.
So I read some reges and preg_match etc. and I just wanted to hear your opinions or advice. How would I accomblish this the best? thanks!
This is too trivial of a task for regex.
$host = $_SERVER['SERVER_NAME'] . explode("?", $_SERVER['REQUEST_URI'], 2)[0];
(Note: this assumes you're up-to-date, or at least using PHP 5.4, for the dereference to work without a temporary variable)
Or if you must omit the get / request section just explode ? and use $host[0]

Categories