Put together from various tutorials and forum posts, etc...
First piece of PHP I have ever written (I'm using tons of deprecated methods and am probably wrecking a dozen of conventions).
If you have tips to improve it -> shoot ^^
<?
// MySQL Username.
$username = "**********";
// MySQL Pass.
$password = "**********";
// MySQL database.
$database = "**********";
// MySQL host. This is "localhost" or the IP specified by your hosting company.
$host = "**********";
// Connect to the database.
mysql_connect($host, $username, $password);
//Select the database.
@mysql_select_db($database) or die("Unable to establish a connection with the database.");
// Get the name of the script from the URL and protect from injection.
$script = htmlspecialchars($_GET["script"]);
$script = stripslashes($script);
$script = mysql_real_escape_string($script);
// Get the name of the user from the URL and protect from injection.
$user = htmlspecialchars($_GET["user"]);
// Set user name to All if no user key was given.
if (empty($user)) {
$user = "all";
} else {
$user = stripslashes($user);
$user = mysql_real_escape_string($user);
}
$query = "SELECT * FROM $script WHERE username = '$user' LIMIT 1";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
if ($rows == 1) {
// Create the image.
Header('Content-type: image/png');
Header('Cache-Control: no-store, no-cache, must-revalidate, post-check = 0, pre-check = 0');
Header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
Header('Pragma: no-cache');
$image = @imagecreatefrompng('./image.png') or die("Picture not found.");
/*
IMAGE TRANSPARENCY.
*/
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
// Remove all the black from the placeholder image.
imagecolortransparent($image, $black);
// Turn off alpha blending (to ensure alpha channel information
// is preserved, rather than removed (blending with the rest of the
// image in the form of black)).
imagealphablending($image, false);
// Turn on alpha channel information saving (to ensure the full range
// of transparency is preserved).
imagesavealpha($image, true);
/*
PREPARE FOR PRINTING.
*/
$font = './font.ttf';
$fontSize = 8;
$xCoord = 15;
$yCoord = 30;
/*
PRINT.
*/
imagettftext($image, $fontSize, 0, $xCoord, $yCoord, $white, $font, 'Script: ' . $script);
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $column => $value) {
$yCoord += 30;
$string = str_replace('_', ' ', ucwords($column)) . ': ' . $value;
imagettftext($image, $fontSize, 0, $xCoord, $yCoord, $white, $font, $string);
}
}
imagepng($image);
imagedestroy($image);
} else {
echo ("Username not found.");
}
mysql_close();
?>