/    Sign up×
Community /Pin to ProfileBookmark

striptags() Useless after htmlspecialchars()

Folks,

Is not this following code taught in tutorials ridiculous ?

“`
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data); //Strips only Backward Slashes. Not Forward Slashes.

**$data = htmlspecialchars($data);
$data = strip_tags($data);**

return $data;
}
“`

Note the 2 **asterisked** lines. In my opinion, either one line from the two bold lines should exist and not both.
Why ? Here’s why …

If the input is this:

[code]
$input_3 = ‘<a href=”http://www.url.com/index.php”>Link</a>’;
[/code]

Then this line is ignored:

[code]
$data = strip_tags($data);
[/code]

As the output is this:
**<a href=”http://www.url.com/index.php”>Link</a>**

If striptags() is ignored then what’s the use of having it after the line:

[code]
$data = htmlspecialchars($data);
[/code]

See my point ?

And so, the code must be either of the two:

[code]
<?php

function test_input($data)
{
$data = trim($data);
$data = stripslashes($data); //Strips only Backward Slashes. Not Forward Slashes.

$data = htmlspecialchars($data);

return $data;
}

$input_3 = ‘<a href=”http://www.url.com/index.php”>Link</a>’;

echo test_input($input_3);

?>
[/code]

[code]
<?php

function test_input($data)
{
$data = trim($data);
$data = stripslashes($data); //Strips only Backward Slashes. Not Forward Slashes.

$data = striptags($data);

return $data;
}

$input_3 = ‘<a href=”http://www.url.com/index.php”>Link</a>’;

echo test_input($input_3);

?>
[/code]

But not both, like this:

[code]
<?php

function test_input($data)
{
$data = trim($data);
$data = stripslashes($data); //Strips only Backward Slashes. Not Forward Slashes.

$data = htmlspecialchars($data);
$data = striptags($data);

return $data;
}

$input_2 = ‘http://www.url.com/index.php’;
$input_3 = ‘<a href=”http://www.url.com/index.php”>Link</a>’;

echo test_input($input_3);

?>
[/code]

Yes or no ?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 01.2021 — First you have to ask and answer the question: what are you trying to accomplish, and why? In other words:

  • - What is the source of the input?

  • - What do you plan to do with the input?

  • - What data transformation is necessary to accomplish that (and why do you think that transformation is necessary)?


  • Simply copying somebody's function from wherever you found it is worthless if it does not address whatever it is that you want/need to accomplish.
    Copy linkTweet thisAlerts:
    @developer_webauthorJul 05.2021 — @NogDog#1633604

    Ok.

    My webform will ask for user's:

    Name

    Age

    Date of Birth

    Url

    Domain

    Email

    Phone

    Mobile

    Fax

    Snail Mail Address

    Zip

    Now you know what the user inputs will be. So, what is your answer now to my previous posts ?
    Copy linkTweet thisAlerts:
    @NogDogJul 05.2021 — > @developer_web#1633735 Now you know what the user inputs will be.

    That's not what I asked.

    > @NogDog#1633604
    >
  • * What is the source of the input? [This is all your responded to]

  • >
  • * What do you plan to do with the input?

  • >
  • * What data transformation is necessary to accomplish that (and why do you think that transformation is necessary)?
  • Copy linkTweet thisAlerts:
    @developer_webauthorJul 18.2021 — @NogDog#1633743

    I understand what you stated.

    You may close this thread.
    Copy linkTweet thisAlerts:
    @NogDogJul 18.2021 — {"locked":true}
    ×

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