0

Hello, i'm kinda starting to feel out of my mind now. I am creating a simple web game. I am now in the process of putting some php codes here in game to have scores.My problem is that, My game's top score is the person who played the game very fast, Now I don't know how will I access the time of the game so I can save it to my database. How can I do that?
Here is my html/php/javascript code:

<?php
    session_start();


    if(isset($_SESSION["player_name"])){
    } else {
        header("Location: login1.php");
    }

    include ("config.php");

    if(isset($_POST['score']))
       $player_name = $_SESSION["player_name"];

       $query = "SELECT score FROM player WHERE player_name = '$player_name'";
       $result = pg_query($query);
       $player_data = pg_fetch_assoc($result);
       $current_score = $player_data["score"];

       $new_score = $current_score;

       $query = "UPDATE player SET score = '$new_score' WHERE player_name = '$player_name'";
       $result = pg_query($query);
      header("LOCATION: hogwart2.html");
    } 

    $field = isset($_POST['time']) ? $_POST['time'] : ''; 
?>

<!DOCTYPE html>
<html>
<title>The Wizarding World of Harry Potter </title>
<head>

        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />

<style>

    table{
        height: 15px;
        padding: 0;
        margin: 0;
        vertical-align: middle;
        border: 0px;
            }

    div.container{
        border: 0px;

            }
    div.content{
        border: 0px;

            }

</style>

 <script language="JavaScript">
     <!--
             // set minutes
var mins = 2;

// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;
function countdown() {
    setTimeout('Decrement()',1000);
}
function Decrement() {
    if (document.getElementById) {
        minutes = document.getElementById("minutes");
        seconds = document.getElementById("seconds");
        // if less than a minute remaining
        if (seconds < 59) {
            seconds.value = secs;
        } else {
            minutes.value = getminutes();
            seconds.value = getseconds();
        }
        secs--;
        setTimeout('Decrement()',1000);
    }
}
function getminutes() {
    // minutes is seconds divided by 60, rounded down
    mins = Math.floor(secs / 60);
    return mins;
}
function getseconds() {
    // take mins remaining (as seconds) away from total seconds remaining
    return secs-Math.round(mins *60);
}
           // -->



    <!--
      var timeout, currentTime = 120;
      function adjustTime()
      {
        window.defaultStatus = "Time left = 0:" + 
          ( currentTime < 30 ? "0" : "" ) + currentTime;

          if( currentTime == 30 )
          {
             alert( "You have only 30 seconds left!" );
          }
          else if( currentTime == 0 )
          {

            if(confirm("Times Up!")) {
                window.location.href = "hogwart2.html"
            }
            else {

                window.location.href = "page1.htm"

            }

             //alert( "Time is up!" );
             //document.theForm.theButton.disabled = true;
             //return;
          }

          currentTime = currentTime - 1;
          timeout = window.setTimeout( "adjustTime();", 1000 );
          }

           var count=0;
          function checkAnswer()
          {
            if( !document.myForm.sub.disabled )
          {
              window.clearTimeout( timeout );

              if( document.myForm.answer1.value == "y" && document.myForm.answer2.value == "y" && document.myForm.answer3.value == "y" && document.myForm.answer4.value == "n" && document.myForm.answer5.value == "n" && document.myForm.answer5.value == "n" )
              {
                count++;
                 alert("Congratulations! You have attempted to answer " + count + " times before getting the correct answer!");
                  location.href = 'spell1.html';


              }
              else
              {
                 count++;
                 if (count < 4){
                 alert( "One of your answers is wrong! Attempt: " + count );
                 }
                 else{
                 alert("Sorry. You already attempted " + count + " times!");
                  location.href = 'hogwart2.html';
                 }

              }
          }
       }
     // -->



</script>

</head>

<body background="bgHog.jpg" onLoad="JavaScript: adjustTime();">

<audio autoplay loop>
      <source src="bgsound.ogg">
      <source src="bgsound.mp3">
  </audio>

<table>
<center>
<img src="there_is.png" height="150" width="1350"/>
</center>
</table>

<div id="container" style="width:1350px">

<div value="transparent" id="header" style="background-color:none;" >

<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>

<div value="transparent" id="menu" style="background-color:none;height:450px;width:180px;float:left;">
<?php
                    $player_name = $_SESSION["player_name"];
                    $query = "SELECT score FROM player WHERE player_name = '$player_name'";
                    $result = pg_query($query);
                    $player_data = pg_fetch_assoc($result);
                    $current_score = $player_data["score"];
                    echo "<a><center> Time last play: $current_score </center></a> " 
                    ?>
<br>                 
<b> <center><font size="5px" color=""#4c210e"" face="monotype corsiva">Are these items here? </font> </center> </b> 
<center> <font size="3px" color=""#4c210e"" face="segoe print"> You have 2 mins to find them! </font>  </center>
<div id="timer">
    <center> <font size="4px" color="#4c210e" face="courier new"> Remaining Time: </font> </center> 
    <center> <input id="minutes" id="score" type="text" style="width: 14px; border: none; background-color:transparent; font-size: 16px; font-weight: bold;"> : <input id="seconds" type="text" style="width: 26px; border: none; background-color:transparent; font-size: 16px; font-weight: bold;"> </center>
</div>
<script>
countdown();
</script>


<br>
<form name="myForm" onsubmit="JavaScript: checkAnswer();return false;" autocomplete="off" method="POST" action= "<?php echo $_SERVER['PHP_SELF']; ?>" >

    <center> <b> <font size="4px" color="#4c210e" face="comic sans"> Car </font> </b> </center>  
              [Y/N] 
    <input type="text" name="answer1" maxlength="1" size="4" /> <br> 

    <center> <b> <font size="4px" color="#4c210e" face="comic sans"> Mat  </font> </b> </center>  
              [Y/N] 
    <input type="text" name="answer2" maxlength="1" size="4" /> <br> 

    <center> <b> <font size="4px" color="#4c210e" face="comic sans"> Building Blocks  </font> </b> </center> 
              [Y/N] 
    <input type="text" name="answer3" maxlength="1" size="4" /> <br> 

    <center> <b> <font size="4px" color="#4c210e" face="comic sans"> Open Notebook  </font> </b> </center> 
              [Y/N] 
    <input type="text" name="answer4" maxlength="1" size="4" /> <br> 

    <center> <b> <font size="4px" color="#4c210e" face="comic sans"> King's Chair  </font> </b> </center> 
              [Y/N] 
    <input type="text" name="answer5" maxlength="1" size="4" /> <br> 

    <center> <b> <font size="4px" color="#4c210e" face="comic sans"> Crown  </font> </b> </center> 
              [Y/N] 
    <input type="text" name="answer6" maxlength="1" size="4" /> <br> <br>

    <br>
    <b> <center> <input type="submit" id="score"value="Submit" name="sub" style="height: 40px; width: 70px" /> </center> <b>
    </form>

</div>

<div id="content" style="background-color:#EEEEEE;height:520px;width:1150px;float:left;">

<center> <img src='require.jpg'  width="1150" height="550"  /> </center>

</div>

</body>
</html>
2
Contributors
2
Replies
23
Views
4 Years
Discussion Span
Last Post by ms061210
0

Sorry, the mix of php/html and js is difficult to follow.

You can save the time to a form element and then get the user to submit the form, which then when handled, gets inserted to the db. However, as this is run via JS and data is sent via form, it should be easy to spoof the form so an unscrupilous user could send fake data. You could check against session data / hash to try to obviate that workaround.

0

thanks for your reply, please guide me if Im doing this wrong.

My Javascript for time:

 <script language="JavaScript">
     <!--
             // set minutes
var mins = 2;

// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;
function countdown() {
    setTimeout('Decrement()',1000);
}
function Decrement() {
    if (document.getElementById) {
        minutes = document.getElementById("minutes");
        seconds = document.getElementById("seconds");
        // if less than a minute remaining
        if (seconds < 59) {
            seconds.value = secs;
        } else {
            minutes.value = getminutes();
            seconds.value = getseconds();
        }
        secs--;
        setTimeout('Decrement()',1000);
    }
}
function getminutes() {
    // minutes is seconds divided by 60, rounded down
    mins = Math.floor(secs / 60);
    return mins;
}
function getseconds() {
    // take mins remaining (as seconds) away from total seconds remaining
    return secs-Math.round(mins *60);
}
           // -->

Then my html where the time script is used:

I aaded this php code in my body:

<body background="bgHog.jpg" onLoad="JavaScript: adjustTime();">

<?php
                    $player_name = $_SESSION["player_name"];
                    $query = "SELECT minutes FROM player WHERE player_name = '$player_name'";
                    $result = pg_query($query);
                    $player_data = pg_fetch_assoc($result);
                    $current_score = $player_data["minutes"];
                    echo "<a> score: $current_score </a> " 
                    ?>

after this is the continuation if the body

<form name="timeform" method="POST" action= "<?php echo $_SERVER['PHP_SELF']; ?>" >
<div id="timer">
    <center> <font size="4px" color="#4c210e" face="courier new"> Remaining Time: </font> </center> 
    <center> <input id="minutes" name="minutes" type="text" style="width: 14px; border: none; background-color:transparent; font-size: 16px; font-weight: bold;"> : <input id="seconds" name="seconds" type="text" style="width: 26px; border: none; background-color:transparent; font-size: 16px; font-weight: bold;"> </center>
</div>
</form>

and then here's my php[i don't know if correct, but I think it is wrong, because my database didn't update:

if(isset($_POST['seconds'])){
           $player_name = $_SESSION["player_name"];

           $query = "SELECT seconds FROM player WHERE player_name = '$player_name'";
           $result = pg_query($query);
           $player_data = pg_fetch_assoc($result);
           $current_score = $player_data["seconds"];

           $new_score = $current_score;

           $query = "UPDATE player SET minutes = '$new_score' WHERE player_name = '$player_name'";
           $result = pg_query($query);
          header("LOCATION: hogwart2.html");



if(isset($_POST['minutes'])){
       $player_name = $_SESSION["player_name"];

       $query = "SELECT minutes FROM player WHERE player_name = '$player_name'";
       $result = pg_query($query);
       $player_data = pg_fetch_assoc($result);
       $current_score = $player_data["minutes"];

       $new_score = $current_score;

       $query = "UPDATE player SET minutes = '$new_score' WHERE player_name = '$player_name'";
       $result = pg_query($query);
      header("LOCATION: hogwart2.html");
    } 

And I have a table name player and has four columns:

player_id | password | minutes | seconds|

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.