Solved

php function

Posted on 2016-11-14
3
31 Views
Last Modified: 2016-11-15
is it correct to write a function like this?

function clean($data) {
  $data = htmlspecialchars($data);
  $data = $mysqli->real_escape_string($data);
  return $data;
}

Open in new window

0
Comment
Question by:Braveheartli
3 Comments
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41887455
In what way? The correctness of a function is two fold
1. Does it run without error
2. Is it appropriate within the context of the application

In this case there is a problem with it.

Look at line 3 - you use $mysqli - where does that come from?

You should be passing that to the function
function clean($mysqli, $data) {

Open in new window

1
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 41887565
Thank you
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41887584
Two things worth noting here.

1. Functions encapsulate "scope."  The "scope" is the set of variables that are visible inside the function.  Since $mysqli is not defined in the function code, it is not in the scope of the function.  As Julian points out, it must therefore be injected into the function.  We call this "dependency injection" because code inside the function depends on the $mysqli variable.
http://php.net/manual/en/language.variables.scope.php
https://www.experts-exchange.com/articles/18210/Software-Design-Dependencies.html
https://www.experts-exchange.com/articles/19999/PHP-Design-Avoiding-Globals-with-Dependency-Injection.html

2. Functions (like classes) should do one thing only.  In the instant case, this function mungs the $data variable twice, and in discordant ways.  The first mung changes special characters into entities.  The second mung changes the string, injecting escape sequences that remove the programmatic meaning of things like quotes and apostrophes.

The second mung, escape(), is used before sending data to the database engine.

The first mung, htmlspecialchars() is used before sending data to the client browser.

This leads me to ask, "which is it?"  If you're preparing data for use in a query string, your script is part of the data model.  But if you're preparing data for use in a browser display, your script is part of the view.  The MVC design pattern dictates isolation of the model and the view (separation of concerns), yet this function is doing two things at once, and is therefore not appropriate for either the model or the view!

If you want to get a better idea of what a clean() function should be doing, just tell us in plain language where the data is coming from and where you want to use the data.  There are fairly well-understood filter and sanitize patterns that we can discuss.
1

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question