/    Sign up×
Community /Pin to ProfileBookmark

Mystery Code – Continued

Below I have provided 2 codes. Both make use of the urlencode(). They are nearly the same code.
I notice that, only the 1st code’s output is normal and not encoded while the 2nd one’s output is encoded. Why ?
Since both are making use of the urlencode() then shouldn’t both their outputs be in encoded format ? This has been greatly puzzling me for days now. I give-up. What’s the mystery behind this ?

1st Code:

““
$url = ‘http://nogdog.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=12’;
echo prepare_url($url) . “n”;

function prepare_url($url) {
$url_parts = parse_url($url);
if($url_parts === false or empty($url_parts[‘host’])) {
return false;
}
$url_out = preg_match(‘/^https?/i’, $url_parts[‘scheme’]) ? strtolower($url_parts[‘scheme’]) : ‘https’;
$url_out .= “://{$url_parts[‘host’]}{$url_parts[‘path’]}”;
if(!empty($url_parts[‘query’])) {
parse_str($url_parts[‘query’], $query_parts);
foreach($query_parts as $q_key => $q_value) {
$query_string_parts[] = urlencode($q_key).’=’.urlencode($q_value);
}
$url_out .= ‘?’.implode(‘&’, $query_string_parts);
}
return $url_out;
}
““

2nd Code:

““
function prepare_url2($url) {
$url_parts = parse_url($url);
if($url_parts === false or empty($url_parts[‘host’])) {
return false;
}
// re-assemble the start of url string
$url_start = preg_match(‘/^https?/i’, $url_parts[‘scheme’]) ? strtolower($url_parts[‘scheme’]) : ‘https’;
$url_start .= “://{$url_parts[‘host’]}{$url_parts[‘path’]}”;
// rawurlencode the start of url string
$url_out = rawurlencode($url_start);
if(!empty($url_parts[‘query’])) {
parse_str($url_parts[‘query’], $query_parts);
foreach($query_parts as $q_key => $q_value) {
// assemble and check if value is numeric
$query_string_parts[] = urlencode($q_key).’=’.(is_numeric($q_value) ? intval($q_value) :urlencode($q_value));
}
$url_out .= ‘?’.implode(‘&’, $query_string_parts);
}

return $url_out;
}

$url = ‘http://zorg.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=13’;
echo prepare_url2($url);

““

**NOTE
The difference between the two codes are that, the 1st one defines the $url and calls the prepare_url() function at the top. (Before the prepare_url() function’s code).**

““
$url = ‘http://nogdog.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=12’;
echo prepare_url($url) . “n”;
““

While, the 2nd one defines the $url and calls the prepare_url() function at the bottom. (After the prepare_url() function’s code).

““
$url = ‘http://zorg.com/cat/subcat?var 1=value 1&var2=2&this other=thing&number is=13’;
echo prepare_url($url);
““

Apart from that, both codes are the same.
So, if both the codes are the same (so to speak), then why does the 1st code output like this:

http://nogdog.com/cat/subcat?var_1=value+1&var2=2&this_other=thing&number_is=12

And, why does the 2nd code output like this:

http%3A%2F%2Fzorg.com%2Fcat%2Fsubcat?var_1=value+1&var2=2&this_other=thing&number_is=13

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorAug 18.2021 — Folks,

This thread is an extension of this one:

https://www.webdeveloper.com/d/395870-mystery-code
Copy linkTweet thisAlerts:
@developer_webauthorAug 18.2021 — I think the issue with the 2nd code (Zorg's) is here:
<i>
</i>$url_out = rawurlencode($url_start);

Shouldn't be using rawurlencode() in the whole url.

How to fix this ? Which lines to change to what, on the 2nd code in my op ?
Copy linkTweet thisAlerts:
@ginerjmAug 18.2021 — So - why not run that line and echo out the result and see what it looks like!?
Copy linkTweet thisAlerts:
@developer_webauthorAug 20.2021 — @ginerjm#1635687

I already said, it looks like this:

http%3A%2F%2Fzorg.com%2Fcat%2Fsubcat?var_1=value+1&var2=2&this_other=thing&number_is=13

Why don;t you try my code in your localhost and see for yourself ?
Copy linkTweet thisAlerts:
@developer_webauthorSep 08.2021 — @ginerjm#1635687

You asked a question and I answered.
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...