/    Sign up×
Community /Pin to ProfileBookmark

urlencode() AND raw_urlencode()

Folks,

In this tutorial:
https://medium.com/oceanize-geeks/php-urlencode-vs-rawurlencode-f6e9f049599a

It says difference between urlencode() and raw_urlencode() is that one converts space into %20 and while the other into +.
Also says to use former when encoding url query and use the latter when encoding file path or url path. But in their example, they don’t convert the query string (in the url) using the urlencode(). And so I am confused now. I mean, by “query” did they mean the “query string” ($_GET[”] param) in the url or the file name ? Because from the looks of it, it seems they are talking abut “file name” and not the “query string”. Strange!
Hence, I prefer you fine folks to show us two proper examples of how to use both these functions when it comes to encoding url path and encoding url query.

Thanks

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorApr 26.2021 — Folks,

We are spoilt for choice when to use the urlencode() and the raw_urlencode() as we won't be always dealing with one same url. For example, we might be outputting on page different urls from our mysql db or outputting on page different urls that the user just submitted on our webform.

In short, we don't know what the value of $url will be.

For our learning purpose, is there any chance you can write two lines of code (a custom function) that checks the $url value to see whether it has string chars that are best to use urlencode() over the raw_url_encode() or best to use raw_url_encode() over the url_encode() ? And then our custom function can use the appropriate function out of the two built-in functions . That way, we don't use' the wrong function, out of the two, unnecessarily.

Have you ever tried building something like this, ever ?

**Anyone else welcome to give this custom function building a go. I'm still at beginner level and so this is a bit over my head.**
Copy linkTweet thisAlerts:
@developer_webauthorApr 27.2021 — @Sempervivum

Care to chime in ?

Thanks!
Copy linkTweet thisAlerts:
@developer_webauthorMay 03.2021 — Who is experienced in php urlencode() and raw_urlencode() ?
Copy linkTweet thisAlerts:
@developer_webauthorJun 09.2021 — @tracknut

Do you mind addressing my original post, mate ?
Copy linkTweet thisAlerts:
@developer_webauthorJun 09.2021 — @NogDog

Do you mind addressing my 2nd post ?

And tell me, what do you think about the following code ? Can you spot any fundamental flaws I made when using the urlencode(), rawurlencode() and htmlentities() as I am experimenting to learn how to use these functions properly ?

The url of the page is:

**https://localhost/Templates/url_encode_Template.php?search=keyword&tbl=links&col=keyword&max=100&page=1**

<i>
</i>&lt;?php
//$_GET PARAMS:
$search = 'keyword';
$tbl = 'links';
$col = 'keyword';
$max = '100'; //Shall I quote this INT/NUMERIC or not ?
$page = '1'; //Shall I quote this INT/NUMERIC or not ?

if(is_numeric($max))
{
echo "Numeric";
}

if(is_int($page))
{
echo "Int";
}

$selfpage = basename(__FILE__,''); //Echoes: url_encode_Template.php. Does not fetch the url $_GET params.
$path = rawurlencode($selfpage);
$query_string = '?search=' .urlencode($search) .'&amp;tbl=' .urlencode($tbl) .'&amp;col=' .urlencode($col) .'&amp;max=' .intval($max) .'&amp;page=' .intval($page);
$url = $path .htmlentities($query_string); //$url value contains the Full URL With $_GET params: https://localhost/Templates/url_encode_Template.php?search=keyword&amp;tbl=links&amp;col=keyword&amp;max=100&amp;page=1
echo '&lt;a href=' .'"' .$url .'"' .'&gt;Next&lt;/a&gt;'; echo '&lt;br&gt;';
?&gt;


> Note these comments:

> $max = '100'; //Shall I quote this INT/NUMERIC or not ?

> $page = '1'; //Shall I quote this INT/NUMERIC or not ?


$max is the total links per SERP.

$page is the SERP number.

Usually, do you have these numbers as INT or NUMERIC ?
Copy linkTweet thisAlerts:
@developer_webauthorJun 09.2021 — @Sempervivum

Mate, look at this pagination code. It's a pagination like google serp.

My concern is the use of urlencode(), rawurlencode() and htmlentities() as I am experimenting to learn how to use these functions properly. Now bearing these 3 functions in mind, especially the rawurlencode(), can you tell me which WHILE loop from the 2 below I should stick to ?

<i>
</i>&lt;?php
//ERROR REPORTING FOR DEVMODE ONLY.
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
error_reporting(E_ALL);
?&gt;

&lt;?php
//PAGINATION SECTION.
$search = $_GET['search']; //Keyword(s) to search.
$col = $_GET['col']; //MySql Tbl Col to search.
$tbl = $_GET['tbl']; //MySql Tbl to search.
$max = $_GET['max']; //Max Result per page.
$page = $_GET['page']; //Serp Number.

$row_count = 100; //WHICH ONE CORRECT ? Matching rows found in this example.
$row_count = '100'; //WHICH ONE CORRECT ? Matching rows found in this example.
$total_pages = ceil($row_count/$max);
$i = '1';

//$selfpage = $_SERVER['PHP_SELF'];
$selfpage = basename(__FILE__,''); //Echoes: url_encode_Template.php. Does not fetch the url $_GET params.
$path = rawurlencode($selfpage);
$query_string_1 = '?search=' .urlencode($search) .'&amp;tbl=' .urlencode($tbl) .'&amp;col=' .urlencode($col) .'&amp;max=' .intval($max);

//WHICH WHILE LOOP IS BEST ?
while($i&lt;=$total_pages)
{
$query_string_2 = '&amp;page=' .intval($i);
$url = $path .htmlentities($query_string_1) .htmlentities($query_string_2); //Full URL With $_GET params: https://localhost/Templates/url_encode_Template.php?search=keyword&amp;tbl=links&amp;col=keyword&amp;max=100&amp;page=1

<i> </i>if($page == $i)
<i> </i>{
<i> </i> echo '&lt;a href=' .'"' .$url .'"' .'&gt;' .'&lt;b&gt;' .intval($i) .'&lt;/b&gt;' .'&lt;/a&gt;';
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> echo '&lt;a href=' .'"' .$url .'"' .'&gt;' .intval($i) .'&lt;/a&gt;';
<i> </i>}
<i> </i>$i++;
}

echo '&lt;br&gt;';
echo __LINE__; echo '&lt;br&gt;';


$i = '1';
//WHICH WHILE LOOP IS BEST ?
while($i&lt;=$total_pages)
{
$query_string_2 = '&amp;page=' .intval($i);

<i> </i>if($page == $i)
<i> </i>{
<i> </i> echo '&lt;a href=' .'"' ."$path" .htmlentities($query_string_1) .htmlentities($query_string_2) .'"' .'&gt;' .'&lt;b&gt;' .intval($i) .'&lt;/b&gt;' .'&lt;/a&gt;';
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> echo '&lt;a href=' .'"' ."$path" .htmlentities($query_string_1) .htmlentities($query_string_2) .'"' .'&gt;' .intval($i) .'&lt;/a&gt;';
<i> </i>}
<i> </i>$i++;
}


Can you spot the differences between the two WHILE loops ? The first one has $url variable while the latter doesn't. I reckon the first one is better. But I want your opinion too.

**NOTE: Anyone else welcome to give their opinions too and address this post.**
Copy linkTweet thisAlerts:
@developer_webauthorJun 25.2021 — Folks,

We are spoilt for choice when to use the urlencode() and the raw_urlencode() as we won't be always dealing with one same url. For example, we might be outputting on page different urls from our mysql db or outputting on page different urls that the user just submitted on our webform.

In short, we don't know what the value of $url will be.

For our learning purpose, is there any chance you can write two lines of code (a custom function) that checks the $url value to see whether it has string chars that are best to use urlencode() over the raw_url_encode() or best to use raw_url_encode() over the url_encode() ? And then our custom function can use the appropriate function out of the two built-in functions . That way, we don't use' the wrong function, out of the two, unnecessarily.

Have you ever tried building something like this, ever ?

**Anyone else welcome to give this custom function building a go. I'm still at beginner level and so this is a bit over my head.**
Copy linkTweet thisAlerts:
@NogDogJun 25.2021 — Or just validate that the URL they gave you works as is, and if not, tell them to fix it?
[code=php]
<?php

function valid_url($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$info = curl_getinfo($ch);
return ($info['http_code'] == 200);
}

// try it out...
$tests = [
'http://example.com/path/to/something?foo=bar',
'https://www.webdeveloper.com/'
];
foreach($tests as $url) {
echo "Trying '$url'...n";
if(valid_url($url)) {
echo " Looks good.n";
} else {
echo " Uh-oh, that didn't work.n";
}
}
[/code]

Result:
[code=text]
Trying 'http://example.com/path/to/something?foo=bar'...
Uh-oh, that didn't work.
Trying 'https://www.webdeveloper.com/'...
Looks good.
[/code]
Copy linkTweet thisAlerts:
@krovlyaproJun 28.2021 — https://profnastil-smf.ru/ugolok-ravnopolochniy-sevastopol
Copy linkTweet thisAlerts:
@developer_webauthorJul 14.2021 — @NogDog#1633368

You misunderstood me NogDog.

Anyway, I had to re-phrase myself on another thread and your code over there and Zorg's code is what I was looking for. Look:

https://www.webdeveloper.com/d/394997-how-to-analyze-url-to-auto-add-appropriate-encoding-on-appropriate-spots/10

Anyway, the code you have given in this thread should become handy for my web crawler to check if the urls extrcacted on pages for crawling are valid urls or not.

Thanks!
×

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.24,
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,
)...