views:

160

answers:

3

I'm trying to create a login process (as people who have answered my previous question will know)

But it seems impossible to create one process that will work with a desktop app and a standard website.

Instead I have the login function in Usermanagement.php (which is a class, icyntk) but then in another php file (api/login.php) I have:

<?php
require_once '../usermanagement.php';
$m = new usermanage(); 
echo $m->login($_POST['username'], $_POST['password']);
?>

And then I will still need to make another login.php just to be used with the login form on the website. As I can't see anyway to get the result (the echo) of the api/login.php script from the index.php file (where the login form is, it only has HTML in though)

Is there anyway I can simplify this so I only need one login.php?

+1  A: 

As the data from your Desktop App is unlikely to be sent via the $_POST array, I would say no. Websites and Desktop Apps are two completely different types of applications, and it would be unrealistic to think you could share much of the front-end code between the two.

I would try to abstract as much of the functionality as you can into core classes, and then create two separate front-end implementations that utilize the core, each in their own way.

Atli
How would you send data to the website, I had thought the only way was through post but I'm a newb so if you have another way that'd be great
Jonathan
Why should that be unlikely? As long as the request is being made using HTTP he should use POST because GET requests will reveal all variables in access log.
Energiequant
I think we may be talking about different things. When you talked about a Desktop App, I assumed you were talking about writing a Desktop App in PHP (as an alternative to a web app). - If you are simply making a HTTP request to a PHP script from a Desktop App written separately, then this doesn't apply. There is no difference in how a Browser (which *is* just a Desktop App) and any other Desktop App would communicate with a HTTP server.
Atli
@Energiequant I was assuming the Desktop App itself was written in PHP, in which case we would be dealing with GUI events rather than the normal HTTP requests. - If the Desktop App is just doing a normal HTTP request, there is of course no difference between it and a normal browser request. (Assuming the Desktop App correctly handles HTTP requests/responses.)
Atli
+1  A: 

As I understand you, what you want is:

  1. a website/webapp having a login
  2. a desktop app (not in PHP!) logging in using the same login method

One way is to export the login method using the same API in both your website and the remote application, so you would be using JavaScript on client-side to access the API from there (AJAX/JSON). You will want to use some framework for that like jQuery for client-side simplification of AJAX (among many other uses) and maybe the JSON module in PHP (to use json_encode; beware that the module may not be available on some webspaces so if that's out of control don't use it). Of course you do not need to use XML or JSON in your API responses but it's easier to open the API to other (including desktop) applications without the need to manually implement a lot of parsing functions to process the response in your interface classes.

Keep in mind that your website/webapp will not work without JavaScript if you do it this way! On non-public parts of a website that's okay, as is for a webapp used by a known user group, but you should not depend on client-side scripting for public parts of a website.

Another solution to simplify that is by using a PHP framework, so you can write the server-side frontend easier. This will basically enable you to give a button a serverside function which is simply calling your login method and acting accordingly by setting a redirect or replace some panel or whatever you like to continue with after the login.

Of course you can do all that by yourself but this will usually result in either a lot of messy code or an implementation of your own framework. If you want to do it on your own, start by posting the form to the same PHP file instead of an extra login.php and add a hidden field like:

<input type="hidden" name="action" value="login"></input>

In PHP, check $_POST['action']=='login' and call the login method.

Edit: While your website will work with a PHP session or a cookie, you may want to track login status with an own session token which you can pass to your desktop app so it can be used for consecutive calls to the API, so you don't need to handle cookies. These tokens should also be bound to the IP and maybe other "individual" information of the client; that information should ideally be hashed into the token or the token encrypted (client-side won't have to "decrypt" that, just return it for authentication). Tokens should also time out after inactivity.

Also, having read your older question, I fear you could be trying to do something bad like sending a hash over the network and simply check that hash with your user table because JS side encoding was discussed there. If you like to implement some encryption algorithm on client-side, either make sure it's secure (difficult to do that unless you are into cryptography) or resort to SSL.

Energiequant
I haven't done anything with JavaScript the only thing I am going to use it for is in registration to check the passwords match and are secure, like minimum length
Jonathan
A: 

I'm going to get really "limby" and go out on a freaking huge limb and say that this is what you're trying to do:

<?php
if(isset($_POST['username'])) {
    require_once '../usermanagement.php';
    $m = new usermanage(); 
    echo $m->login($_POST['username'], $_POST['password']);
}else{
    ?>Put your login form HTML here<?php
}

This way you only need one login.php file.

mattbasta
Mark Tomlin
The only problem I that the HTML form posts the login info to the login.php in the same way a the vb.net. The difference between the app and the form needs to be for the app the result is echoed but for the form the result is GETed to the index.php unless there are other ways of doing this.
Jonathan
@Jonathan if that's the case, then you should have two separate login files: a desktop version and a web version. The reason why is because you've got two different interfaces: you wouldn't want a web hacker to tap into your desktop version or vice versa. You can still do something like placing the login in a separate file and `include()`ing it.
mattbasta
How did you expect the web version to work when you gave this answer? Perhaps I should do it that way
Jonathan
Generally you have a page with the HTML form and a PHP file to accept the input from the form (and redirect back to the form afterward). The desktop page would have a separate PHP file to perform the authentication.
mattbasta