Solved

php function

Posted on 2016-11-14
3
12 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 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
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
Comment Utility
Thank you
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now