Jump to content

graffix857

New Members
  • Posts

    4
  • Joined

  • Last visited

Everything posted by graffix857

  1. I've been working on reworking the php web UI to use an internal API..Once that's done I'll start to modify my external api It seems to all make sense to me now. Thanks again for everything!
  2. Every thing is hosted on my server. But I get it now I think. Yes, the site (my php web app) is working as expected so far. When I say site I mean the web UI php app that's already up and running. I had just decided to write a mobile app version of it and started looking up database access an mobile apps. I wanted to write as little code as possible and reuse the code. That's why I was trying to make the external api work with the internal php app but can see I had it all wrong. That last little code snippet made sense to me and I think made it click. Let me just run through it and make sure I understand. So if the user logs in from the php web ui form, loginprocess.php would call loginuser(), gets the array that it returns, parses through the array it and store that in my session variables and such. For the api that the mobile app will access: the api/user/login external api is passed the username and password from the mobile app as a post request, which is then passed to loginuser(), the array that is returned is encoded into json which the mobile app then parses through and stores what it needs to store from the result.. If that's correct then I believe I got it and seeing it like that made sense to me. Oh and what you mentioned earlier about having the token refresh. Would it be bad to have the token_expire update with every request from the external api? Like when it's validated for example. If it's valid, extend the expiration time . And one last question about he token. For the web UI: Since the stored procedure returns the token field, should I just use that and store that in a session variable or cookie and validate it against the one stored in the db every time I try to access data the same as I would with the external api or is that not really needed? just set a session variable flag after they successfully login and check that flag before doing something, not ever taking the token or token expire into consideration on the web UI and just letting the session handle it like it normally would? Sorry for such the noob questions. I haven't really done this stuff since classic ASP was first released! I'm familiar with server side stuff from that but just old stuff and it seems like a lot has changed. This is the first time I've dealt with PHP and the first experience I have writing any kind of web service(on top of that I did some of the web UI with polymer which I have never used before either). It's been an adventure that's for sure. I was also reading a lot on stackoverflow which I see now probably made me more confused than anything else and lead me down many dead ends.. I really really do appreciate your very detailed answer and taking the time to help me understand this!
  3. I think I understand it much better now. So basically the site won't use the REST API Only the mobile app?. I guess the only time your site would really need to call an external api is if it was a server hosted elsewhere or something and not on the traditional site. Is that correct? Your last paragraph threw me a little. Using the login function I pasted above, suppose that's my internal api. How will that internal api know if it was the site that called it or if it was the external api that called it and know what to return based on that? Would it be bad to just leave the site as is and just focus on the external api for the mobile app to consume? Thanks for your response as I think it is much clearer now then it was before. haha
  4. Hi all, I currently have a web app (php my first once actually) that accesses a MySql database. How I was handling the login was like this: This user enters the username and pw which gets sent to a stored procedure. If they successfully are validated I output a record (contains userid, first name and if they are logged in or not) and I set two session variables, 1 that stores a boolean if they are logged in (1 if they are, 0 if they are not) and the other stores the user id of that user (so I can use later to make sure they only get their data). I then check these session variables to 1.Make sure they are logged in and 2. Make sure they are only requesting their data (userid) I'm now going to be working on an Android app and make all the data access stuff a rest api that both the app and the website can consume. I modified the login stored procedure so it now will return a token as well. The token is generated on the DB(a hashed value of a couple of fields concatenated). When they log in successfully the token generated is stored in a user token table.(one user, one token) The table also stores a token_expire timestamp. Every time they log in a new token is created(and token_expire is updated). If they try to do something after the token expired (based on the token_expire field) then it should redirect them to login so a new token can be created. When I do the Android app, dealing with this and storing this token on the client is easy and there are many ways to store it (I was thinking storing it in a local sqlite table, shared_prefs (prob not the best way) etc..) and I would just parse through the json result. So keeping track of the token is easy with the app but my problem comes in with the PHP web site. So I'm faced with two issues: Issue 1. Right now I have a php form (with login and password fields) and it posts to a login process page which calls the stored procedure and if all is good redirects them to a dashboard page. Now if I use rest the post action would be something like: api/users/login instead of loginprocess.php correct? But then the api just spits out json and I'm not sure how to hand the result from the api to the php code. As when I change the post action I just get a white page with the json result string. So I need help knowing what to do once the api returns the result. Does this have to be called differently than a normal form submit? Do I just have the form submit call a js funcation that makes the call to the page and parses the result? Similar to something like this but instead of passing the cookie passing the login information? $opts = array('http' => array('header'=> 'Cookie: ' . $_SERVER['HTTP_COOKIE']."\r\n")); $context = stream_context_create($opts); session_write_close(); // unlock the file $contents = file_get_contents(url, false, $context); Issue 2. Once this token is generated in MySQL and sent back to the api, I need to pass it back to the PHP(related to #1) but How do I store it so that other pages of the site can send it when it requests the data? I know it has to be sent in the header in future requests and that's not my issue. My issue is where do I store the token on the web client so that other pages can use it? Should I store it in a client cookie? (but then doesn't this go against rest?) Store it in local storage? I'm pretty new to PHP and REST (was a classic ASP guy and just getting back into this stuff. This project is the first personal project for myself to learn this stuff and get the rust out) so I'm just trying to figure out the best way. I do not want to use sessions as that violates REST. I also do not want to use oauth or any 3rd party solution. I have been reading a lot about this but I'm still unclear as to how to go about these changes to the web version of the app and have it function properly. This is what my rest login api looks like so far (I know this will have to change but I'm stuck here with it): function loginUser() { global $app; $req = $app->request(); $paramUsername = $req->params('username'); $paramPassword = $req->params('password'); $sql = "CALL checkPassword(:username,:password)"; try { $dbCon = getConnection(); $stmt = $dbCon->prepare($sql); $stmt->bindParam("username", $paramUsername); $stmt->bindParam("password", $paramPassword); $stmt->execute(); $result = $stmt->fetchAll(); $loggedin=$result[0]["loggedin"]; $uid= $result[0]["uid"]; $fname=$result[0]["firstname"]; $token=$result[0]["token"]; $response["uid"]=$uid; $response["loggedin"]=$loggedin; $response["firstname"]=$fname; $response["token"]=$token; echo json_encode($response); $dbCon = null; } catch(PDOException $e) { echo '{"error":{"text":'. $e->getMessage() .'}}'; } } Which returns: {"uid":"100","loggedin":"1","firstname":"John","token":"f0165d67221563bef150018276f4f77b7bd1e1763223e"} Here is what the form looks like calling the api currently: <form id="login" method="post" action="webservices/api/users/login"> <input class="my-class" style="width:20em" type="email" name="username" required> <input class="my-class" style="width:20em" type="password" name="password" required> <button type="submit" id="SubmitButton" name="submit" "></button> </form> Can anyone recommend the best way to deal with these two issues? Any help would be appreciated. Oh I should mention I'm using slim to help with the rest api's. TIA
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.