In the previous Hangman article we wrote with PHP and GD a graphics port that with this code will complete the hangman game. This bit of code handles the user interaction as well as initialization of the game.
This code can be added on to the other code to create a complete game.
Without further ado here is the entire hangman game user interface written in php
$misses=0;
if($_REQUEST['addmiss'] == 1){
$misses = 1;
}
if($_REQUEST['draw']==1){
session_start();
$misses = $_SESSION['misses'] + $misses;
$_SESSION['misses'] = $misses;
$im = drawTxt($im,$word,$_SESSION['choices']);
$im = drawHangMan($im,$misses);
header ("Content-type: image/png");
ImagePng ($im);
}
if($_REQUEST['new_word']){
require_once('/home/rionmass/hawkenterprises-wordlist-config.php');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
$sql = 'SELECT word from TABLENAME LIMIT 1';
$rs = mysql_query($sql);
$word = mysql_result($rs,0,0);
session_start();
$_SESSION['word'] = $word;
}
session_start();
if(!isset($_SESSION['word'])){
header('location:?new_word=1');
}else{
$_SESSION['choices'][] = $_POST['choice'];
if(isset($_POST['choice'])){
$_SESSION['choices'][] = $_POST['choice'];
if(false !== strpos($_POST['choice'],$_SESSION['word'])){
header('location:?draw=1');
}else{
header('location:?draw=1&addmiss=1');
}
}
}
?>
I hope you enjoyed this little series I know we did.
His achievements read like the graffiti on the walls of a hangman’s changing room.
Johnathan Larsen











