/    Sign up×
Community /Pin to ProfileBookmark

Why rawurlencode() used on Partial Path ?

Hi,

Look at this code bit ..

[code]
echo ‘<a href=”http://example.com/department_list_script/’,
rawurlencode(‘sales and marketing/Miami’), ‘”>’;
[/code]

https://www.php.net/manual/en/function.rawurlencode.php

Puzzling why rawurlencode() is used on part of path and not full path.
This is the link that needs urlencode():
http://example.com/department_list_script/sales and marketing/Miami
My question is, why not either use rawurlencode() on full path like this:

[code]
echo ‘<a href=”http://example.com/’,
rawurlencode(‘department_list_script/sales and marketing/Miami’), ‘”>’;
[/code]

Or, like this:

[code]
echo ‘<a href=”http://example.com’,
rawurlencode(‘/department_list_script/sales and marketing/Miami’), ‘”>’;
[/code]

There are 3 DIRs and they used rawurlencode() on the last 2 DIRs only. Why ignore the first DIR: ‘department_list_script.
That is the puzzle.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@novice2022authorMay 31.2022 — Hi,

On this tutorial however, the whole path, and not partial unlike the previous post's code, got urlencoded.
<i>
</i> echo '&lt;a href="http://testdomain.com/', rawurlencode('subscribers and admins/India'), '"&gt;';


https://www.delftstack.com/howto/php/php-url-encoding/

So now, to tutorials teaching two different things. First one teaches to urlencode() partial path while last one teaches to do it to whole path.
Copy linkTweet thisAlerts:
@NogDogMay 31.2022 — They're probably doing something "RESTful", and are effectively passing that part of the "path" as what will ultimately be treated as part of the GET request data, not actually part of the path (e.g. via manipulation in a .htaccess file or such). If you're not planning to do something like that, then it does not apply to you and you can ignore it.
Copy linkTweet thisAlerts:
@novice2022authorMay 31.2022 — @NogDog#1644371

So, I should rawurlencode() the full path ? Yes ?
Copy linkTweet thisAlerts:
@NogDogMay 31.2022 — Probably not. You only need it if you actually want to url-encode some part of the URI. In normal situations you would not want to do that to the actual path -- you _want_ those literal slashes to be there in your link tag.
Copy linkTweet thisAlerts:
@novice2022authorMay 31.2022 — @NogDog#1644382

So, why the tutorial did it then to the slash if we literally need the slash ?

Check my 2nd post's tutorial code.
Copy linkTweet thisAlerts:
@NogDogMay 31.2022 — > @novice2022#1644385 So, why the tutorial did it then to the slash if we literally need the slash ?

Who knows? As I just responded in your other post, you need to figure out what _you_ are trying to do and what parts of that need to be escaped/encoded/whatever, and then use the applicable function to do so. Picking random examples from random tutorials that are just showing what those functions do, may have nothing to do with what _you_ actually need to do.
Copy linkTweet thisAlerts:
@novice2022authorJun 02.2022 — @NogDog#1644388

I will be echoing visitor submitted links on my pages. Links maybe static or dynamic. So, links may be with queries. No matter the link, must be outputted securely so no other visitors see malicious code on page. No html tags should be outputted on screen either.

Imagine how word press blogs echoing visitor submitted links on visitors' comments.

You don't want to be reading comments on my blog pages and see malicious script submitted by some user that messes up your pc or fone.

Trying to build my own word press clone. Learning to output third party submitted links securely. That is the project.
Copy linkTweet thisAlerts:
@sawer09Jun 04.2022 — yes this partial path help me a lot for my site [LESCO Online Bill](https://lescobill.com.pk/) so i heard it from a friend about that so I join it,
×

Success!

Help @novice2022 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 5.6,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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