PHP does not work properly in Android WebView - php

I created a simple app in android studio using WebView. The WebView element connected to a mobile website. When I login, some sessions like user and id are created to make the app work properly. Depending on the values of these sessions, information in a list is shown. But, when I login to the mobile application using the same credentials, it looks like some of the sessions are not created. When I echo'ed the sessions, they are simply not shown.
Some time ago I faced this problem too. Because I had to design the list-items, I hardcoded them. When I finished designing them, I replaced the hardcoded with the old function, and it worked! For a short time tho, after rebooting my device the old problem (as described) appeared again.

Related

Creating an Angular project that uses a PHP login system running on Xampp

I have created a login system running on XAMPP for a web app using php, html, materialise for css, and a tiny bit of javascript. The system consists of login, signup, and index pages. In addition, a database is implemented to store user data and app data in the future. If all form validation is verified, a user's sign up details are stored in the database, after which they can log in normally. If a user successfully logs in, a session is started using the start_session() and SESSION global variable of php. The index page indicates whether a session is currently in progress.
Once a session is in progress, I want to display the application contents (yet to be developed) on the index page where a user can practice their functionalities and log out when they want. I want to implement this logic using angular but I'm confused as to how I'm going to get angular to work with the php login system and the database that are running on xampp. I'm a beginner to angular (and development in general) but from what I understand it comes with it's own "server" so the application contents can be hosted locally. Does anyone have any idea how I can get the angular app working with my implementated database and php login system? Thanks in advance!
I am learning angular now a day but I think I can answer your question. By default angular serve your application on localhost:4200. This application would contain your UI and interaction logic. After the app gets loaded in the browser, your application would call PHP script as you would have normally called with HTML because now everything is on browser so you need not to bother about angular server.
However in angular, you should utilize angular services to make server request.
Your web architecture would look like this (think of apache in place of nginx)

If the WebView in my Xcode app works why would it not work in my Android app?

Today I started my first android app using "Android Studio". I have created a WebView app that displays a PHP scripts which has a HTML form with one input field and a submit button. Once the correct user code is entered the PHP script checks if the user code is correct. If correct opens another PHP script displaying data. At least that what it should do.
I already have a App written in Mac Xcode which works as expected and accessing the PHP using a browser works find.
The PHP script using PHP SESSION variables but the Android app does not seem to like them.
The problem seems to be related to how cookies are handled by Android's WebView.
By default, cookies are not enabled, therefore even if PHP tries to store the session_id, it cannot retrieve it later, and this causes a new session to start.
On SO you can find many answers that explain you how to enable cookies for WebView. See for example this one: How to enable cookies in Android WebView?.

Cookie and session issues in php / mysql / wamp

I use wamp to develop on a windows 7 machine. For this app I have an admin area which tracks the admin username and encrypted password with $_SESSION and a cookie which keeps track of the randomly generated encryption key for the password.
I set the cookie like:
setcookie('key', $key, time()+7200, 'admin/');
The admin user verification is run on every admin page after the admin user has entered username and password details to login.
The directory structure of the site and the admin is:
localhost/mysite/
locahost/mysite/admin/
A session runs in the admin section, and a session also runs in the user interface of the front-end to keep track of chosen criteria for searches.
When I run long database queries in the admin area ie. multiple updates and inserts where each iteration requires a connection to an external API, I am unable to load the front-end user interface in the same browser ie. firefox until after the admin operation has completed. If I use a different browser ie. chrome , I am able to load the front-end of the site while running admin mysql functions with no problems.
I'd like to view the site while these operations are running, and use the same browser. It's a small problem, but I'd like to know how to get around it because I want to learn. I mean I could just tell myself that it doesn't matter because the site works fine when I open another browser, and the final user isn't going to be running admin operations while viewing the site anyway, so from that perspective it doesn't matter, but I am curious.
What is going on with the sessions and cookies that stops me from viewing the site whilst the admin operations are running? Is my question even a good one? Part of me thinks that it's a silly question because ultimately the site works perfectly well in a separate browser. Anyway, thank you for looking!
I think your session is being locked. On the page that takes a long time to run add this at the top: session_write_close(); that should fix it.

Handling sessions in jQuery Mobile and PHP

I'm writing a JQM web app with a PHP web service. Users will be able to sign-in and register that they've made a purchase of, for instance, a soda from the club. This info will be stored in a database and eventually billed.
To illustrate what I want to do: I have already implemented this as an Android app. My "session handling" in the Android app consists of simply storing the user's credentials in Android's savedPeferences (persistent local storage) upon succesful authentication with the server. These credentials are then resent with every subsequent server request so that users only ever have to sign-in once - upon running the app for the first time.
I want to mimic this behavior in my JQM app as closely as possible. Ideally, the user should only ever have to sign in once unless they choose log out.
I'm a bit rusty when it comes to website programming, so what would be the best approach? Non-expiring cookings? Do I use a PHP session or handle everything in javascript?
This is for a hobby project; I prefer a simple solution over something overly secure and complex. Thanks!
Edit: After reading Mike's answer I stumbled across this plugin: https://github.com/carhartl/jquery-cookie
Perhaps this is the easiest way to keep users logged in..?
PHP sessions are going to be invalidated after a set amount of time (depending on your php.ini settings or any runtime modificatoins to the settings).
You can use long-time expiring cookies to persist a login (typically user is given checkbox at login to allow their login credential to be stored).
Since you are developing for a mobile device, you do also have the alternative of using HTML5 local storage since most every Android browser out there supports it. See more info at the link below.
HTML5 Local storage info
I prefer this as the login hash could be persisted even if the user clears their browser cookies and it can be handled strictly within Javascript.

Facebook Connect - Mobile

I am currently in the process of creating a mobile version of my web app.
The app is being developed with Facebook's PHP Client Library.
The issue:
I am using the following mobile url to allow users to log in using the mobile devices:
http://m.facebook.com/tos.php?api_key=APIKEY&v=1.0&next=http%3A%2F%2Ftweelay.net%2Fm.php&cancel=http%3A%2F%2Ftweelay.net%2Fm.php
APIKEY being my app's actual Facebook API key.
In the url I am telling Facebook to redirect the user back to http://tweelay.net/m.php when the user signs in or clicks cancel on the log in screen. I am pulling my hair trying to figure out why it keeps sending the user to http://m.tweelay.net/m.php which is currently an invalid end point.
I have gone through all of my app's settings on Facebook and I cant find any that reference http://m.tweelay.net and going through all of my source code I cant find any that reference the m. sub-domain either.
Any ideas? Is there a setting I'm missing? Maybe a Flag in the library?
I've seen Facebook do this when detecting the mobile browser type and also sometimes randomly through Firefox (it can also happen when trying to get to facebook.com). I've managed to reset it sometimes, but it's not a guaranteed fix.
If you want to be sure the user makes it to your correct site I suggest creating the subdomain and redirecting traffic to your usual site, it's what I did and now I don't worry about it reverting back.

Categories