How I redirect dynamic url with query string in php? [duplicate] - php

This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
Closed 6 years ago.
I need to redirect dynamic URL like this "https://test.com?user=abc#gmail.com" to "https://test.com"

If I understand correctly, this URL is coming dynamically with a query string. And you want to remove the query string portion and redirect to this modified URL.
Let's assume the dynamic URL is stored in a variable $url.
Try this:
$url = "https://test.com?user=abc#gmail.com";
$modified_url = strstr($url, "?", true); // Remove the query string, which results in https://test.com
header("location:".$modified_url); // Redirect to the modified URL.

You can store the dynamic part in a variable and use the header() function to redirect your user.
$dynamicPart = "someguy#somedomain.com";
header("Location: https://test.com/index.php?username=".$dynamicPart);
exit;

Related

How to change regular data into url query parameters in php [duplicate]

This question already has answers here:
How can I properly URL encode a string in PHP?
(6 answers)
Closed 12 months ago.
I want to create a redirect in PHP where I want to pass + sign as %2B in URL but whenever, I'm using header("Location: url ") . It is passing the + sign as +, So at the end server, after decoding it is showing a blank space instead of showing + sign.
<?php
echo $eqn=$_GET['eqn'];
$ord=$_GET['ord'];
header("Location: http://example.com/$eqn")
?>
Basically, I've created a HTML form where I am passing data to the following php page, Here I want to redirect to a url like http://example.com?i0=encoded-value-required/aa.
And here in the encoded-value-required parameter, I want to pass the + sign as %2B in the header location.
Like ADyson wrote you should use urlencode.
$eqn = urlencode($_GET['eqn']);

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)

How can I get URL (with some parameters)? [duplicate]

This question already has answers here:
Get URL query string parameters
(11 answers)
Closed 6 years ago.
I have a form on page, with url like this:
site.com/form.index.html?ref=https://login....
And I get page URL via this code:
http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]
But as result I get not full URL, just this:
site.com/form.index.html
How can I get full URL?
You want to add the query string:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]";
Additionally, you may replace the http bit with $_SERVER[REQUEST_SCHEME]:
"$_SERVER[REQUEST_SCHEME]://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]"

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.

What to parameter value from returning url in php [duplicate]

This question already has answers here:
GET URL parameter in PHP
(7 answers)
Closed 7 years ago.
I have a client url[www.mysite.com/page.aspx]
when we put this url in browser then it revert the automatically
with this url
[www.mysite.com/page.aspx?|&CC=121&DD=123&AA=323|&CC=321&DD=555&AA=000]
Now we want to fetch that parameter value in php page through php or jquery.
Kindly Help and give the best suggestions.
You can simply use GET
$cc = $_GET['CC'];
$dd = $_GET['DD'];
You can access those values using $_REQUEST or $_GET

Categories