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

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']);

Related

Should echoing a PHP $_GET variable remove + character? [duplicate]

This question already has answers here:
PHP - Plus sign with GET query
(6 answers)
Obtain $_GET value have plus ('+') Character in PHP
(4 answers)
Is it possible to preserve plus signs in PHP $_GET vars without encoding?
(7 answers)
Closed 6 months ago.
I have a URL string like https://example.com/path/?welcome_screen=1&email_address=something+else#example.com
In PHP, I call <?php echo $_GET['email_address']; ?>
Which produces something else#example.com
Specifically, a space instead of the + in the email address.
Is this expected?
If so, is there a way to prevent this from happening in the echo code above?
Or should it be handled when collecting the email address somehow?
Yes, + is one way to represent a space character in a URL. PHP automatically URL-decodes the value when it creates the $_GET data and converts it to a space, as it assumes that's what the value is supposed to represent in the raw URL.
No, it's too late by then.
Yes, you should URL-encode the value before including it in the URL, so that the + is not treated as a special character. If PHP is generating the URL, you'd use the urlencode() function. Most other programming languages have equivalent built-in functions.

How can I get value from URL as it is? [duplicate]

This question already has answers here:
Is it possible to preserve plus signs in PHP $_GET vars without encoding?
(7 answers)
Apache mod_rewrite %2B and plus (+) signs
(1 answer)
Closed 3 years ago.
How can I get the value from URL without replacing + sign with spaces?
URL: https://example.com/platform?id=xxtMEzBG56F3Z2Z+w1VT+g==
Currently, when I try to get variable in PHP by $_GET['id'] it automatically replaces + with space. How can I get the real value here?
The value I am getting is xxtMEzBG56F3Z2Z w1VT g==, what I want is xxtMEzBG56F3Z2Z+w1VT+g==
You can use $_SERVER with QUERY_STRING
echo str_replace('id=','',$_SERVER["QUERY_STRING"]);

$_GET not passing results containing value containing '#' [duplicate]

This question already has answers here:
Encoding javascript url to pass #(hashtag)
(3 answers)
How to pass parameters with hash in PHP
(4 answers)
How to pass a hash tag (#) with an address bar variable ?searchTerm=#JustKeepGoing
(1 answer)
Closed 5 years ago.
I am passing a value for eg : http://example.com/channel.php?channel=#football
Note : #football is an hashtag , # is used as a reference to hashtag
But on the channel.php page the value '#football' is not getting using the below code.
<?
if (isset($_GET['channel']) && $_GET['channel'] != "") {
$channel = $_GET['channel'];
}
?>
The # symbol ends the request part and starts the hash part of the URI, it is not sent to the server. Use encodeURIComponent to encode hash and decode it back on the back-end before processing the request. So your URI should be something like this:
"http://example.com/channel.php?channel="+encodeURIComponent("#football")
// gives
"http://example.com/channel.php?channel=%23football"

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

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;

how to pass variables in url with an ID section using PHP? [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 9 years ago.
I can do this:
home
But when I have a link with an Id, I can't pass variables:
home <--!this doesn't work of course -->
Thanks a lot!
When using PHP you can not use '#' in the URL, it will not be passed to the server.
You can use urlencode in order to encode the non-alphanumeric characters.
Use window.location.hash in javascript
<script>alert(window.location.hash);</script>
And the parse_url() function in PHP
<?php echo parse_url("home.php?var=home#sectionID",PHP_URL_FRAGMENT);?>
your HTML will be..
home
Declare a variable with $home dollar sign, i think you getting confused with JavaScript variable and PHP. To retrieve the variable values on a new page use $_GET['ID'].
Hope this helps.

Categories