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

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.

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 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 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.

how to properly escape data with javascript for query string for decoding in PHP [duplicate]

This question already has answers here:
Get PHP to stop replacing '.' characters in $_GET or $_POST arrays?
(13 answers)
Closed 9 years ago.
I'm trying to create a query string, which passes all form elements and the entered data via a GET request to a PHP file.
I'm using encodeURIComponent in JavaScript to encode the input field names as well as the field values.
What I'm encountering is that the field values seem to be passed well as I receive them in $GET correctly, but the field names will have the dot (.) replaced by an underscore ().
Example:
<input type="text" name="form.0.text.0" value="" />
This field.name would arrive at my PHP script as form_0_text_0 instead of form.0.text.0, while the entered text (e.g. this contains a lot of ....) would arrive just fine.
I'm using the following code as part of the query string generation:
+ encodeURIComponent(field.name) + "=" + (field.type == "checkbox" ? (field.checked) : encodeURIComponent(field.value))
Any ideas about what to do?
This is PHP “protecting you”. If you try to pass GET variables containing dots, they'll be replaced by underscores. See Get PHP to stop replacing '.' characters in $_GET or $_POST arrays?
There is a way around that “protection” if you really must use dots in your name attributes: https://stackoverflow.com/a/1939911/2397004

Categories