PHP and Javascript/HTML5 Collaboration [closed]

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I've recently been working on a fairly complicated game. I've stored information with local storage, but that allows the player to edit it, and does not transfer from computer to computer. The two scripts run on a single server, and I use the following to ping the php server:

function initiateLoginSequence() {
    var isnewplayer=prompt("Hello! Are you new?(Y/N)");
    if(isnewplayer.equalsIgnoreCase("N")) {
        var username = prompt("Enter username.");
        var password = prompt("Enter Password.");
        //parsing code goes here later.
    } else {
        var username = prompt("Enter a username. This will be used for future logins.");
        var password = prompt("Enter a password. This will be used for future logins.","*");
        var req = new XMLHttpRequest();  
        req.open('POST', 'http://69.144.34.106/Scores.php', true);   
        req.send("returning=false"+"&name="+username+"&pw="+password+"&level="+lvl+"&save=true");
        req.send();
    }
}

I then process the results with this:

php
   $returning=_POST["returning"];
   $save=_POST["save"]
   $password=_POST["pw"];
   $username=_POST["name"];
   $score=_POST["lvl"];
   $connection=mysql_connect("localhost:3306","****","*****");
   if($returning=="false" && $save=="true") {
     mysql_select_db("userdata",$connection);
     $sqlcmd = "CREATE TABLE ".$username."(
     Username varchar(".$username.")
     Password varchar(".$password.")
     Score varchar(".$score.")
     )";
     mysql_query($sqlcmd);
   } else if($save==false) {
     $sqlcmd="SELECT * FROM userdata"
     $result = mysql_query($sqlcmd);
     while($row=mysql_fetch_array($result)) {
        if($row['Username']==$username && $row['Password']) {
            echo row['Score'];
        }
     }
   } else 
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

 

Several points:

  • You should definitely enable error reporting and error logging into a file on the PHP side, otherwise you'll have no evidence of problems. AJAX errors usually cause little visible error to the user.
  • You should check for errors on the Javascript side too

PHP error handling is famous for being both bad (e.g. the default action for most unusual conditions is to continue regardless and issue a warning, which possibly goes to the client browser, or might be ignored as well), AND complicated. Once you fully understand PHP error handling, you can write an error handler which carries out appropriate actions in all cases.

In my opinion:

  • Creating a table for each user is not a good database model (Consider a row per user instead)
  • Hard-coding the server IP address in the Javascript code is almost certainly not the right thing to do (e.g, how do you move your application from your test to production server without changing it?)
  • a PHP error handler should log all errors and report every possible piece of contexual information (especially a stack trace) into the error log, and stop execution; it is necessary to provide a mechanism for certain errors to be suppressed in some cases (for example, error_reporting). Don't use the @ operator ever.

Security:

  • You may wish to research what SQL injection is, before you continue to use PHP / MySQL in your application (Hint: Consider using parameterised queries)

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.