homepage Welcome to WebmasterWorld Guest from 54.167.185.110
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP to Java
A little help making a PHP file into a Java class
Alternative Future




msg:4064196
 1:29 pm on Jan 20, 2010 (gmt 0)

Greetings

I was wondering if there was anyone that knew what the equivalent java code (not javascript) would be to the PHP below:

session_start();

($_SERVER['REQUEST_METHOD'] == "POST" && $_POST[variable] && $_POST[variable] == $_SESSION[variable])

$rand = rand(0,4);
$_SESSION[variable] = $rand;
echo $rand;

With thanks in advance

-gs

 

CyBerAliEn




msg:4064343
 4:49 pm on Jan 20, 2010 (gmt 0)

One, Java is not entirely like PHP --- it does not contain SESSIONS. Why? PHP is a web language (primarily), where a request hits the server, it is processed, then is finished. Sessions exist so that you can store relevant information "in memory" so that is available the next time a request is made. Compiled applications (ex: Java) don't need these because they are running on the system: their variables are stored in memory until the application destroys them or until the application terminates (same purpose as sessions). Likewise, Java lacks such information as a "SERVER" and "POST" variable.

With this in mind... I don't see how you can 100% exactly replicate your code into Java because there are some fundamental differences.

Your code is conditional on:
(a) request type is post;
(b) post variable 'variable' exists (?); and
(c) post variable 'variable' equals the value of 'variable' in SESSION

If this occurs, your code then create a random integer (0 to 4) and sets this to the session variable, then displays the value.

Considering just this logic, you can recreate the idea in Java. I haven't coded Java in awhile, so I do not recall the exact notations/syntax, but here's what you got to do:

It looks like you are trying to compare a "posted or given value" against a value "stored in memory". All you need to do is get the value you want from the user. You can do this by creating a dialog requesting user input [ex: store it as something 'userinput']. You can then return this input to a method that checks if the entered value matches a predefined value. You'll want to create a global/class variable that holds the defining value [ex: call it 'setting']. This method can then check if the returning 'userinput' matches the 'setting'. If it does, then you go through the process of updating the value of 'setting' with a new random integer (Java has such functionality built into its math library I believe). You can then also display this new value at this point. However, if they do not match... you could then alert the user that the values do not match, and even restart the process by recalling the dialog for input.

Hope that makes sense. I'm sure you were looking for the code to copy/paste, but that isn't always my style... perhaps my thoughts can help you figure out how to get your "idea" working in Java. :)

Regardless... What you are doing in your code seems ambiguous and like a quasi-validation attempt. I am curious as to what/why/how you are doing this.

Note my experience with Java is in standalone, compiled applications. Though I suppose some sites may be using a Java backend, I am not familiar with this.

Alternative Future




msg:4064882
 8:46 am on Jan 21, 2010 (gmt 0)

Hi CyBerAliEn

Thanks for your explanation; it makes a lot of sense.

The PHP code posted was part of a captcha that I was going to try and implement on my site.

-Gs

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved