Hot questions for Using Applets in php

Question:

I'm trying to wrap my mind around how to deploy a Java applet in my Laravel app.

To start with I'm just going to keep it simple and use the applet tag in a view:

<applet code="OHLib.class" width=0 height=0 />

(The applet contains just helper functions ... no UI, so I set width and height to 0. Will that work?)

When the browser requests that class from my web site, how to I serve it up? Do I define a route? I would like the java class to be located at www.mydomain.com/java/OHLib.class.


Answer:

Deploy An Applet

Applets are deployed onto a web page via certain tags, which vary by browser, so the best thing to do is use a script provided by Oracle to inject the correct tag for you. I put the following code in a View so as to appear within the <body> tag. (It won't work in the <head> section.)

<script src="https://www.java.com/js/deployJava.js"></script> 
<script>
    var attributes = { 
        id:'ohApplet',
        code:'OHLib',
        codebase: 'java',
        archive: 'OHLib.jar',   
        width:1, 
        height:1,
    } ;
    var parameters = { 
        jnlp_href: 'OHLib.jnlp',
        classloader_cache: 'false',
    } ;
    deployJava.runApplet(attributes, parameters, '1.6');
</script>

This will cause the browser to load OHLib.jar from the java folder. If the .jar file is in the same folder as the HTML document, you can skip the codebase attribute. See Deploying a Java Applet.

Serve An Applet

When the browser parses the injected tag, it will then make a request to the server for the Java applet, based on the 'archive' and 'codebase' attributes. So in my case I created a folder called 'java' under the Public folder, and placed the .jar file in there. Remember that your web app only gets invoked for URIs that don't exist on the server file system.

Question:

I want to have a simple web page with fields for userName and password.Once a user enters the correct userName and password and press logIn button then it should display an applet. Here's my code.

<?php  
$un=$_POST["username"];  
$pw=$_POST["password"];  
$log=$_POST["Login"];  
$con=mysqli_connect("localhost","root","","");  
if(mysqli_connect_errno($con))  
    {  
    echo "Failed to connect".mysqli_connect_error();  
    }  
mysqli_select_db($con,student);  
$query="SELECT * FROM studentinfo WHERE stName=$un AND stP=$pw";  

$result=mysqli_query($con,$query);  
$num_rows = mysqli_num_rows($result);  
if($log){  
    if($num_rows==1){  
    $isLogged=true;  
    }  
    else{  
    echo "Error log In.Invalid username or password";     
    $isLogged=false;  
    }  
}  
?>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>User LogIn</title>  
</head>  

<body>  
<form method="post">  
            <label>Username<input name="username" type="text" /></label> <label>Password<input name="password" type="password" /></label>  
            <input name="cmd" type="submit" value="Login" />  
        </form>  
<?php if($isLogged) {?>  
<applet code="studentWeb.html" width="32" height="32" alt="Couldn't launch applet" title="Student Details">  
</applet>  
<?php }?>  
</body>  
</html>  

Right now it gives an error as

mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in and it doesn't open up a applet even when correct username and password has been entered.

Edit: After I changed to action="logindata.php" in the form, here's the logindata.php

<?php
$connect=mysqli_connect("localhost","root","");
if(mysqli_connect_errno($connect))
{
    echo "Failed to connect".mysqli_connect_error();
}
mysqli_select_db($connect,"student") or die("couldn't connect to db");
$un=$_POST["username"];
$pw=$_POST["Password"];

$sql="SELECT * FROM studentinfo WHERE stUserName='$un' AND stPassword='pw'";

$query=mysqli_query($connect,$sql) or die("couldn't find values");

if($query){
    include_once("studentWeb.html");
}
else{
echo ("Invalid username or password");

}


?>  

Why does this allow incorrect password and username ?


Answer:

Quote these WHERE stName='$un' AND stP='$pw' since we're dealing with strings, which is why you are getting a boolean error.

Plus, use isset() around your executed code since you're using your entire code inside the same file.

You stand at getting an undefined index warning upon page entry.

Using error reporting will give you that:

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Also add or die(mysqli_error($con)) to mysqli_query()


Footnotes:

Your present code is open to SQL injection.

Use prepared statements, or PDO with prepared statements, they're much safer.


Edit:

If you wish to include a file (show contents of), do the following, which I am under the impression you wish to do:

<?php if($isLogged) { 

    include 'studentWeb.html';
}
?>

This is to be replaced by your present code:

<?php if($isLogged) {?>  
<applet code="studentWeb.html" width="32" height="32" alt="Couldn't launch applet" title="Student Details">  
</applet>  
<?php }?>

You can also try:

<?php $file = file_get_contents('studentWeb.html', true);
  echo $file;
?>

or '../foldername/studentWeb.html' depending on where the file is located.


The <applet> tag is not supported in HTML5. Use the <object> tag instead if you are having problems with your present code. Yet, applets usually have the .class extension.

Consult http://www.tutorialspoint.com/html/html_applet_tag.htm


Edit #2:

As per your edit, change:

if($query){
    include_once("studentWeb.html");
}
else{
echo ("Invalid username or password");

}

to:

$numrows = mysqli_num_rows($query);

if($numrows > 0){
   include_once("studentWeb.html");
}

Question:

I am developing a Web based Biometric registration portal (PHP+MySQL+JavaScript) where a student will be registered with his personal info and his fingerprint. The fingerprint will be taken trough Futronic FS82 fingerprint scanner. The device supplier company Futronic (http://www.futronic-tech.com) are providing SDK for VB.NET and JAVA.

This device captures the finger image and saves the image in a specific folder and search any user with the fingerprint from that specific folder.

Now I am trying to integrate everything in my web portal where there will be a button like "Scan". When someone clicks that button, automatically the user will be asked to provide fingerprint and I need to save it in my specified folder. And again when I will search the user with his fingerprint, it will search from that folder and give me the result.........

I am not getting any idea how to do it. Please guide me....Do I need to make a java applet or something? How can I do that?


Answer:

Since PHP is server-side, you cant just click a button, and let it wait for input.

So if the fingerprint scanner can print a value thats unique to each finger (like most barcode scanners do), you can just do an input field, and have that POST to a PHP script, that will verify the value, will all the already saved onces.

If it dosn't return a value, you will most likely need to create a VB.NET or JAVA "plugin", that can handle this, and either post a value it self, or make it return the value of the finger thats scanned.