My GTN php game is showing a blank page

Started on the old forum in July 2011 · view the original in the archive · old links redirect here

#debugg #guess-the-number #php #script

6 replies · 718 views · started 15 years ago · 1 min read

4 participants
s3nzo Developer · 156 posts #1

Hey guys i'm upgrading guess the number game i created, i saved the number on a text file but i had problems if many users played the game simultaneously because the number gets overwritten so now i want to use a database to store the number where a nickname, ip address, generated number and user id will be stored. But now the game shows blank pages can you help? Here's the source code

<html>
<head>
<title>wapEvening | Guess the number game</title>
<link rel="stylesheet" type="text/css" href="includes/style.css"/>
</head>
<body>
<?php
require_once "includes/header.inc";
require_once "includes/con.inc";
$ran = rand(1,50);
$ip  = $_SERVER['HTTP_X_FORWARDED_FOR'];
$insert = "INSERT INTO *** (id,ip,number)
VALUES(NULL,{$ip},{$ran})";
$enter = @mysql_clean(mysql_query($insert));
print 'Welcome to guess the number game, you have 5 chances to guess the number between 1 and 50
(included) you loose 10 points for every incorrect number you guess, you get a bonus of 20
points if you guess correct number first time';
$nick  = print '<input type="text" size="5" maxlength="10" name="nick">';
$guess = print '<input type="text" size="1" maxlength="1" name="guess">';
print "\nEnter :\n<form action=\"gtn_answer.php\" method=\"POST\">Nickname : ".$nick."\nYour
guess : ".$guess."</form>";
?>
</body>
</html>
i0nutzxp Developer · 150 posts #2

You have a fatal error on $enter = @mysql_clean(mysql_query($insert)); -> remove @ to see what is the problem .... Better use Sessions instead database for a little game ...

<?php unlink('World/Europe/Romania.country'); ?>
arnage Lead · 811 posts #3

For example:

function mysql_clean($var) {
$var = mysql_real_escape_string($var);
return $var;
}
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
i0nutzxp Developer · 150 posts #4

You cant clean the query .... and here its a simple example ...

<?php
// author : Ionutxp
// of course , whatever

session_start();
error_reporting(0);

if (isset($_GET['action']))
    $act = $_GET['action'];
else
    $act = '';

function check_game()
{
    if (!isset($_SESSION['game_number']))
    {
        return 'INACTIVE';
    }
    if (isset($_SESSION['game_number']) && $_SESSION['game_errors'] < 5 && !$_SESSION['game_guessed'])
    {
        return 'ACTIVE';
    }
    if (isset($_SESSION['game_guessed']) && $_SESSION['game_guessed'])
    {
        return 'WON';
    }
    if (isset($_SESSION['game_errors']) && $_SESSION['game_errors'] > 4)
    {
        return 'LOSE';
    }
    print_r($_SESSION);
}
function print_formular($params = array('status' => 1, 'text' =>'Type a number!'))
{
?>
 <html> 
<head> 
<title>wapEvening | Guess the number game</title> 
<link rel="stylesheet" type="text/css" href="includes/style.css"/> 
</head> 
<body>System: 
<?php print $params['text']?>
<br/>
<?php if ($params['status'] != 2)
{
?>
<form action="" method="post">
Number : <input type="text" name="number"/>
<input type="submit" value="Try!"/>
</form>
<?php
}
?>
</body> 
</html>   
 <?php

}

if (isset($_GET['newgame']))
{
    unset($_SESSION['game_number']);
    unset($_SESSION['game_guessed']);
    unset($_SESSION['game_errors']);
    header('Location: guess_number.php');
}
$arr = array();
$game_status = check_game();

switch ($game_status)
{
    case 'INACTIVE':
        {
            $_SESSION['game_number'] = mt_rand(1, 100);
            $_SESSION['game_guessed'] = false;
            $_SESSION['game_errors'] = 0;
            print_forumlar();
        }
        break;
    case 'ACTIVE':
        {
            if (isset($_POST['number']) && ((int)$_POST['number'] > 0 && (int)$_POST['number'] <=100))
            {
                if ($_POST['number'] == $_SESSION['game_number'])
                {
                    // give plusses sql
                    
                    $_SESSION['game_guessed'] = true;
                    $arr['text'] = 'You won!';
                    $arr['status'] = 2;
                    print_formular($arr);
                    exit;
                }
                if ($_POST['number'] != $_SESSION['game_number'])
                {
                    // here you can put mysql query to extract plusses
                    $_SESSION['game_errors']++;
                    if ($_SESSION['game_errors'] < 5)
                    {
                        if ($_POST{'number'} > $_SESSION['game_number'])
                        {
                            $arr['text'] = 'The secret number is lower.';
                            $arr['status'] = 1;
                        }
                        else
                        {
                            $arr['text'] = 'The secret number is bigger.';
                            $arr['status'] = 1;
                        }
                        print_formular($arr);
                        exit;
                    }
                    else
                    {
                        // lose
                        $arr['text'] = 'You lose.';
                        $arr['status'] = 2;
                        print_formular($arr);
                        exit;
                    }
                }
            }
            else
                print_formular();
            exit;
        }
        break;
    case 'WON':
        echo 'Already won! Start a new game?!<br/><a href="' . $_SERVER['php_self'] .
            '?newgame">YES!</a>';
        break;
    case 'LOSE':
        echo 'Already losed! Start a new game?<br/><a href="' . $_SERVER['php_self'] .
            '?newgame">YES!</a>';
        break;
}
?>

I didnt tested yet ...

<?php unlink('World/Europe/Romania.country'); ?>
arnage Lead · 811 posts #5

Thats just what "mysql_clean" in this case probably ment to be.

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
something else Fellow · 2,296 posts #6
$nick  = print '<input type="text" size="5" maxlength="10" name="nick">'; 
$guess = print '<input type="text" size="1" maxlength="1" name="guess">';

to :

$nick  =  '<input type="text" size="5" maxlength="10" name="nick">'; 
$guess =  '<input type="text" size="1" maxlength="1" name="guess">';

will help as there is no need to print thoose lines

s3nzo Developer · 156 posts #7

Thanks guys i removed

@mysql_clean

now the script is working and Thanks to

something else wrote:

$nick  =  '<input type="text" size="5" maxlength="10" name="nick">'; 
$guess =  '<input type="text" size="1" maxlength="1" name="guess">';

will help as there is no need to print thoose lines

That's a stupid mistake i made there

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close