/    Sign up×
Community /Pin to ProfileBookmark

urlencode() & decode()

Php Folks,

If someone submits a message (eg suggesting me a url/link to checkout) to me via my webform on my webpage then if I get my webform to urlencode() his message before submitting to my mysql db then to output his message on a page (so my other readers can see what he suggested to me), I need to urldecode() it before the output to page. Right ?
And, if I don’t urlencode() it during saubmission to me then no need to urldecode() it during output to page. Right ? But for safety reason, I must always urlencode() it during message submission to me. Right ?
Anything else I need to know ?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 15.2021 — No idea why you'd want to urlencode() it for database storage. The database doesn't care.

The web form will automatically urlencode any form values being submitted to your script, and PHP will automatically decode them into the applicable $_POST or $_GET array values.
Copy linkTweet thisAlerts:
@developer_webauthorMar 15.2021 — @NogDog#1629273

Mmm. Let me see if I understood you correctly or not.

If my form looks like this:

<form action="/action_page_binary.asp" method="post">
``
Then by default it is still enctyping. Right ? Meaning urlencoding or encoding all submitted data and then decoding them back before dumping the values to $_GET or $_POST and so I do not have to write like this:<i>
</i></CODE>
urldecode($_GET[url])
<CODE>
``

Correct ?
Copy linkTweet thisAlerts:
@developer_webauthorMar 15.2021 — @NogDog#1629273

Look at this pagination section.

You know when you search on google and it presents you with link on it's SERP.

You know at the bottom of the SERP you got the pagination section that links to other serp pages of the same query: Page: 123456789

Well, my pagination code looks like this. I need you to tell me whether I got the urlencode() correct or not. Or should I substitute it for urldecode() instead ? I did my best. If you don't mind then show me where I went wrong and where I should modify.


$total_pages = ceil($row_count/$limit); //$row_count is the number of rows the search query to mysql tbl yielded.
$tbl = $_GET[tbl]; //Which table to query.
$col = $_GET[col]; //Which Column to query.
$search = $_GET[search]; //The keywords to search.
$match = $_GET[match]; //Search Matching Type (Exact, Fuzzy).
$max = $_GET[max]; //Result Limit Per Whole Query.
$limit = $_GET[limit]; //Result Limit Per Page.
$page = $_GET[page]; //Page Number.

if($page>$total_page)
{
echo 'Final Page: <a href=urlencode($selfpage)' .'?tbl=' .urlencode($tbl) .'&col=' .urlencode($col) .'&search=' .urlencode($search) .'&match=' .urlencode($match) .'max=' .INTVAL($max) .'&limit=' .INTVAL($limit) .'page=' .INTVAL($total_pages) .'"><b> .INTVAL($totalpages) .'</b></a>';
}
else
{
$i = 1;
while($i<=$totalpages)
{
if($page==$page)
{
echo '<a href=urlencode($selfpage)' .'?tbl=' .urlencode($tbl) .'&col=' .urlencode($col) .'&search='
.urlencode($search) .'&match=' .urlencode($match) .'&max=' .INTVAL($max) .'&limit=' .INTVAL($limit)
.'page='.INTVAL($i) .'"><b> .INTVAL($i) .'</b></a>';
}
else
{
echo '<a href=urlencode($selfpage)' .'?tbl=' .urlencode($tbl) .'&col=' .urlencode($col) .'&search='
.urlencode($search) .'&match=' .urlencode($match) .'&max=' .INTVAL($max) .'&limit=' .INTVAL($limit)
.'page='.INTVAL($i) .'"> .INTVAL($i) .'</a>';
}
}
Copy linkTweet thisAlerts:
@developer_webauthorMar 15.2021 — @Sempervivum

@NogDog

Do you mind checking this code out ?

Can you see where I have echoed the $url ?

Should I encode the URL there, like so:
<i>
</i>echo urlencode("$url");


Or should I instead:
<i>
</i>echo urldecode($url);


<i>
</i>$query = "SELECT id,date_and_time,user_id,username,domain,domain_email,url,title,anchor,description,phrase,word FROM $tbl WHERE $col LIKE ? LIMIT $offset, $limit";
}
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt,$query);
mysqli_stmt_bind_param($stmt,'s',$search);
mysqli_stmt_execute($stmt);

//Get Result - Fetch Multiple Records.
$result = mysqli_stmt_get_result($stmt);
while($row = mysqli_fetch_assoc($result))
{
//For tracker.php.
$_SESSION['ids'][] = $id = $row['id'];
$_SESSION['dates_and_times'][] = $date_and_time = $row['date_and_time'];
$_SESSION['link_submitter_ids'][] = $link_submitter_ids = $row['link_submitter_id'];
$_SESSION['link_submitter_usernames'][] = $link_submitter_username = $row['link_submitter_username'];
$_SESSION['domains'][] = $domain = $row['domain'];
$_SESSION['domain_emails'][] = $domain_email = $row['domain_email'];
$_SESSION['urls'][] = $url = $row['url'];
$_SESSION['titles'][] = $title = $row['title'];
$_SESSION['anchors'][] = $anchor = $row['anchor'];
$link = '&lt;a href="' .'Tracker_Template.php?tbl=' .$tbl .'&amp;col=' .$col .'&amp;id=' .INTVAL($id) .'"&gt;' .$anchor .'&lt;/a&gt;';
$_SESSION['descriptions'][] = $description = $row['description'];
$_SESSION['phrases'][] = $phrase = $row['phrase'];
$_SESSION['words'][] = $word = $row['word'];

<i> </i>echo 'Id: ' .'&lt;br&gt;';
<i> </i>echo $id .'&lt;br&gt;';
<i> </i>echo 'Url: ' .'&lt;br&gt;';
<i> </i>echo $url .'&lt;br&gt;';
<i> </i>echo 'Title: ' .'&lt;br&gt;';
<i> </i>echo $title .'&lt;br&gt;';
<i> </i>echo 'Link: ' .'&lt;br&gt;';
<i> </i>echo $link .'&lt;br&gt;';
<i> </i>echo 'Description: ' .'&lt;br&gt;';
<i> </i>echo $description .'&lt;br&gt;';
<i> </i>echo 'Phrase: ' .'&lt;br&gt;';
<i> </i>echo $phrase .'&lt;br&gt;';
<i> </i>echo 'Word: ' .'&lt;br&gt;';
<i> </i>echo $word .'&lt;br&gt;';
<i> </i>echo '&lt;br&gt;&lt;br&gt;';
<i> </i>echo '&lt;br&gt;&lt;br&gt;';
}

mysqli_stmt_close($stmt);


Ok. The above is my SERP code.

It's like this. You submit via my "Link Submit" form your link and it's related keywords to my seaechengine. And when users do a keyword search related to your keywords then the above code (SERP code) will output your link and it's other details such as Title, Description, etc. Imagine Google Serp.

Now when I echo your link ($url), should I echo as is like you see in above code or should I either urlencode() it or urldecode() it ?

If I urldecode() it then must my 'Submit Link" form urlencode() it before dumping it to my MySql dB (Searchengine Index).

What I will be doing now is urlencode($url) before submission to MySql dB (via Link Submit form) and then when I echo the URL (output it in SERPs) then I will urldecode() it.

Planning in updating the above code to urldecode() before outputting in SERP page. As you can see I haven't urldecode() yet.

As for Link Submission Form, it's not built yet.

Yes or no ?
Copy linkTweet thisAlerts:
@developer_webauthorMar 15.2021 — Folks,

When you use the mysqli_stmt_bind_result() and mysqli_stmt_get_result() and echo result on page such as a url/link related to the mysql query, do you echo the url by urlencoding it or urldecoding it or don't use either. That is the big question, tonight.
Copy linkTweet thisAlerts:
@developer_webauthorAug 07.2021 — @NogDog,

Back then I didn't understand your post, I think.

https://www.webdeveloper.com/d/393206-urlencode-decode/2

But now I do. As I read about it.
×

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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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