Wizzup?
2008-09-08 09:32:48
Some days ago I very quickly made a system that allows you to add hints to a database, and display them in text form, with a transparent background. I thought they might be a nice addition to SRL?
There are options to use a random hint, or a static one. The link defines whatever you choose.
Random: (www.villavu.com/HintSigs/) You might get the same one as the static one, since it's random.)
Some ID:www.villavu.com/HintSigs/2.png

All I need now is more hints, and idea's what kind of hints there should be in the database.
EDIT:
Done!
Here is the code for making the signature:
[php]<?php
header("Content-type: image/png");
header("expires: Mon, 26 Jul 1990 05:00:00 GMT");
header("last-modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("cache-control: no-store, no-cache, must-revalidate");
header("cache-control: post-check=0, pre-check=0", false);
header("pragma: no-cache");
include_once 'Functions.php';
$link = $_SERVER['PHP_SELF'];
$start = strpos($link, 'HintSigs/');
$end = strpos($link, '.png');
$id = substr($link, $start + 9, $end - $start - 9);
if(($id == 'random') or (!is_numeric($id))) {
$hint = randomHint();
} else {
$hint = getHint($id);
}
$im = imagecreatetruecolor(500, 100);
$color = imagecolorallocate($im, 255, 255, 255);
imagecolortransparent($im, $color);
imagefill($im, 0, 0, $color);
$color2 = imagecolorallocate($im, 100, 100, 100);
$imw = imagesx($im);
$imh = imagesy($im);
$font = 2;
$startx = 0;
$starty = 0;
$max_rows = ceil($imh - 15) / (imagefontheight($font) + 5);
$words = explode(' ', 'Did you know that… ' . $hint);
$s = '';
$len = 0;
foreach($words as $word) {
$len+= ((strlen($word) + 1) * imagefontwidth($font));
if($len > $imw) {
$len-= (strlen($word) + 1) * imagefontwidth($font);
$startx = ($imw / 2) - ($len / 2);
imagestring($im, $font, $startx, $starty, substr($s, 0, strlen($s) - 1), $color2);
$starty+= imagefontheight($font) + ceil(5 / $max_rows);
$len= strlen($word) * imagefontwidth($font);
$s = '';
}
$s.= $word . ' ';
}
$startx = ($imw / 2) - ($len / 2);
imagestring($im, $font, $startx, $starty, substr($s, 0, strlen($s) - 1), $color2);
imagepng($im);
imagedestroy($im);
?>[/php]
And here is the other code:
[php]<?php
function connect_SQL($db) {
$mysql_server = '';
$mysql_user = '';
$mysql_password = '';
$connection = mysql_connect($mysql_server, $mysql_user, $mysql_password) Or Die("Could not connect to mySQL Database.");
if(!empty($db)) {
mysql_select_db($db);
}
return $connection;
}
function loggedIn() {
return $_SESSION['loggedin'] == true;
}
function register() {
if(isset($_POST['rname']) & (isset($_POST['rpass']))) {
$conn = Connect_SQL('srlhints');
$name = mysql_real_escape_string($_POST['rname'], $conn);
$pass = md5(strtolower($_POST['rpass']));
mysql_query('insert into user (user, pass) VALUES (\'' . $name . '\', \'' . $pass. '\')');
mysql_close($conn);
}
}
function logIn() {
if(isset($_POST['name']) & (isset($_POST['pass']))) {
$conn = Connect_SQL('srlhints');
$name = mysql_real_escape_string($_POST['name'], $conn);
$pass = md5(strtolower($_POST['pass'])); // Don't really need mysql_real_escape_string() here…
$query = mysql_query('SELECT Id, user From user Where user = \'' . $name . '\' AND Pass = \'' . $pass. '\'');
if(mysql_num_rows($query) == 1) {
$the_array = mysql_fetch_array($query);
$_SESSION['loggedin'] = true;
$_SESSION['userid'] = $the_array[0];
echo 'You are logged in. <br/>';
}
mysql_close($conn);
}
}
function addHint() {
if(loggedIn()) {
if(isSet($_POST['hint'])) {
$conn = Connect_SQL('srlhints');
$hint = mysql_real_escape_string($_POST['hint'], $conn);
mysql_query('insert into hint (value, userid) VALUES (\'' . $hint . '\', \'' . $_SESSION['userid'] . '\')');
}
}
}
function numberOfHints() {
$conn = connect_SQL('srlhints');
$raw_result = mysql_query('Select Count(ID) From hint');
$result_arr = mysql_fetch_array($raw_result);
mysql_close($conn);
return $result_arr[0];
}
function randomHint() {
$hintid = numberOfHints();
$conn = connect_SQL('srlhints');
$raw_result = mysql_query('Select value From hint where id = ' . rand(1, $hintid));
$result_arr = mysql_fetch_array($raw_result);
$hint = $result_arr[0];
mysql_close($conn);
return $hint;
}
function getHint($id) {
$hintid = numberOfHints();
$conn = connect_SQL('srlhints');
$raw_result = mysql_query('Select value From hint where id = ' . $id);
$result_arr = mysql_fetch_array($raw_result);
$hint = $result_arr[0];
mysql_close($conn);
return $hint;
}
function listHints() {
$conn = connect_SQL('srlhints');
$raw_result = mysql_query('Select hint.id, hint.value, user.user From hint, user where hint.userid = user.id');
echo 'All Hints:
<table border="2">';
while(list($id, $hint, $username) = mysql_fetch_row($raw_result)) {
echo '<tr>
<td width="50"><center>
' . $id . '
</center></td>
<td width="100"><center>
' . $username . '
</center></td>
<td width="300"><center>
' . $hint . '
</center></td>
</tr>';
}
echo '</table>';
}
?>[/php]
I apologize for the bad coding (and html), I have never written code as ugly as this in PHP, but it does the trick, and I wanted to finish this small system quick, so you guys can add hints.
http://www.villavu.com/villavu/srlhints/
EDIT2: I cleared all the hints. Now have fun adding them!
