How to repeat the same PHP snippet in different PHP files?
For example I define the variable $user, then I insert PHP code, which have an access to this variable.
Update:
I’m going to use this with if statement
Example(without if statement):
function sanitizeString($var)
{
$var = htmlentities($var, ENT_QUOTES, "UTF-8");
return mysql_real_escape_string($var);
}
$user = sanitizeString($_POST['user']);
$pass = sanitizeString($_POST['pass']);
//code i want to repeat in different files
$user=$user;
$pass=$pass;
#salt generation
$salt=uniqid(mt_rand(), true);
#Add data to tables
queryMysql("INSERT INTO accounts VALUES('$user', '".hash('sha512',$pass+$salt)."', '$salt', '$cookie_value')");
mysql_query("INSERT INTO passwordreset VALUES('$user', NULL, NULL)");
//end of code to repeat
Use functions with parameters, that’s what they’re for. Snippets of code that can be reused in a different context with different variables.