Reputation: 3469
So. I'm accessing a PHP page and reading some data from it using a Java URL() object. I was wondering if someone could show me how, using PHP, to check if the person viewing the page is accessing it from the Java application, or from a real web browser (I don't want them to see the data if they're using a web browser).
My current Java Code is:
try{
URL url = new URL("http://www.mysite.com/myurl.php");
Scanner s = new Scanner(url.openStream());
while(s.hasNext()){
String auths = s.next();
String real = "test";
if(auths.equalsIgnoreCase(real)){
return true;
}else{
return false;
}
}
return false;
}catch(Exception e){
return false;
}
And my PHP file at "http://www.mysite.com/myurl.php" has this code:
<?php echo "test"; ?>
Thing is, I only want the PHP file to output the "test", if it's being accessed from the Java application. Can anyone help me out?.. Thanks
Upvotes: 0
Views: 89
Reputation: 20404
There is no foolproof solution for what you're trying to do.
Having this said you can use get_browser() or $_SERVER['HTTP_USER_AGENT']
to read the user-agent
request header.
Upvotes: 3